[clang-tools-extra] r341462 - [clangd] Add xrefs LSP boilerplate implementation.
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 5 04:53:07 PDT 2018
Author: sammccall
Date: Wed Sep 5 04:53:07 2018
New Revision: 341462
URL: http://llvm.org/viewvc/llvm-project?rev=341462&view=rev
Log:
[clangd] Add xrefs LSP boilerplate implementation.
Reviewers: ilya-biryukov, ioeric
Subscribers: MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50896
Added:
clang-tools-extra/trunk/test/clangd/references.test
Modified:
clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
clang-tools-extra/trunk/clangd/ClangdLSPServer.h
clang-tools-extra/trunk/clangd/ClangdServer.cpp
clang-tools-extra/trunk/clangd/ClangdServer.h
clang-tools-extra/trunk/clangd/Protocol.cpp
clang-tools-extra/trunk/clangd/Protocol.h
clang-tools-extra/trunk/clangd/ProtocolHandlers.cpp
clang-tools-extra/trunk/clangd/ProtocolHandlers.h
clang-tools-extra/trunk/clangd/XRefs.cpp
clang-tools-extra/trunk/test/clangd/initialize-params-invalid.test
clang-tools-extra/trunk/test/clangd/initialize-params.test
Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Wed Sep 5 04:53:07 2018
@@ -129,6 +129,7 @@ void ClangdLSPServer::onInitialize(Initi
{"renameProvider", true},
{"documentSymbolProvider", true},
{"workspaceSymbolProvider", true},
+ {"referencesProvider", true},
{"executeCommandProvider",
json::Object{
{"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
@@ -449,6 +450,17 @@ void ClangdLSPServer::onChangeConfigurat
applyConfiguration(Params.settings);
}
+void ClangdLSPServer::onReference(ReferenceParams &Params) {
+ Server.findReferences(Params.textDocument.uri.file(), Params.position,
+ [](llvm::Expected<std::vector<Location>> Locations) {
+ if (!Locations)
+ return replyError(
+ ErrorCode::InternalError,
+ llvm::toString(Locations.takeError()));
+ reply(llvm::json::Array(*Locations));
+ });
+}
+
ClangdLSPServer::ClangdLSPServer(JSONOutput &Out,
const clangd::CodeCompleteOptions &CCOpts,
llvm::Optional<Path> CompileCommandsDir,
Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.h?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h Wed Sep 5 04:53:07 2018
@@ -67,6 +67,7 @@ private:
void onCompletion(TextDocumentPositionParams &Params) override;
void onSignatureHelp(TextDocumentPositionParams &Params) override;
void onGoToDefinition(TextDocumentPositionParams &Params) override;
+ void onReference(ReferenceParams &Params) override;
void onSwitchSourceHeader(TextDocumentIdentifier &Params) override;
void onDocumentHighlight(TextDocumentPositionParams &Params) override;
void onFileEvent(DidChangeWatchedFilesParams &Params) override;
Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Wed Sep 5 04:53:07 2018
@@ -560,6 +560,18 @@ void ClangdServer::documentSymbols(
Bind(Action, std::move(CB)));
}
+void ClangdServer::findReferences(PathRef File, Position Pos,
+ Callback<std::vector<Location>> CB) {
+ auto Action = [Pos, this](Callback<std::vector<Location>> CB,
+ llvm::Expected<InputsAndAST> InpAST) {
+ if (!InpAST)
+ return CB(InpAST.takeError());
+ CB(clangd::findReferences(InpAST->AST, Pos, Index));
+ };
+
+ WorkScheduler.runWithAST("References", File, Bind(Action, std::move(CB)));
+}
+
std::vector<std::pair<Path, std::size_t>>
ClangdServer::getUsedBytesPerFile() const {
return WorkScheduler.getUsedBytesPerFile();
Modified: clang-tools-extra/trunk/clangd/ClangdServer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.h Wed Sep 5 04:53:07 2018
@@ -157,6 +157,10 @@ public:
void documentSymbols(StringRef File,
Callback<std::vector<SymbolInformation>> CB);
+ /// Retrieve locations for symbol references.
+ void findReferences(PathRef File, Position Pos,
+ Callback<std::vector<Location>> CB);
+
/// Run formatting for \p Rng inside \p File with content \p Code.
llvm::Expected<tooling::Replacements> formatRange(StringRef Code,
PathRef File, Range Rng);
Modified: clang-tools-extra/trunk/clangd/Protocol.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Protocol.cpp?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Protocol.cpp (original)
+++ clang-tools-extra/trunk/clangd/Protocol.cpp Wed Sep 5 04:53:07 2018
@@ -616,6 +616,11 @@ bool fromJSON(const json::Value &Params,
O.map("compilationDatabaseChanges", CCPC.compilationDatabaseChanges);
}
+bool fromJSON(const json::Value &Params, ReferenceParams &R) {
+ TextDocumentPositionParams &Base = R;
+ return fromJSON(Params, Base);
+}
+
json::Value toJSON(const CancelParams &CP) {
return json::Object{{"id", CP.ID}};
}
Modified: clang-tools-extra/trunk/clangd/Protocol.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Protocol.h?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Protocol.h (original)
+++ clang-tools-extra/trunk/clangd/Protocol.h Wed Sep 5 04:53:07 2018
@@ -878,6 +878,11 @@ struct DocumentHighlight {
llvm::json::Value toJSON(const DocumentHighlight &DH);
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const DocumentHighlight &);
+struct ReferenceParams : public TextDocumentPositionParams {
+ // For now, no options like context.includeDeclaration are supported.
+};
+bool fromJSON(const llvm::json::Value &, ReferenceParams &);
+
struct CancelParams {
/// The request id to cancel.
/// This can be either a number or string, if it is a number simply print it
Modified: clang-tools-extra/trunk/clangd/ProtocolHandlers.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ProtocolHandlers.cpp?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ProtocolHandlers.cpp (original)
+++ clang-tools-extra/trunk/clangd/ProtocolHandlers.cpp Wed Sep 5 04:53:07 2018
@@ -63,6 +63,7 @@ void clangd::registerCallbackHandlers(JS
Register("textDocument/completion", &ProtocolCallbacks::onCompletion);
Register("textDocument/signatureHelp", &ProtocolCallbacks::onSignatureHelp);
Register("textDocument/definition", &ProtocolCallbacks::onGoToDefinition);
+ Register("textDocument/references", &ProtocolCallbacks::onReference);
Register("textDocument/switchSourceHeader",
&ProtocolCallbacks::onSwitchSourceHeader);
Register("textDocument/rename", &ProtocolCallbacks::onRename);
Modified: clang-tools-extra/trunk/clangd/ProtocolHandlers.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ProtocolHandlers.h?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ProtocolHandlers.h (original)
+++ clang-tools-extra/trunk/clangd/ProtocolHandlers.h Wed Sep 5 04:53:07 2018
@@ -47,6 +47,7 @@ public:
virtual void onCompletion(TextDocumentPositionParams &Params) = 0;
virtual void onSignatureHelp(TextDocumentPositionParams &Params) = 0;
virtual void onGoToDefinition(TextDocumentPositionParams &Params) = 0;
+ virtual void onReference(ReferenceParams &Params) = 0;
virtual void onSwitchSourceHeader(TextDocumentIdentifier &Params) = 0;
virtual void onFileEvent(DidChangeWatchedFilesParams &Params) = 0;
virtual void onCommand(ExecuteCommandParams &Params) = 0;
Modified: clang-tools-extra/trunk/clangd/XRefs.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/XRefs.cpp?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/XRefs.cpp (original)
+++ clang-tools-extra/trunk/clangd/XRefs.cpp Wed Sep 5 04:53:07 2018
@@ -17,6 +17,7 @@
#include "clang/Index/IndexingAction.h"
#include "clang/Index/USRGeneration.h"
#include "llvm/Support/Path.h"
+
namespace clang {
namespace clangd {
using namespace llvm;
Modified: clang-tools-extra/trunk/test/clangd/initialize-params-invalid.test
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/initialize-params-invalid.test?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clangd/initialize-params-invalid.test (original)
+++ clang-tools-extra/trunk/test/clangd/initialize-params-invalid.test Wed Sep 5 04:53:07 2018
@@ -5,41 +5,7 @@
# CHECK-NEXT: "jsonrpc": "2.0",
# CHECK-NEXT: "result": {
# CHECK-NEXT: "capabilities": {
-# CHECK-NEXT: "codeActionProvider": true,
-# CHECK-NEXT: "completionProvider": {
-# CHECK-NEXT: "resolveProvider": false,
-# CHECK-NEXT: "triggerCharacters": [
-# CHECK-NEXT: ".",
-# CHECK-NEXT: ">",
-# CHECK-NEXT: ":"
-# CHECK-NEXT: ]
-# CHECK-NEXT: },
-# CHECK-NEXT: "definitionProvider": true,
-# CHECK-NEXT: "documentFormattingProvider": true,
-# CHECK-NEXT: "documentHighlightProvider": true,
-# CHECK-NEXT: "documentOnTypeFormattingProvider": {
-# CHECK-NEXT: "firstTriggerCharacter": "}",
-# CHECK-NEXT: "moreTriggerCharacter": []
-# CHECK-NEXT: },
-# CHECK-NEXT: "documentRangeFormattingProvider": true,
-# CHECK-NEXT: "documentSymbolProvider": true,
-# CHECK-NEXT: "executeCommandProvider": {
-# CHECK-NEXT: "commands": [
-# CHECK-NEXT: "clangd.applyFix"
-# CHECK-NEXT: ]
-# CHECK-NEXT: },
-# CHECK-NEXT: "hoverProvider": true,
-# CHECK-NEXT: "renameProvider": true,
-# CHECK-NEXT: "signatureHelpProvider": {
-# CHECK-NEXT: "triggerCharacters": [
-# CHECK-NEXT: "(",
-# CHECK-NEXT: ","
-# CHECK-NEXT: ]
-# CHECK-NEXT: },
-# CHECK-NEXT: "textDocumentSync": 2,
-# CHECK-NEXT: "workspaceSymbolProvider": true
-# CHECK-NEXT: }
-# CHECK-NEXT: }
+# ...
---
{"jsonrpc":"2.0","id":3,"method":"shutdown"}
---
Modified: clang-tools-extra/trunk/test/clangd/initialize-params.test
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/initialize-params.test?rev=341462&r1=341461&r2=341462&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clangd/initialize-params.test (original)
+++ clang-tools-extra/trunk/test/clangd/initialize-params.test Wed Sep 5 04:53:07 2018
@@ -29,6 +29,7 @@
# CHECK-NEXT: ]
# CHECK-NEXT: },
# CHECK-NEXT: "hoverProvider": true,
+# CHECK-NEXT: "referencesProvider": true,
# CHECK-NEXT: "renameProvider": true,
# CHECK-NEXT: "signatureHelpProvider": {
# CHECK-NEXT: "triggerCharacters": [
Added: clang-tools-extra/trunk/test/clangd/references.test
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/references.test?rev=341462&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clangd/references.test (added)
+++ clang-tools-extra/trunk/test/clangd/references.test Wed Sep 5 04:53:07 2018
@@ -0,0 +1,40 @@
+# 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":"int x; int y = x;"}}}
+---
+{"jsonrpc":"2.0","id":1,"method":"textDocument/references","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":0,"character":4}}}
+# CHECK: "id": 1
+# CHECK-NEXT: "jsonrpc": "2.0",
+# CHECK-NEXT: "result": [
+# CHECK-NEXT: {
+# CHECK-NEXT: "range": {
+# CHECK-NEXT: "end": {
+# CHECK-NEXT: "character": 5,
+# CHECK-NEXT: "line": 0
+# CHECK-NEXT: },
+# CHECK-NEXT: "start": {
+# CHECK-NEXT: "character": 4,
+# CHECK-NEXT: "line": 0
+# CHECK-NEXT: }
+# CHECK-NEXT: },
+# CHECK-NEXT: "uri": "test:///main.cpp"
+# CHECK-NEXT: },
+# CHECK-NEXT: {
+# CHECK-NEXT: "range": {
+# CHECK-NEXT: "end": {
+# CHECK-NEXT: "character": 16,
+# CHECK-NEXT: "line": 0
+# CHECK-NEXT: },
+# CHECK-NEXT: "start": {
+# CHECK-NEXT: "character": 15,
+# CHECK-NEXT: "line": 0
+# CHECK-NEXT: }
+# CHECK-NEXT: },
+# CHECK-NEXT: "uri": "test:///main.cpp"
+# CHECK-NEXT: },
+# CHECK-NEXT: ]
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
More information about the cfe-commits
mailing list