blob: 5a812a6b336a90755b8daed23d03bcf2a154fb53 [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{
Sam McCall0930ab02017-11-07 15:49:35 +000039 {{"capabilities",
40 json::obj{
41 {"textDocumentSync", 1},
42 {"documentFormattingProvider", true},
43 {"documentRangeFormattingProvider", true},
44 {"documentOnTypeFormattingProvider",
45 json::obj{
46 {"firstTriggerCharacter", "}"},
47 {"moreTriggerCharacter", {}},
48 }},
49 {"codeActionProvider", true},
50 {"completionProvider",
51 json::obj{
52 {"resolveProvider", false},
53 {"triggerCharacters", {".", ">", ":"}},
54 }},
55 {"signatureHelpProvider",
56 json::obj{
57 {"triggerCharacters", {"(", ","}},
58 }},
59 {"definitionProvider", true},
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +000060 {"documentHighlightProvider", true},
Haojian Wu345099c2017-11-09 11:30:04 +000061 {"renameProvider", true},
Sam McCall0930ab02017-11-07 15:49:35 +000062 {"executeCommandProvider",
63 json::obj{
64 {"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
65 }},
66 }}}});
Sam McCall8a5dded2017-10-12 13:29:58 +000067 if (Params.rootUri && !Params.rootUri->file.empty())
68 Server.setRootPath(Params.rootUri->file);
69 else if (Params.rootPath && !Params.rootPath->empty())
70 Server.setRootPath(*Params.rootPath);
Ilya Biryukovafb55542017-05-16 14:40:30 +000071}
72
Sam McCall8a5dded2017-10-12 13:29:58 +000073void ClangdLSPServer::onShutdown(Ctx C, ShutdownParams &Params) {
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000074 // Do essentially nothing, just say we're ready to exit.
75 ShutdownRequestReceived = true;
Sam McCalldd0566b2017-11-06 15:40:30 +000076 C.reply(nullptr);
Sam McCall8a5dded2017-10-12 13:29:58 +000077}
Ilya Biryukovafb55542017-05-16 14:40:30 +000078
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000079void ClangdLSPServer::onExit(Ctx C, ExitParams &Params) { IsDone = true; }
80
Sam McCall8a5dded2017-10-12 13:29:58 +000081void ClangdLSPServer::onDocumentDidOpen(Ctx C,
82 DidOpenTextDocumentParams &Params) {
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000083 if (Params.metadata && !Params.metadata->extraFlags.empty())
Sam McCall4db732a2017-09-30 10:08:52 +000084 CDB.setExtraFlagsForFile(Params.textDocument.uri.file,
85 std::move(Params.metadata->extraFlags));
86 Server.addDocument(Params.textDocument.uri.file, Params.textDocument.text);
Ilya Biryukovafb55542017-05-16 14:40:30 +000087}
88
Sam McCall8a5dded2017-10-12 13:29:58 +000089void ClangdLSPServer::onDocumentDidChange(Ctx C,
90 DidChangeTextDocumentParams &Params) {
Benjamin Kramerb560a9a2017-10-26 10:36:20 +000091 if (Params.contentChanges.size() != 1)
Haojian Wu2375c922017-11-07 10:21:02 +000092 return C.replyError(ErrorCode::InvalidParams,
93 "can only apply one change at a time");
Ilya Biryukovafb55542017-05-16 14:40:30 +000094 // We only support full syncing right now.
Sam McCall4db732a2017-09-30 10:08:52 +000095 Server.addDocument(Params.textDocument.uri.file,
96 Params.contentChanges[0].text);
Ilya Biryukovafb55542017-05-16 14:40:30 +000097}
98
Sam McCall8a5dded2017-10-12 13:29:58 +000099void ClangdLSPServer::onFileEvent(Ctx C, DidChangeWatchedFilesParams &Params) {
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000100 Server.onFileEvent(Params);
101}
102
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000103void ClangdLSPServer::onCommand(Ctx C, ExecuteCommandParams &Params) {
104 if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND &&
105 Params.workspaceEdit) {
106 // The flow for "apply-fix" :
107 // 1. We publish a diagnostic, including fixits
108 // 2. The user clicks on the diagnostic, the editor asks us for code actions
109 // 3. We send code actions, with the fixit embedded as context
110 // 4. The user selects the fixit, the editor asks us to apply it
111 // 5. We unwrap the changes and send them back to the editor
112 // 6. The editor applies the changes (applyEdit), and sends us a reply (but
113 // we ignore it)
114
115 ApplyWorkspaceEditParams ApplyEdit;
116 ApplyEdit.edit = *Params.workspaceEdit;
Sam McCalldd0566b2017-11-06 15:40:30 +0000117 C.reply("Fix applied.");
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000118 // We don't need the response so id == 1 is OK.
119 // Ideally, we would wait for the response and if there is no error, we
120 // would reply success/failure to the original RPC.
Sam McCallff8b8742017-11-30 21:32:29 +0000121 C.call("workspace/applyEdit", ApplyEdit);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000122 } else {
123 // We should not get here because ExecuteCommandParams would not have
124 // parsed in the first place and this handler should not be called. But if
125 // more commands are added, this will be here has a safe guard.
126 C.replyError(
Haojian Wu2375c922017-11-07 10:21:02 +0000127 ErrorCode::InvalidParams,
128 llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000129 }
130}
131
Haojian Wu345099c2017-11-09 11:30:04 +0000132void ClangdLSPServer::onRename(Ctx C, RenameParams &Params) {
133 auto File = Params.textDocument.uri.file;
134 auto Replacements = Server.rename(File, Params.position, Params.newName);
135 if (!Replacements) {
Ilya Biryukov9e11c4c2017-11-15 18:04:56 +0000136 C.replyError(ErrorCode::InternalError,
137 llvm::toString(Replacements.takeError()));
Haojian Wu345099c2017-11-09 11:30:04 +0000138 return;
139 }
140 std::string Code = Server.getDocument(File);
141 std::vector<TextEdit> Edits = replacementsToEdits(Code, *Replacements);
142 WorkspaceEdit WE;
Sam McCallec109022017-11-28 09:37:43 +0000143 WE.changes = {{Params.textDocument.uri.uri, Edits}};
Sam McCallff8b8742017-11-30 21:32:29 +0000144 C.reply(WE);
Haojian Wu345099c2017-11-09 11:30:04 +0000145}
146
Sam McCall8a5dded2017-10-12 13:29:58 +0000147void ClangdLSPServer::onDocumentDidClose(Ctx C,
148 DidCloseTextDocumentParams &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000149 Server.removeDocument(Params.textDocument.uri.file);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000150}
151
Sam McCall4db732a2017-09-30 10:08:52 +0000152void ClangdLSPServer::onDocumentOnTypeFormatting(
Sam McCall8a5dded2017-10-12 13:29:58 +0000153 Ctx C, DocumentOnTypeFormattingParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000154 auto File = Params.textDocument.uri.file;
Sam McCall4db732a2017-09-30 10:08:52 +0000155 std::string Code = Server.getDocument(File);
Sam McCalldd0566b2017-11-06 15:40:30 +0000156 C.reply(json::ary(
157 replacementsToEdits(Code, Server.formatOnType(File, Params.position))));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000158}
159
Sam McCall4db732a2017-09-30 10:08:52 +0000160void ClangdLSPServer::onDocumentRangeFormatting(
Sam McCall8a5dded2017-10-12 13:29:58 +0000161 Ctx C, DocumentRangeFormattingParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000162 auto File = Params.textDocument.uri.file;
Sam McCall4db732a2017-09-30 10:08:52 +0000163 std::string Code = Server.getDocument(File);
Sam McCalldd0566b2017-11-06 15:40:30 +0000164 C.reply(json::ary(
165 replacementsToEdits(Code, Server.formatRange(File, Params.range))));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000166}
167
Sam McCall8a5dded2017-10-12 13:29:58 +0000168void ClangdLSPServer::onDocumentFormatting(Ctx C,
169 DocumentFormattingParams &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000170 auto File = Params.textDocument.uri.file;
171 std::string Code = Server.getDocument(File);
Sam McCalldd0566b2017-11-06 15:40:30 +0000172 C.reply(json::ary(replacementsToEdits(Code, Server.formatFile(File))));
Sam McCall4db732a2017-09-30 10:08:52 +0000173}
174
Sam McCall8a5dded2017-10-12 13:29:58 +0000175void ClangdLSPServer::onCodeAction(Ctx C, CodeActionParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000176 // We provide a code action for each diagnostic at the requested location
177 // which has FixIts available.
Sam McCall4db732a2017-09-30 10:08:52 +0000178 std::string Code = Server.getDocument(Params.textDocument.uri.file);
Sam McCalldd0566b2017-11-06 15:40:30 +0000179 json::ary Commands;
Ilya Biryukovafb55542017-05-16 14:40:30 +0000180 for (Diagnostic &D : Params.context.diagnostics) {
181 std::vector<clang::tooling::Replacement> Fixes =
Sam McCall4db732a2017-09-30 10:08:52 +0000182 getFixIts(Params.textDocument.uri.file, D);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000183 auto Edits = replacementsToEdits(Code, Fixes);
Sam McCalldd0566b2017-11-06 15:40:30 +0000184 if (!Edits.empty()) {
185 WorkspaceEdit WE;
186 WE.changes = {{Params.textDocument.uri.uri, std::move(Edits)}};
187 Commands.push_back(json::obj{
188 {"title", llvm::formatv("Apply FixIt {0}", D.message)},
189 {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND},
190 {"arguments", {WE}},
191 });
192 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000193 }
Sam McCalldd0566b2017-11-06 15:40:30 +0000194 C.reply(std::move(Commands));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000195}
196
Sam McCall8a5dded2017-10-12 13:29:58 +0000197void ClangdLSPServer::onCompletion(Ctx C, TextDocumentPositionParams &Params) {
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000198 auto List =
199 Server
200 .codeComplete(
201 Params.textDocument.uri.file,
202 Position{Params.position.line, Params.position.character}, CCOpts)
203 .get() // FIXME(ibiryukov): This could be made async if we
204 // had an API that would allow to attach callbacks to
205 // futures returned by ClangdServer.
206 .Value;
Sam McCalla40371b2017-11-15 09:16:29 +0000207 C.reply(List);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000208}
209
Sam McCall8a5dded2017-10-12 13:29:58 +0000210void ClangdLSPServer::onSignatureHelp(Ctx C,
211 TextDocumentPositionParams &Params) {
Benjamin Krameree19f162017-10-26 12:28:13 +0000212 auto SignatureHelp = Server.signatureHelp(
213 Params.textDocument.uri.file,
214 Position{Params.position.line, Params.position.character});
215 if (!SignatureHelp)
Haojian Wu2375c922017-11-07 10:21:02 +0000216 return C.replyError(ErrorCode::InvalidParams,
217 llvm::toString(SignatureHelp.takeError()));
Sam McCalldd0566b2017-11-06 15:40:30 +0000218 C.reply(SignatureHelp->Value);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000219}
220
Sam McCall8a5dded2017-10-12 13:29:58 +0000221void ClangdLSPServer::onGoToDefinition(Ctx C,
222 TextDocumentPositionParams &Params) {
Benjamin Krameree19f162017-10-26 12:28:13 +0000223 auto Items = Server.findDefinitions(
224 Params.textDocument.uri.file,
225 Position{Params.position.line, Params.position.character});
226 if (!Items)
Haojian Wu2375c922017-11-07 10:21:02 +0000227 return C.replyError(ErrorCode::InvalidParams,
228 llvm::toString(Items.takeError()));
Sam McCalldd0566b2017-11-06 15:40:30 +0000229 C.reply(json::ary(Items->Value));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000230}
231
Sam McCall8a5dded2017-10-12 13:29:58 +0000232void ClangdLSPServer::onSwitchSourceHeader(Ctx C,
233 TextDocumentIdentifier &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000234 llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file);
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000235 std::string ResultUri;
Sam McCalldd0566b2017-11-06 15:40:30 +0000236 C.reply(Result ? URI::fromFile(*Result).uri : "");
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000237}
238
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000239void ClangdLSPServer::onDocumentHighlight(Ctx C,
240 TextDocumentPositionParams &Params) {
241
242 auto Highlights = Server.findDocumentHighlights(
243 Params.textDocument.uri.file,
244 Position{Params.position.line, Params.position.character});
245
246 if (!Highlights) {
247 C.replyError(ErrorCode::InternalError,
248 llvm::toString(Highlights.takeError()));
249 return;
250 }
251
252 C.reply(json::ary(Highlights->Value));
253}
254
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000255ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000256 bool StorePreamblesInMemory,
Sam McCalladccab62017-11-23 16:58:22 +0000257 const clangd::CodeCompleteOptions &CCOpts,
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000258 llvm::Optional<StringRef> ResourceDir,
259 llvm::Optional<Path> CompileCommandsDir)
260 : Out(Out), CDB(/*Logger=*/Out, std::move(CompileCommandsDir)),
Ilya Biryukovd3b04e32017-12-05 10:42:57 +0000261 CCOpts(CCOpts), Server(CDB, /*DiagConsumer=*/*this, FSProvider,
262 AsyncThreadsCount, StorePreamblesInMemory,
263 /*Logger=*/Out, ResourceDir) {}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000264
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000265bool ClangdLSPServer::run(std::istream &In) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000266 assert(!IsDone && "Run was called before");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000267
Ilya Biryukovafb55542017-05-16 14:40:30 +0000268 // Set up JSONRPCDispatcher.
Sam McCall8a5dded2017-10-12 13:29:58 +0000269 JSONRPCDispatcher Dispatcher(
Sam McCallec109022017-11-28 09:37:43 +0000270 [](RequestContext Ctx, const json::Expr &Params) {
Haojian Wu2375c922017-11-07 10:21:02 +0000271 Ctx.replyError(ErrorCode::MethodNotFound, "method not found");
Sam McCall8a5dded2017-10-12 13:29:58 +0000272 });
Sam McCall4db732a2017-09-30 10:08:52 +0000273 registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000274
Ilya Biryukovafb55542017-05-16 14:40:30 +0000275 // Run the Language Server loop.
276 runLanguageServerLoop(In, Out, Dispatcher, IsDone);
277
278 // Make sure IsDone is set to true after this method exits to ensure assertion
279 // at the start of the method fires if it's ever executed again.
280 IsDone = true;
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000281
282 return ShutdownRequestReceived;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000283}
284
285std::vector<clang::tooling::Replacement>
286ClangdLSPServer::getFixIts(StringRef File, const clangd::Diagnostic &D) {
287 std::lock_guard<std::mutex> Lock(FixItsMutex);
288 auto DiagToFixItsIter = FixItsMap.find(File);
289 if (DiagToFixItsIter == FixItsMap.end())
290 return {};
291
292 const auto &DiagToFixItsMap = DiagToFixItsIter->second;
293 auto FixItsIter = DiagToFixItsMap.find(D);
294 if (FixItsIter == DiagToFixItsMap.end())
295 return {};
296
297 return FixItsIter->second;
298}
299
Sam McCall4db732a2017-09-30 10:08:52 +0000300void ClangdLSPServer::onDiagnosticsReady(
301 PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000302 json::ary DiagnosticsJSON;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000303
304 DiagnosticToReplacementMap LocalFixIts; // Temporary storage
Sam McCall4db732a2017-09-30 10:08:52 +0000305 for (auto &DiagWithFixes : Diagnostics.Value) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000306 auto Diag = DiagWithFixes.Diag;
Sam McCalldd0566b2017-11-06 15:40:30 +0000307 DiagnosticsJSON.push_back(json::obj{
308 {"range", Diag.range},
309 {"severity", Diag.severity},
310 {"message", Diag.message},
311 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000312 // We convert to Replacements to become independent of the SourceManager.
313 auto &FixItsForDiagnostic = LocalFixIts[Diag];
314 std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(),
315 std::back_inserter(FixItsForDiagnostic));
316 }
317
318 // Cache FixIts
319 {
320 // FIXME(ibiryukov): should be deleted when documents are removed
321 std::lock_guard<std::mutex> Lock(FixItsMutex);
322 FixItsMap[File] = LocalFixIts;
323 }
324
325 // Publish diagnostics.
Sam McCalldd0566b2017-11-06 15:40:30 +0000326 Out.writeMessage(json::obj{
327 {"jsonrpc", "2.0"},
328 {"method", "textDocument/publishDiagnostics"},
329 {"params",
330 json::obj{
331 {"uri", URI::fromFile(File)},
332 {"diagnostics", std::move(DiagnosticsJSON)},
333 }},
334 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000335}