blob: e21695185b9418e62490e0cb2b8aea8da5b722a7 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- ClangdLSPServer.cpp - LSP server ------------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===---------------------------------------------------------------------===//
9
10#include "ClangdLSPServer.h"
11#include "JSONRPCDispatcher.h"
12
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000013#include "llvm/Support/FormatVariadic.h"
14
Ilya Biryukov38d79772017-05-16 09:38:59 +000015using namespace clang::clangd;
16using namespace clang;
17
Ilya Biryukovafb55542017-05-16 14:40:30 +000018namespace {
19
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000020std::vector<TextEdit>
Ilya Biryukovafb55542017-05-16 14:40:30 +000021replacementsToEdits(StringRef Code,
22 const std::vector<tooling::Replacement> &Replacements) {
23 // Turn the replacements into the format specified by the Language Server
Sam McCalldd0566b2017-11-06 15:40:30 +000024 // Protocol. Fuse them into one big JSON array.
25 std::vector<TextEdit> Edits;
Ilya Biryukovafb55542017-05-16 14:40:30 +000026 for (auto &R : Replacements) {
27 Range ReplacementRange = {
28 offsetToPosition(Code, R.getOffset()),
29 offsetToPosition(Code, R.getOffset() + R.getLength())};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000030 Edits.push_back({ReplacementRange, R.getReplacementText()});
Ilya Biryukovafb55542017-05-16 14:40:30 +000031 }
Ilya Biryukovafb55542017-05-16 14:40:30 +000032 return Edits;
33}
34
35} // namespace
36
Sam McCall8a5dded2017-10-12 13:29:58 +000037void ClangdLSPServer::onInitialize(Ctx C, InitializeParams &Params) {
Sam McCalldd0566b2017-11-06 15:40:30 +000038 C.reply(json::obj{
39 {"textDocumentSync", 1},
40 {"documentFormattingProvider", true},
41 {"documentRangeFormattingProvider", true},
42 {"documentOnTypeFormattingProvider",
43 json::obj{
44 {"firstTriggerCharacter", "}"},
45 {"moreTriggerCharacter", {}},
46 }},
47 {"codeActionProvider", true},
48 {"completionProvider",
49 json::obj{
50 {"resolveProvider", false},
51 {"triggerCharacters", {".", ">", ":"}},
52 }},
53 {"signatureHelpProvider",
54 json::obj{
55 {"triggerCharacters", {"(", ","}},
56 }},
57 {"definitionProvider", true},
58 {"executeCommandProvider",
59 json::obj{
60 {"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
61 }},
62 });
Sam McCall8a5dded2017-10-12 13:29:58 +000063 if (Params.rootUri && !Params.rootUri->file.empty())
64 Server.setRootPath(Params.rootUri->file);
65 else if (Params.rootPath && !Params.rootPath->empty())
66 Server.setRootPath(*Params.rootPath);
Ilya Biryukovafb55542017-05-16 14:40:30 +000067}
68
Sam McCall8a5dded2017-10-12 13:29:58 +000069void ClangdLSPServer::onShutdown(Ctx C, ShutdownParams &Params) {
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000070 // Do essentially nothing, just say we're ready to exit.
71 ShutdownRequestReceived = true;
Sam McCalldd0566b2017-11-06 15:40:30 +000072 C.reply(nullptr);
Sam McCall8a5dded2017-10-12 13:29:58 +000073}
Ilya Biryukovafb55542017-05-16 14:40:30 +000074
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000075void ClangdLSPServer::onExit(Ctx C, ExitParams &Params) { IsDone = true; }
76
Sam McCall8a5dded2017-10-12 13:29:58 +000077void ClangdLSPServer::onDocumentDidOpen(Ctx C,
78 DidOpenTextDocumentParams &Params) {
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000079 if (Params.metadata && !Params.metadata->extraFlags.empty())
Sam McCall4db732a2017-09-30 10:08:52 +000080 CDB.setExtraFlagsForFile(Params.textDocument.uri.file,
81 std::move(Params.metadata->extraFlags));
82 Server.addDocument(Params.textDocument.uri.file, Params.textDocument.text);
Ilya Biryukovafb55542017-05-16 14:40:30 +000083}
84
Sam McCall8a5dded2017-10-12 13:29:58 +000085void ClangdLSPServer::onDocumentDidChange(Ctx C,
86 DidChangeTextDocumentParams &Params) {
Benjamin Kramerb560a9a2017-10-26 10:36:20 +000087 if (Params.contentChanges.size() != 1)
Haojian Wu2375c922017-11-07 10:21:02 +000088 return C.replyError(ErrorCode::InvalidParams,
89 "can only apply one change at a time");
Ilya Biryukovafb55542017-05-16 14:40:30 +000090 // We only support full syncing right now.
Sam McCall4db732a2017-09-30 10:08:52 +000091 Server.addDocument(Params.textDocument.uri.file,
92 Params.contentChanges[0].text);
Ilya Biryukovafb55542017-05-16 14:40:30 +000093}
94
Sam McCall8a5dded2017-10-12 13:29:58 +000095void ClangdLSPServer::onFileEvent(Ctx C, DidChangeWatchedFilesParams &Params) {
Marc-Andre Laperlebf114242017-10-02 18:00:37 +000096 Server.onFileEvent(Params);
97}
98
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000099void ClangdLSPServer::onCommand(Ctx C, ExecuteCommandParams &Params) {
100 if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND &&
101 Params.workspaceEdit) {
102 // The flow for "apply-fix" :
103 // 1. We publish a diagnostic, including fixits
104 // 2. The user clicks on the diagnostic, the editor asks us for code actions
105 // 3. We send code actions, with the fixit embedded as context
106 // 4. The user selects the fixit, the editor asks us to apply it
107 // 5. We unwrap the changes and send them back to the editor
108 // 6. The editor applies the changes (applyEdit), and sends us a reply (but
109 // we ignore it)
110
111 ApplyWorkspaceEditParams ApplyEdit;
112 ApplyEdit.edit = *Params.workspaceEdit;
Sam McCalldd0566b2017-11-06 15:40:30 +0000113 C.reply("Fix applied.");
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000114 // We don't need the response so id == 1 is OK.
115 // Ideally, we would wait for the response and if there is no error, we
116 // would reply success/failure to the original RPC.
117 C.call("workspace/applyEdit", ApplyWorkspaceEditParams::unparse(ApplyEdit));
118 } else {
119 // We should not get here because ExecuteCommandParams would not have
120 // parsed in the first place and this handler should not be called. But if
121 // more commands are added, this will be here has a safe guard.
122 C.replyError(
Haojian Wu2375c922017-11-07 10:21:02 +0000123 ErrorCode::InvalidParams,
124 llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000125 }
126}
127
Sam McCall8a5dded2017-10-12 13:29:58 +0000128void ClangdLSPServer::onDocumentDidClose(Ctx C,
129 DidCloseTextDocumentParams &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000130 Server.removeDocument(Params.textDocument.uri.file);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000131}
132
Sam McCall4db732a2017-09-30 10:08:52 +0000133void ClangdLSPServer::onDocumentOnTypeFormatting(
Sam McCall8a5dded2017-10-12 13:29:58 +0000134 Ctx C, DocumentOnTypeFormattingParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000135 auto File = Params.textDocument.uri.file;
Sam McCall4db732a2017-09-30 10:08:52 +0000136 std::string Code = Server.getDocument(File);
Sam McCalldd0566b2017-11-06 15:40:30 +0000137 C.reply(json::ary(
138 replacementsToEdits(Code, Server.formatOnType(File, Params.position))));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000139}
140
Sam McCall4db732a2017-09-30 10:08:52 +0000141void ClangdLSPServer::onDocumentRangeFormatting(
Sam McCall8a5dded2017-10-12 13:29:58 +0000142 Ctx C, DocumentRangeFormattingParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000143 auto File = Params.textDocument.uri.file;
Sam McCall4db732a2017-09-30 10:08:52 +0000144 std::string Code = Server.getDocument(File);
Sam McCalldd0566b2017-11-06 15:40:30 +0000145 C.reply(json::ary(
146 replacementsToEdits(Code, Server.formatRange(File, Params.range))));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000147}
148
Sam McCall8a5dded2017-10-12 13:29:58 +0000149void ClangdLSPServer::onDocumentFormatting(Ctx C,
150 DocumentFormattingParams &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000151 auto File = Params.textDocument.uri.file;
152 std::string Code = Server.getDocument(File);
Sam McCalldd0566b2017-11-06 15:40:30 +0000153 C.reply(json::ary(replacementsToEdits(Code, Server.formatFile(File))));
Sam McCall4db732a2017-09-30 10:08:52 +0000154}
155
Sam McCall8a5dded2017-10-12 13:29:58 +0000156void ClangdLSPServer::onCodeAction(Ctx C, CodeActionParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000157 // We provide a code action for each diagnostic at the requested location
158 // which has FixIts available.
Sam McCall4db732a2017-09-30 10:08:52 +0000159 std::string Code = Server.getDocument(Params.textDocument.uri.file);
Sam McCalldd0566b2017-11-06 15:40:30 +0000160 json::ary Commands;
Ilya Biryukovafb55542017-05-16 14:40:30 +0000161 for (Diagnostic &D : Params.context.diagnostics) {
162 std::vector<clang::tooling::Replacement> Fixes =
Sam McCall4db732a2017-09-30 10:08:52 +0000163 getFixIts(Params.textDocument.uri.file, D);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000164 auto Edits = replacementsToEdits(Code, Fixes);
Sam McCalldd0566b2017-11-06 15:40:30 +0000165 if (!Edits.empty()) {
166 WorkspaceEdit WE;
167 WE.changes = {{Params.textDocument.uri.uri, std::move(Edits)}};
168 Commands.push_back(json::obj{
169 {"title", llvm::formatv("Apply FixIt {0}", D.message)},
170 {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND},
171 {"arguments", {WE}},
172 });
173 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000174 }
Sam McCalldd0566b2017-11-06 15:40:30 +0000175 C.reply(std::move(Commands));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000176}
177
Sam McCall8a5dded2017-10-12 13:29:58 +0000178void ClangdLSPServer::onCompletion(Ctx C, TextDocumentPositionParams &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000179 auto Items = Server
Ilya Biryukov574b7532017-08-02 09:08:39 +0000180 .codeComplete(Params.textDocument.uri.file,
181 Position{Params.position.line,
182 Params.position.character})
Ilya Biryukovdcd21692017-10-05 17:04:13 +0000183 .get() // FIXME(ibiryukov): This could be made async if we
184 // had an API that would allow to attach callbacks to
185 // futures returned by ClangdServer.
Ilya Biryukov574b7532017-08-02 09:08:39 +0000186 .Value;
Sam McCalldd0566b2017-11-06 15:40:30 +0000187 C.reply(json::ary(Items));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000188}
189
Sam McCall8a5dded2017-10-12 13:29:58 +0000190void ClangdLSPServer::onSignatureHelp(Ctx C,
191 TextDocumentPositionParams &Params) {
Benjamin Krameree19f162017-10-26 12:28:13 +0000192 auto SignatureHelp = Server.signatureHelp(
193 Params.textDocument.uri.file,
194 Position{Params.position.line, Params.position.character});
195 if (!SignatureHelp)
Haojian Wu2375c922017-11-07 10:21:02 +0000196 return C.replyError(ErrorCode::InvalidParams,
197 llvm::toString(SignatureHelp.takeError()));
Sam McCalldd0566b2017-11-06 15:40:30 +0000198 C.reply(SignatureHelp->Value);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000199}
200
Sam McCall8a5dded2017-10-12 13:29:58 +0000201void ClangdLSPServer::onGoToDefinition(Ctx C,
202 TextDocumentPositionParams &Params) {
Benjamin Krameree19f162017-10-26 12:28:13 +0000203 auto Items = Server.findDefinitions(
204 Params.textDocument.uri.file,
205 Position{Params.position.line, Params.position.character});
206 if (!Items)
Haojian Wu2375c922017-11-07 10:21:02 +0000207 return C.replyError(ErrorCode::InvalidParams,
208 llvm::toString(Items.takeError()));
Sam McCalldd0566b2017-11-06 15:40:30 +0000209 C.reply(json::ary(Items->Value));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000210}
211
Sam McCall8a5dded2017-10-12 13:29:58 +0000212void ClangdLSPServer::onSwitchSourceHeader(Ctx C,
213 TextDocumentIdentifier &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000214 llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file);
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000215 std::string ResultUri;
Sam McCalldd0566b2017-11-06 15:40:30 +0000216 C.reply(Result ? URI::fromFile(*Result).uri : "");
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000217}
218
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000219ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000220 bool SnippetCompletions,
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000221 llvm::Optional<StringRef> ResourceDir,
222 llvm::Optional<Path> CompileCommandsDir)
223 : Out(Out), CDB(/*Logger=*/Out, std::move(CompileCommandsDir)),
Sam McCall4db732a2017-09-30 10:08:52 +0000224 Server(CDB, /*DiagConsumer=*/*this, FSProvider, AsyncThreadsCount,
Ilya Biryukovb080cb12017-10-23 14:46:48 +0000225 clangd::CodeCompleteOptions(
226 /*EnableSnippetsAndCodePatterns=*/SnippetCompletions),
227 /*Logger=*/Out, ResourceDir) {}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000228
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000229bool ClangdLSPServer::run(std::istream &In) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000230 assert(!IsDone && "Run was called before");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000231
Ilya Biryukovafb55542017-05-16 14:40:30 +0000232 // Set up JSONRPCDispatcher.
Sam McCall8a5dded2017-10-12 13:29:58 +0000233 JSONRPCDispatcher Dispatcher(
234 [](RequestContext Ctx, llvm::yaml::MappingNode *Params) {
Haojian Wu2375c922017-11-07 10:21:02 +0000235 Ctx.replyError(ErrorCode::MethodNotFound, "method not found");
Sam McCall8a5dded2017-10-12 13:29:58 +0000236 });
Sam McCall4db732a2017-09-30 10:08:52 +0000237 registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000238
Ilya Biryukovafb55542017-05-16 14:40:30 +0000239 // Run the Language Server loop.
240 runLanguageServerLoop(In, Out, Dispatcher, IsDone);
241
242 // Make sure IsDone is set to true after this method exits to ensure assertion
243 // at the start of the method fires if it's ever executed again.
244 IsDone = true;
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000245
246 return ShutdownRequestReceived;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000247}
248
249std::vector<clang::tooling::Replacement>
250ClangdLSPServer::getFixIts(StringRef File, const clangd::Diagnostic &D) {
251 std::lock_guard<std::mutex> Lock(FixItsMutex);
252 auto DiagToFixItsIter = FixItsMap.find(File);
253 if (DiagToFixItsIter == FixItsMap.end())
254 return {};
255
256 const auto &DiagToFixItsMap = DiagToFixItsIter->second;
257 auto FixItsIter = DiagToFixItsMap.find(D);
258 if (FixItsIter == DiagToFixItsMap.end())
259 return {};
260
261 return FixItsIter->second;
262}
263
Sam McCall4db732a2017-09-30 10:08:52 +0000264void ClangdLSPServer::onDiagnosticsReady(
265 PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000266 json::ary DiagnosticsJSON;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000267
268 DiagnosticToReplacementMap LocalFixIts; // Temporary storage
Sam McCall4db732a2017-09-30 10:08:52 +0000269 for (auto &DiagWithFixes : Diagnostics.Value) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000270 auto Diag = DiagWithFixes.Diag;
Sam McCalldd0566b2017-11-06 15:40:30 +0000271 DiagnosticsJSON.push_back(json::obj{
272 {"range", Diag.range},
273 {"severity", Diag.severity},
274 {"message", Diag.message},
275 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000276 // We convert to Replacements to become independent of the SourceManager.
277 auto &FixItsForDiagnostic = LocalFixIts[Diag];
278 std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(),
279 std::back_inserter(FixItsForDiagnostic));
280 }
281
282 // Cache FixIts
283 {
284 // FIXME(ibiryukov): should be deleted when documents are removed
285 std::lock_guard<std::mutex> Lock(FixItsMutex);
286 FixItsMap[File] = LocalFixIts;
287 }
288
289 // Publish diagnostics.
Sam McCalldd0566b2017-11-06 15:40:30 +0000290 Out.writeMessage(json::obj{
291 {"jsonrpc", "2.0"},
292 {"method", "textDocument/publishDiagnostics"},
293 {"params",
294 json::obj{
295 {"uri", URI::fromFile(File)},
296 {"diagnostics", std::move(DiagnosticsJSON)},
297 }},
298 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000299}