2017-05-16 17:38:59 +08:00
|
|
|
//===--- ClangdLSPServer.cpp - LSP server ------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangdLSPServer.h"
|
|
|
|
#include "JSONRPCDispatcher.h"
|
|
|
|
|
|
|
|
using namespace clang::clangd;
|
|
|
|
using namespace clang;
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string
|
|
|
|
replacementsToEdits(StringRef Code,
|
|
|
|
const std::vector<tooling::Replacement> &Replacements) {
|
|
|
|
// Turn the replacements into the format specified by the Language Server
|
|
|
|
// Protocol. Fuse them into one big JSON array.
|
|
|
|
std::string Edits;
|
|
|
|
for (auto &R : Replacements) {
|
|
|
|
Range ReplacementRange = {
|
|
|
|
offsetToPosition(Code, R.getOffset()),
|
|
|
|
offsetToPosition(Code, R.getOffset() + R.getLength())};
|
|
|
|
TextEdit TE = {ReplacementRange, R.getReplacementText()};
|
|
|
|
Edits += TextEdit::unparse(TE);
|
|
|
|
Edits += ',';
|
|
|
|
}
|
|
|
|
if (!Edits.empty())
|
|
|
|
Edits.pop_back();
|
|
|
|
|
|
|
|
return Edits;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onInitialize(Ctx C, InitializeParams &Params) {
|
|
|
|
C.reply(
|
|
|
|
R"({"capabilities":{
|
2017-05-16 22:40:30 +08:00
|
|
|
"textDocumentSync": 1,
|
|
|
|
"documentFormattingProvider": true,
|
|
|
|
"documentRangeFormattingProvider": true,
|
|
|
|
"documentOnTypeFormattingProvider": {"firstTriggerCharacter":"}","moreTriggerCharacter":[]},
|
|
|
|
"codeActionProvider": true,
|
2017-07-31 17:27:52 +08:00
|
|
|
"completionProvider": {"resolveProvider": false, "triggerCharacters": [".",">",":"]},
|
2017-10-06 19:54:17 +08:00
|
|
|
"signatureHelpProvider": {"triggerCharacters": ["(",","]},
|
2017-06-29 00:12:10 +08:00
|
|
|
"definitionProvider": true
|
2017-10-12 21:29:58 +08:00
|
|
|
}})");
|
|
|
|
if (Params.rootUri && !Params.rootUri->file.empty())
|
|
|
|
Server.setRootPath(Params.rootUri->file);
|
|
|
|
else if (Params.rootPath && !Params.rootPath->empty())
|
|
|
|
Server.setRootPath(*Params.rootPath);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onShutdown(Ctx C, ShutdownParams &Params) {
|
2017-10-25 16:45:41 +08:00
|
|
|
// Do essentially nothing, just say we're ready to exit.
|
|
|
|
ShutdownRequestReceived = true;
|
|
|
|
C.reply("null");
|
2017-10-12 21:29:58 +08:00
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
2017-10-25 16:45:41 +08:00
|
|
|
void ClangdLSPServer::onExit(Ctx C, ExitParams &Params) { IsDone = true; }
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onDocumentDidOpen(Ctx C,
|
|
|
|
DidOpenTextDocumentParams &Params) {
|
2017-07-06 16:44:54 +08:00
|
|
|
if (Params.metadata && !Params.metadata->extraFlags.empty())
|
2017-09-30 18:08:52 +08:00
|
|
|
CDB.setExtraFlagsForFile(Params.textDocument.uri.file,
|
|
|
|
std::move(Params.metadata->extraFlags));
|
|
|
|
Server.addDocument(Params.textDocument.uri.file, Params.textDocument.text);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onDocumentDidChange(Ctx C,
|
|
|
|
DidChangeTextDocumentParams &Params) {
|
2017-05-16 22:40:30 +08:00
|
|
|
// We only support full syncing right now.
|
2017-09-30 18:08:52 +08:00
|
|
|
Server.addDocument(Params.textDocument.uri.file,
|
|
|
|
Params.contentChanges[0].text);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onFileEvent(Ctx C, DidChangeWatchedFilesParams &Params) {
|
2017-10-03 02:00:37 +08:00
|
|
|
Server.onFileEvent(Params);
|
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onDocumentDidClose(Ctx C,
|
|
|
|
DidCloseTextDocumentParams &Params) {
|
2017-09-30 18:08:52 +08:00
|
|
|
Server.removeDocument(Params.textDocument.uri.file);
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:08:52 +08:00
|
|
|
void ClangdLSPServer::onDocumentOnTypeFormatting(
|
2017-10-12 21:29:58 +08:00
|
|
|
Ctx C, DocumentOnTypeFormattingParams &Params) {
|
2017-05-16 22:40:30 +08:00
|
|
|
auto File = Params.textDocument.uri.file;
|
2017-09-30 18:08:52 +08:00
|
|
|
std::string Code = Server.getDocument(File);
|
|
|
|
std::string Edits =
|
|
|
|
replacementsToEdits(Code, Server.formatOnType(File, Params.position));
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply("[" + Edits + "]");
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:08:52 +08:00
|
|
|
void ClangdLSPServer::onDocumentRangeFormatting(
|
2017-10-12 21:29:58 +08:00
|
|
|
Ctx C, DocumentRangeFormattingParams &Params) {
|
2017-05-16 22:40:30 +08:00
|
|
|
auto File = Params.textDocument.uri.file;
|
2017-09-30 18:08:52 +08:00
|
|
|
std::string Code = Server.getDocument(File);
|
|
|
|
std::string Edits =
|
|
|
|
replacementsToEdits(Code, Server.formatRange(File, Params.range));
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply("[" + Edits + "]");
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onDocumentFormatting(Ctx C,
|
|
|
|
DocumentFormattingParams &Params) {
|
2017-05-16 22:40:30 +08:00
|
|
|
auto File = Params.textDocument.uri.file;
|
2017-09-30 18:08:52 +08:00
|
|
|
std::string Code = Server.getDocument(File);
|
|
|
|
std::string Edits = replacementsToEdits(Code, Server.formatFile(File));
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply("[" + Edits + "]");
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onCodeAction(Ctx C, CodeActionParams &Params) {
|
2017-05-16 22:40:30 +08:00
|
|
|
// We provide a code action for each diagnostic at the requested location
|
|
|
|
// which has FixIts available.
|
2017-09-30 18:08:52 +08:00
|
|
|
std::string Code = Server.getDocument(Params.textDocument.uri.file);
|
2017-05-16 22:40:30 +08:00
|
|
|
std::string Commands;
|
|
|
|
for (Diagnostic &D : Params.context.diagnostics) {
|
|
|
|
std::vector<clang::tooling::Replacement> Fixes =
|
2017-09-30 18:08:52 +08:00
|
|
|
getFixIts(Params.textDocument.uri.file, D);
|
2017-05-16 22:40:30 +08:00
|
|
|
std::string Edits = replacementsToEdits(Code, Fixes);
|
|
|
|
|
|
|
|
if (!Edits.empty())
|
|
|
|
Commands +=
|
|
|
|
R"({"title":"Apply FixIt ')" + llvm::yaml::escape(D.message) +
|
|
|
|
R"('", "command": "clangd.applyFix", "arguments": [")" +
|
|
|
|
llvm::yaml::escape(Params.textDocument.uri.uri) +
|
|
|
|
R"(", [)" + Edits +
|
|
|
|
R"(]]},)";
|
|
|
|
}
|
|
|
|
if (!Commands.empty())
|
|
|
|
Commands.pop_back();
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply("[" + Commands + "]");
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onCompletion(Ctx C, TextDocumentPositionParams &Params) {
|
2017-09-30 18:08:52 +08:00
|
|
|
auto Items = Server
|
2017-08-02 17:08:39 +08:00
|
|
|
.codeComplete(Params.textDocument.uri.file,
|
|
|
|
Position{Params.position.line,
|
|
|
|
Params.position.character})
|
2017-10-06 01:04:13 +08:00
|
|
|
.get() // FIXME(ibiryukov): This could be made async if we
|
|
|
|
// had an API that would allow to attach callbacks to
|
|
|
|
// futures returned by ClangdServer.
|
2017-08-02 17:08:39 +08:00
|
|
|
.Value;
|
2017-05-16 22:40:30 +08:00
|
|
|
|
|
|
|
std::string Completions;
|
|
|
|
for (const auto &Item : Items) {
|
|
|
|
Completions += CompletionItem::unparse(Item);
|
|
|
|
Completions += ",";
|
|
|
|
}
|
|
|
|
if (!Completions.empty())
|
|
|
|
Completions.pop_back();
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply("[" + Completions + "]");
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onSignatureHelp(Ctx C,
|
|
|
|
TextDocumentPositionParams &Params) {
|
|
|
|
C.reply(SignatureHelp::unparse(
|
2017-10-06 19:54:17 +08:00
|
|
|
Server
|
|
|
|
.signatureHelp(
|
|
|
|
Params.textDocument.uri.file,
|
|
|
|
Position{Params.position.line, Params.position.character})
|
2017-10-12 21:29:58 +08:00
|
|
|
.Value));
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onGoToDefinition(Ctx C,
|
|
|
|
TextDocumentPositionParams &Params) {
|
2017-09-30 18:08:52 +08:00
|
|
|
auto Items = Server
|
2017-08-02 17:08:39 +08:00
|
|
|
.findDefinitions(Params.textDocument.uri.file,
|
|
|
|
Position{Params.position.line,
|
|
|
|
Params.position.character})
|
|
|
|
.Value;
|
2017-06-29 00:12:10 +08:00
|
|
|
|
|
|
|
std::string Locations;
|
|
|
|
for (const auto &Item : Items) {
|
|
|
|
Locations += Location::unparse(Item);
|
|
|
|
Locations += ",";
|
|
|
|
}
|
|
|
|
if (!Locations.empty())
|
|
|
|
Locations.pop_back();
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply("[" + Locations + "]");
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void ClangdLSPServer::onSwitchSourceHeader(Ctx C,
|
|
|
|
TextDocumentIdentifier &Params) {
|
2017-09-30 18:08:52 +08:00
|
|
|
llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file);
|
2017-09-28 11:14:40 +08:00
|
|
|
std::string ResultUri;
|
2017-10-12 21:29:58 +08:00
|
|
|
C.reply(Result ? URI::unparse(URI::fromFile(*Result)) : R"("")");
|
2017-09-28 11:14:40 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
|
2017-09-12 21:57:14 +08:00
|
|
|
bool SnippetCompletions,
|
2017-10-02 23:13:20 +08:00
|
|
|
llvm::Optional<StringRef> ResourceDir,
|
|
|
|
llvm::Optional<Path> CompileCommandsDir)
|
|
|
|
: Out(Out), CDB(/*Logger=*/Out, std::move(CompileCommandsDir)),
|
2017-09-30 18:08:52 +08:00
|
|
|
Server(CDB, /*DiagConsumer=*/*this, FSProvider, AsyncThreadsCount,
|
2017-10-23 22:46:48 +08:00
|
|
|
clangd::CodeCompleteOptions(
|
|
|
|
/*EnableSnippetsAndCodePatterns=*/SnippetCompletions),
|
|
|
|
/*Logger=*/Out, ResourceDir) {}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-10-25 16:45:41 +08:00
|
|
|
bool ClangdLSPServer::run(std::istream &In) {
|
2017-05-16 22:40:30 +08:00
|
|
|
assert(!IsDone && "Run was called before");
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
// Set up JSONRPCDispatcher.
|
2017-10-12 21:29:58 +08:00
|
|
|
JSONRPCDispatcher Dispatcher(
|
|
|
|
[](RequestContext Ctx, llvm::yaml::MappingNode *Params) {
|
|
|
|
Ctx.replyError(-32601, "method not found");
|
|
|
|
});
|
2017-09-30 18:08:52 +08:00
|
|
|
registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this);
|
2017-05-16 22:40:30 +08:00
|
|
|
|
|
|
|
// Run the Language Server loop.
|
|
|
|
runLanguageServerLoop(In, Out, Dispatcher, IsDone);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
// Make sure IsDone is set to true after this method exits to ensure assertion
|
|
|
|
// at the start of the method fires if it's ever executed again.
|
|
|
|
IsDone = true;
|
2017-10-25 16:45:41 +08:00
|
|
|
|
|
|
|
return ShutdownRequestReceived;
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<clang::tooling::Replacement>
|
|
|
|
ClangdLSPServer::getFixIts(StringRef File, const clangd::Diagnostic &D) {
|
|
|
|
std::lock_guard<std::mutex> Lock(FixItsMutex);
|
|
|
|
auto DiagToFixItsIter = FixItsMap.find(File);
|
|
|
|
if (DiagToFixItsIter == FixItsMap.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto &DiagToFixItsMap = DiagToFixItsIter->second;
|
|
|
|
auto FixItsIter = DiagToFixItsMap.find(D);
|
|
|
|
if (FixItsIter == DiagToFixItsMap.end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return FixItsIter->second;
|
|
|
|
}
|
|
|
|
|
2017-09-30 18:08:52 +08:00
|
|
|
void ClangdLSPServer::onDiagnosticsReady(
|
|
|
|
PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) {
|
2017-05-16 17:38:59 +08:00
|
|
|
std::string DiagnosticsJSON;
|
|
|
|
|
|
|
|
DiagnosticToReplacementMap LocalFixIts; // Temporary storage
|
2017-09-30 18:08:52 +08:00
|
|
|
for (auto &DiagWithFixes : Diagnostics.Value) {
|
2017-05-16 17:38:59 +08:00
|
|
|
auto Diag = DiagWithFixes.Diag;
|
|
|
|
DiagnosticsJSON +=
|
|
|
|
R"({"range":)" + Range::unparse(Diag.range) +
|
|
|
|
R"(,"severity":)" + std::to_string(Diag.severity) +
|
|
|
|
R"(,"message":")" + llvm::yaml::escape(Diag.message) +
|
|
|
|
R"("},)";
|
|
|
|
|
|
|
|
// We convert to Replacements to become independent of the SourceManager.
|
|
|
|
auto &FixItsForDiagnostic = LocalFixIts[Diag];
|
|
|
|
std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(),
|
|
|
|
std::back_inserter(FixItsForDiagnostic));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache FixIts
|
|
|
|
{
|
|
|
|
// FIXME(ibiryukov): should be deleted when documents are removed
|
|
|
|
std::lock_guard<std::mutex> Lock(FixItsMutex);
|
|
|
|
FixItsMap[File] = LocalFixIts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish diagnostics.
|
|
|
|
if (!DiagnosticsJSON.empty())
|
|
|
|
DiagnosticsJSON.pop_back(); // Drop trailing comma.
|
|
|
|
Out.writeMessage(
|
|
|
|
R"({"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":")" +
|
|
|
|
URI::fromFile(File).uri + R"(","diagnostics":[)" + DiagnosticsJSON +
|
|
|
|
R"(]}})");
|
|
|
|
}
|