[clang-tools-extra] b31486a - [clangd] textDocument/implementation (LSP layer)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 04:58:46 PST 2020
Author: Utkarsh Saxena
Date: 2020-11-23T13:50:44+01:00
New Revision: b31486ad971774c859e3e031fc0d8d9b77e3b083
URL: https://github.com/llvm/llvm-project/commit/b31486ad971774c859e3e031fc0d8d9b77e3b083
DIFF: https://github.com/llvm/llvm-project/commit/b31486ad971774c859e3e031fc0d8d9b77e3b083.diff
LOG: [clangd] textDocument/implementation (LSP layer)
Differential Revision: https://reviews.llvm.org/D91721
Added:
clang-tools-extra/clangd/test/implementations.test
Modified:
clang-tools-extra/clangd/ClangdLSPServer.cpp
clang-tools-extra/clangd/ClangdLSPServer.h
clang-tools-extra/clangd/ClangdServer.cpp
clang-tools-extra/clangd/ClangdServer.h
clang-tools-extra/clangd/test/initialize-params.test
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index e726271fe7cbe..335a6fc9ad94e 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -604,6 +604,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
}},
{"declarationProvider", true},
{"definitionProvider", true},
+ {"implementationProvider", true},
{"documentHighlightProvider", true},
{"documentLinkProvider",
llvm::json::Object{
@@ -1291,6 +1292,22 @@ void ClangdLSPServer::onReference(const ReferenceParams &Params,
});
}
+void ClangdLSPServer::onGoToImplementation(
+ const TextDocumentPositionParams &Params,
+ Callback<std::vector<Location>> Reply) {
+ Server->findImplementations(
+ Params.textDocument.uri.file(), Params.position,
+ [Reply = std::move(Reply)](
+ llvm::Expected<std::vector<LocatedSymbol>> Overrides) mutable {
+ if (!Overrides)
+ return Reply(Overrides.takeError());
+ std::vector<Location> Impls;
+ for (const LocatedSymbol &Sym : *Overrides)
+ Impls.push_back(Sym.PreferredDeclaration);
+ return Reply(std::move(Impls));
+ });
+}
+
void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params,
Callback<std::vector<SymbolDetails>> Reply) {
Server->symbolInfo(Params.textDocument.uri.file(), Params.position,
@@ -1431,6 +1448,7 @@ ClangdLSPServer::ClangdLSPServer(class Transport &Transp,
MsgHandler->bind("textDocument/signatureHelp", &ClangdLSPServer::onSignatureHelp);
MsgHandler->bind("textDocument/definition", &ClangdLSPServer::onGoToDefinition);
MsgHandler->bind("textDocument/declaration", &ClangdLSPServer::onGoToDeclaration);
+ MsgHandler->bind("textDocument/implementation", &ClangdLSPServer::onGoToImplementation);
MsgHandler->bind("textDocument/references", &ClangdLSPServer::onReference);
MsgHandler->bind("textDocument/switchSourceHeader", &ClangdLSPServer::onSwitchSourceHeader);
MsgHandler->bind("textDocument/prepareRename", &ClangdLSPServer::onPrepareRename);
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h
index b9200f6a2e1b8..4d568bc13d8bf 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -115,6 +115,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
Callback<std::vector<Location>>);
void onGoToDefinition(const TextDocumentPositionParams &,
Callback<std::vector<Location>>);
+ void onGoToImplementation(const TextDocumentPositionParams &,
+ Callback<std::vector<Location>>);
void onReference(const ReferenceParams &, Callback<std::vector<Location>>);
void onSwitchSourceHeader(const TextDocumentIdentifier &,
Callback<llvm::Optional<URIForFile>>);
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index b6f9fcfd23da8..889d2cbcf2807 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -718,6 +718,18 @@ void ClangdServer::foldingRanges(llvm::StringRef File,
TUScheduler::InvalidateOnUpdate);
}
+void ClangdServer::findImplementations(
+ PathRef File, Position Pos, Callback<std::vector<LocatedSymbol>> CB) {
+ auto Action = [Pos, CB = std::move(CB),
+ this](llvm::Expected<InputsAndAST> InpAST) mutable {
+ if (!InpAST)
+ return CB(InpAST.takeError());
+ CB(clangd::findImplementations(InpAST->AST, Pos, Index));
+ };
+
+ WorkScheduler.runWithAST("Implementations", File, std::move(Action));
+}
+
void ClangdServer::findReferences(PathRef File, Position Pos, uint32_t Limit,
Callback<ReferencesResult> CB) {
auto Action = [Pos, Limit, CB = std::move(CB),
diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h
index 0056f5072cca3..1ccb4c5899f81 100644
--- a/clang-tools-extra/clangd/ClangdServer.h
+++ b/clang-tools-extra/clangd/ClangdServer.h
@@ -253,6 +253,10 @@ class ClangdServer {
/// Retrieve ranges that can be used to fold code within the specified file.
void foldingRanges(StringRef File, Callback<std::vector<FoldingRange>> CB);
+ /// Retrieve implementations for virtual method.
+ void findImplementations(PathRef File, Position Pos,
+ Callback<std::vector<LocatedSymbol>> CB);
+
/// Retrieve locations for symbol references.
void findReferences(PathRef File, Position Pos, uint32_t Limit,
Callback<ReferencesResult> CB);
diff --git a/clang-tools-extra/clangd/test/implementations.test b/clang-tools-extra/clangd/test/implementations.test
new file mode 100644
index 0000000000000..cc20d90bab775
--- /dev/null
+++ b/clang-tools-extra/clangd/test/implementations.test
@@ -0,0 +1,39 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}
+---
+{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///main.cpp","languageId":"cpp","version":1,"text":"struct Parent { virtual void Foo(); };\nstruct Child1 : Parent { void Foo() override(); };\nstruct Child2 : Parent { void Foo() override(); };"}}}
+---
+{"jsonrpc":"2.0","id":1,"method":"textDocument/implementation","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":0,"character":32}}}
+# CHECK: "id": 1
+# CHECK-NEXT: "jsonrpc": "2.0",
+# CHECK-NEXT: "result": [
+# CHECK-NEXT: {
+# CHECK-NEXT: "range": {
+# CHECK-NEXT: "end": {
+# CHECK-NEXT: "character": 33,
+# CHECK-NEXT: "line": 1
+# CHECK-NEXT: },
+# CHECK-NEXT: "start": {
+# CHECK-NEXT: "character": 30,
+# CHECK-NEXT: "line": 1
+# CHECK-NEXT: }
+# CHECK-NEXT: },
+# CHECK-NEXT: "uri": "file://{{.*}}/clangd-test/main.cpp"
+# CHECK-NEXT: },
+# CHECK-NEXT: {
+# CHECK-NEXT: "range": {
+# CHECK-NEXT: "end": {
+# CHECK-NEXT: "character": 33,
+# CHECK-NEXT: "line": 2
+# CHECK-NEXT: },
+# CHECK-NEXT: "start": {
+# CHECK-NEXT: "character": 30,
+# CHECK-NEXT: "line": 2
+# CHECK-NEXT: }
+# CHECK-NEXT: },
+# CHECK-NEXT: "uri": "file://{{.*}}/clangd-test/main.cpp"
+# CHECK-NEXT: }
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
diff --git a/clang-tools-extra/clangd/test/initialize-params.test b/clang-tools-extra/clangd/test/initialize-params.test
index a08a9b14a317d..dbfe7d006c726 100644
--- a/clang-tools-extra/clangd/test/initialize-params.test
+++ b/clang-tools-extra/clangd/test/initialize-params.test
@@ -67,7 +67,8 @@
# CHECK-NEXT: ]
# CHECK-NEXT: },
# CHECK-NEXT: "hoverProvider": true,
-# CHECK-NEXT: "memoryUsageProvider": true
+# CHECK-NEXT: "implementationProvider": true,
+# CHECK-NEXT: "memoryUsageProvider": true,
# CHECK-NEXT: "referencesProvider": true,
# CHECK-NEXT: "renameProvider": true,
# CHECK-NEXT: "selectionRangeProvider": true,
More information about the cfe-commits
mailing list