Skip to content

Commit

Permalink
[clangd] Add xrefs LSP boilerplate implementation.
Browse files Browse the repository at this point in the history
Reviewers: ilya-biryukov, ioeric

Subscribers: MaskRay, jkorous, arphaman, cfe-commits

Differential Revision: https://reviews.llvm.org/D50896

llvm-svn: 341462
  • Loading branch information
sam-mccall committed Sep 5, 2018
1 parent 837f93a commit 1ad142f
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 35 deletions.
12 changes: 12 additions & 0 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Expand Up @@ -129,6 +129,7 @@ void ClangdLSPServer::onInitialize(InitializeParams &Params) {
{"renameProvider", true},
{"documentSymbolProvider", true},
{"workspaceSymbolProvider", true},
{"referencesProvider", true},
{"executeCommandProvider",
json::Object{
{"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
Expand Down Expand Up @@ -449,6 +450,17 @@ void ClangdLSPServer::onChangeConfiguration(
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,
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/ClangdLSPServer.h
Expand Up @@ -67,6 +67,7 @@ class ClangdLSPServer : private DiagnosticsConsumer, private ProtocolCallbacks {
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;
Expand Down
12 changes: 12 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.cpp
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.h
Expand Up @@ -157,6 +157,10 @@ class ClangdServer {
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);
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clangd/Protocol.cpp
Expand Up @@ -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}};
}
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clangd/Protocol.h
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/ProtocolHandlers.cpp
Expand Up @@ -63,6 +63,7 @@ void clangd::registerCallbackHandlers(JSONRPCDispatcher &Dispatcher,
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);
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/ProtocolHandlers.h
Expand Up @@ -47,6 +47,7 @@ class ProtocolCallbacks {
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;
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/XRefs.cpp
Expand Up @@ -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;
Expand Down
36 changes: 1 addition & 35 deletions clang-tools-extra/test/clangd/initialize-params-invalid.test
Expand Up @@ -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"}
---
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/test/clangd/initialize-params.test
Expand Up @@ -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": [
Expand Down
40 changes: 40 additions & 0 deletions clang-tools-extra/test/clangd/references.test
@@ -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"}

0 comments on commit 1ad142f

Please sign in to comment.