blob: b700b7f8ca3d94873f7b7cbb82ebff2833fbac5f [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"
Sam McCallb536a2a2017-12-19 12:23:48 +000012#include "SourceCode.h"
Eric Liu78ed91a72018-01-29 15:37:46 +000013#include "URI.h"
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000014#include "llvm/Support/FormatVariadic.h"
15
Ilya Biryukov38d79772017-05-16 09:38:59 +000016using namespace clang::clangd;
17using namespace clang;
18
Ilya Biryukovafb55542017-05-16 14:40:30 +000019namespace {
20
Raoul Wols212bcf82017-12-12 20:25:06 +000021TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R) {
22 Range ReplacementRange = {
23 offsetToPosition(Code, R.getOffset()),
24 offsetToPosition(Code, R.getOffset() + R.getLength())};
25 return {ReplacementRange, R.getReplacementText()};
26}
27
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000028std::vector<TextEdit>
Ilya Biryukovafb55542017-05-16 14:40:30 +000029replacementsToEdits(StringRef Code,
30 const std::vector<tooling::Replacement> &Replacements) {
31 // Turn the replacements into the format specified by the Language Server
Sam McCalldd0566b2017-11-06 15:40:30 +000032 // Protocol. Fuse them into one big JSON array.
33 std::vector<TextEdit> Edits;
Raoul Wols212bcf82017-12-12 20:25:06 +000034 for (const auto &R : Replacements)
35 Edits.push_back(replacementToEdit(Code, R));
36 return Edits;
37}
38
39std::vector<TextEdit> replacementsToEdits(StringRef Code,
40 const tooling::Replacements &Repls) {
41 std::vector<TextEdit> Edits;
42 for (const auto &R : Repls)
43 Edits.push_back(replacementToEdit(Code, R));
Ilya Biryukovafb55542017-05-16 14:40:30 +000044 return Edits;
45}
46
47} // namespace
48
Sam McCalld1a7a372018-01-31 13:40:48 +000049void ClangdLSPServer::onInitialize(InitializeParams &Params) {
50 reply(json::obj{
Sam McCall0930ab02017-11-07 15:49:35 +000051 {{"capabilities",
52 json::obj{
53 {"textDocumentSync", 1},
54 {"documentFormattingProvider", true},
55 {"documentRangeFormattingProvider", true},
56 {"documentOnTypeFormattingProvider",
57 json::obj{
58 {"firstTriggerCharacter", "}"},
59 {"moreTriggerCharacter", {}},
60 }},
61 {"codeActionProvider", true},
62 {"completionProvider",
63 json::obj{
64 {"resolveProvider", false},
65 {"triggerCharacters", {".", ">", ":"}},
66 }},
67 {"signatureHelpProvider",
68 json::obj{
69 {"triggerCharacters", {"(", ","}},
70 }},
71 {"definitionProvider", true},
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +000072 {"documentHighlightProvider", true},
Haojian Wu345099c2017-11-09 11:30:04 +000073 {"renameProvider", true},
Sam McCall0930ab02017-11-07 15:49:35 +000074 {"executeCommandProvider",
75 json::obj{
76 {"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}},
77 }},
78 }}}});
Sam McCall8a5dded2017-10-12 13:29:58 +000079 if (Params.rootUri && !Params.rootUri->file.empty())
80 Server.setRootPath(Params.rootUri->file);
81 else if (Params.rootPath && !Params.rootPath->empty())
82 Server.setRootPath(*Params.rootPath);
Ilya Biryukovafb55542017-05-16 14:40:30 +000083}
84
Sam McCalld1a7a372018-01-31 13:40:48 +000085void ClangdLSPServer::onShutdown(ShutdownParams &Params) {
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000086 // Do essentially nothing, just say we're ready to exit.
87 ShutdownRequestReceived = true;
Sam McCalld1a7a372018-01-31 13:40:48 +000088 reply(nullptr);
Sam McCall8a5dded2017-10-12 13:29:58 +000089}
Ilya Biryukovafb55542017-05-16 14:40:30 +000090
Sam McCalld1a7a372018-01-31 13:40:48 +000091void ClangdLSPServer::onExit(ExitParams &Params) { IsDone = true; }
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000092
Sam McCalld1a7a372018-01-31 13:40:48 +000093void ClangdLSPServer::onDocumentDidOpen(DidOpenTextDocumentParams &Params) {
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000094 if (Params.metadata && !Params.metadata->extraFlags.empty())
Sam McCall4db732a2017-09-30 10:08:52 +000095 CDB.setExtraFlagsForFile(Params.textDocument.uri.file,
96 std::move(Params.metadata->extraFlags));
Sam McCalld1a7a372018-01-31 13:40:48 +000097 Server.addDocument(Params.textDocument.uri.file, Params.textDocument.text);
Ilya Biryukovafb55542017-05-16 14:40:30 +000098}
99
Sam McCalld1a7a372018-01-31 13:40:48 +0000100void ClangdLSPServer::onDocumentDidChange(DidChangeTextDocumentParams &Params) {
Benjamin Kramerb560a9a2017-10-26 10:36:20 +0000101 if (Params.contentChanges.size() != 1)
Sam McCalld1a7a372018-01-31 13:40:48 +0000102 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000103 "can only apply one change at a time");
Ilya Biryukovafb55542017-05-16 14:40:30 +0000104 // We only support full syncing right now.
Sam McCalld1a7a372018-01-31 13:40:48 +0000105 Server.addDocument(Params.textDocument.uri.file,
Sam McCall4db732a2017-09-30 10:08:52 +0000106 Params.contentChanges[0].text);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000107}
108
Sam McCalld1a7a372018-01-31 13:40:48 +0000109void ClangdLSPServer::onFileEvent(DidChangeWatchedFilesParams &Params) {
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000110 Server.onFileEvent(Params);
111}
112
Sam McCalld1a7a372018-01-31 13:40:48 +0000113void ClangdLSPServer::onCommand(ExecuteCommandParams &Params) {
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000114 if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND &&
115 Params.workspaceEdit) {
116 // The flow for "apply-fix" :
117 // 1. We publish a diagnostic, including fixits
118 // 2. The user clicks on the diagnostic, the editor asks us for code actions
119 // 3. We send code actions, with the fixit embedded as context
120 // 4. The user selects the fixit, the editor asks us to apply it
121 // 5. We unwrap the changes and send them back to the editor
122 // 6. The editor applies the changes (applyEdit), and sends us a reply (but
123 // we ignore it)
124
125 ApplyWorkspaceEditParams ApplyEdit;
126 ApplyEdit.edit = *Params.workspaceEdit;
Sam McCalld1a7a372018-01-31 13:40:48 +0000127 reply("Fix applied.");
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000128 // We don't need the response so id == 1 is OK.
129 // Ideally, we would wait for the response and if there is no error, we
130 // would reply success/failure to the original RPC.
Sam McCalld1a7a372018-01-31 13:40:48 +0000131 call("workspace/applyEdit", ApplyEdit);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000132 } else {
133 // We should not get here because ExecuteCommandParams would not have
134 // parsed in the first place and this handler should not be called. But if
135 // more commands are added, this will be here has a safe guard.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000136 replyError(
Sam McCalld1a7a372018-01-31 13:40:48 +0000137 ErrorCode::InvalidParams,
Haojian Wu2375c922017-11-07 10:21:02 +0000138 llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000139 }
140}
141
Sam McCalld1a7a372018-01-31 13:40:48 +0000142void ClangdLSPServer::onRename(RenameParams &Params) {
Haojian Wu345099c2017-11-09 11:30:04 +0000143 auto File = Params.textDocument.uri.file;
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000144 auto Code = Server.getDocument(File);
145 if (!Code)
Sam McCalld1a7a372018-01-31 13:40:48 +0000146 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000147 "onRename called for non-added file");
148
Sam McCalld1a7a372018-01-31 13:40:48 +0000149 auto Replacements = Server.rename(File, Params.position, Params.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000150 if (!Replacements) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000151 replyError(ErrorCode::InternalError,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000152 llvm::toString(Replacements.takeError()));
Haojian Wu345099c2017-11-09 11:30:04 +0000153 return;
154 }
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000155
156 std::vector<TextEdit> Edits = replacementsToEdits(*Code, *Replacements);
Haojian Wu345099c2017-11-09 11:30:04 +0000157 WorkspaceEdit WE;
Eric Liu78ed91a72018-01-29 15:37:46 +0000158 WE.changes = {{Params.textDocument.uri.uri(), Edits}};
Sam McCalld1a7a372018-01-31 13:40:48 +0000159 reply(WE);
Haojian Wu345099c2017-11-09 11:30:04 +0000160}
161
Sam McCalld1a7a372018-01-31 13:40:48 +0000162void ClangdLSPServer::onDocumentDidClose(DidCloseTextDocumentParams &Params) {
163 Server.removeDocument(Params.textDocument.uri.file);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000164}
165
Sam McCall4db732a2017-09-30 10:08:52 +0000166void ClangdLSPServer::onDocumentOnTypeFormatting(
Sam McCalld1a7a372018-01-31 13:40:48 +0000167 DocumentOnTypeFormattingParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000168 auto File = Params.textDocument.uri.file;
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000169 auto Code = Server.getDocument(File);
170 if (!Code)
Sam McCalld1a7a372018-01-31 13:40:48 +0000171 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000172 "onDocumentOnTypeFormatting called for non-added file");
173
174 auto ReplacementsOrError = Server.formatOnType(*Code, File, Params.position);
Raoul Wols212bcf82017-12-12 20:25:06 +0000175 if (ReplacementsOrError)
Sam McCalld1a7a372018-01-31 13:40:48 +0000176 reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get())));
Raoul Wols212bcf82017-12-12 20:25:06 +0000177 else
Sam McCalld1a7a372018-01-31 13:40:48 +0000178 replyError(ErrorCode::UnknownErrorCode,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000179 llvm::toString(ReplacementsOrError.takeError()));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000180}
181
Sam McCall4db732a2017-09-30 10:08:52 +0000182void ClangdLSPServer::onDocumentRangeFormatting(
Sam McCalld1a7a372018-01-31 13:40:48 +0000183 DocumentRangeFormattingParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000184 auto File = Params.textDocument.uri.file;
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000185 auto Code = Server.getDocument(File);
186 if (!Code)
Sam McCalld1a7a372018-01-31 13:40:48 +0000187 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000188 "onDocumentRangeFormatting called for non-added file");
189
190 auto ReplacementsOrError = Server.formatRange(*Code, File, Params.range);
Raoul Wols212bcf82017-12-12 20:25:06 +0000191 if (ReplacementsOrError)
Sam McCalld1a7a372018-01-31 13:40:48 +0000192 reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get())));
Raoul Wols212bcf82017-12-12 20:25:06 +0000193 else
Sam McCalld1a7a372018-01-31 13:40:48 +0000194 replyError(ErrorCode::UnknownErrorCode,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000195 llvm::toString(ReplacementsOrError.takeError()));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000196}
197
Sam McCalld1a7a372018-01-31 13:40:48 +0000198void ClangdLSPServer::onDocumentFormatting(DocumentFormattingParams &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000199 auto File = Params.textDocument.uri.file;
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000200 auto Code = Server.getDocument(File);
201 if (!Code)
Sam McCalld1a7a372018-01-31 13:40:48 +0000202 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000203 "onDocumentFormatting called for non-added file");
204
205 auto ReplacementsOrError = Server.formatFile(*Code, File);
Raoul Wols212bcf82017-12-12 20:25:06 +0000206 if (ReplacementsOrError)
Sam McCalld1a7a372018-01-31 13:40:48 +0000207 reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get())));
Raoul Wols212bcf82017-12-12 20:25:06 +0000208 else
Sam McCalld1a7a372018-01-31 13:40:48 +0000209 replyError(ErrorCode::UnknownErrorCode,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000210 llvm::toString(ReplacementsOrError.takeError()));
Sam McCall4db732a2017-09-30 10:08:52 +0000211}
212
Sam McCalld1a7a372018-01-31 13:40:48 +0000213void ClangdLSPServer::onCodeAction(CodeActionParams &Params) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000214 // We provide a code action for each diagnostic at the requested location
215 // which has FixIts available.
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000216 auto Code = Server.getDocument(Params.textDocument.uri.file);
217 if (!Code)
Sam McCalld1a7a372018-01-31 13:40:48 +0000218 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov261c72e2018-01-17 12:30:24 +0000219 "onCodeAction called for non-added file");
220
Sam McCalldd0566b2017-11-06 15:40:30 +0000221 json::ary Commands;
Ilya Biryukovafb55542017-05-16 14:40:30 +0000222 for (Diagnostic &D : Params.context.diagnostics) {
Sam McCall8111d3b2017-12-13 08:48:42 +0000223 auto Edits = getFixIts(Params.textDocument.uri.file, D);
Sam McCalldd0566b2017-11-06 15:40:30 +0000224 if (!Edits.empty()) {
225 WorkspaceEdit WE;
Eric Liu78ed91a72018-01-29 15:37:46 +0000226 WE.changes = {{Params.textDocument.uri.uri(), std::move(Edits)}};
Sam McCalldd0566b2017-11-06 15:40:30 +0000227 Commands.push_back(json::obj{
228 {"title", llvm::formatv("Apply FixIt {0}", D.message)},
229 {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND},
230 {"arguments", {WE}},
231 });
232 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000233 }
Sam McCalld1a7a372018-01-31 13:40:48 +0000234 reply(std::move(Commands));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000235}
236
Sam McCalld1a7a372018-01-31 13:40:48 +0000237void ClangdLSPServer::onCompletion(TextDocumentPositionParams &Params) {
238 Server.codeComplete(Params.textDocument.uri.file,
239 Position{Params.position.line, Params.position.character},
240 CCOpts,
241 [](Tagged<CompletionList> List) { reply(List.Value); });
Ilya Biryukovafb55542017-05-16 14:40:30 +0000242}
243
Sam McCalld1a7a372018-01-31 13:40:48 +0000244void ClangdLSPServer::onSignatureHelp(TextDocumentPositionParams &Params) {
Benjamin Krameree19f162017-10-26 12:28:13 +0000245 auto SignatureHelp = Server.signatureHelp(
Sam McCalld1a7a372018-01-31 13:40:48 +0000246 Params.textDocument.uri.file,
Benjamin Krameree19f162017-10-26 12:28:13 +0000247 Position{Params.position.line, Params.position.character});
248 if (!SignatureHelp)
Sam McCalld1a7a372018-01-31 13:40:48 +0000249 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000250 llvm::toString(SignatureHelp.takeError()));
Sam McCalld1a7a372018-01-31 13:40:48 +0000251 reply(SignatureHelp->Value);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000252}
253
Sam McCalld1a7a372018-01-31 13:40:48 +0000254void ClangdLSPServer::onGoToDefinition(TextDocumentPositionParams &Params) {
Benjamin Krameree19f162017-10-26 12:28:13 +0000255 auto Items = Server.findDefinitions(
Sam McCalld1a7a372018-01-31 13:40:48 +0000256 Params.textDocument.uri.file,
Benjamin Krameree19f162017-10-26 12:28:13 +0000257 Position{Params.position.line, Params.position.character});
258 if (!Items)
Sam McCalld1a7a372018-01-31 13:40:48 +0000259 return replyError(ErrorCode::InvalidParams,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000260 llvm::toString(Items.takeError()));
Sam McCalld1a7a372018-01-31 13:40:48 +0000261 reply(json::ary(Items->Value));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000262}
263
Sam McCalld1a7a372018-01-31 13:40:48 +0000264void ClangdLSPServer::onSwitchSourceHeader(TextDocumentIdentifier &Params) {
Sam McCall4db732a2017-09-30 10:08:52 +0000265 llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file);
Sam McCalld1a7a372018-01-31 13:40:48 +0000266 reply(Result ? URI::createFile(*Result).toString() : "");
Marc-Andre Laperle6571b3e2017-09-28 03:14:40 +0000267}
268
Sam McCalld1a7a372018-01-31 13:40:48 +0000269void ClangdLSPServer::onDocumentHighlight(TextDocumentPositionParams &Params) {
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000270 auto Highlights = Server.findDocumentHighlights(
Sam McCalld1a7a372018-01-31 13:40:48 +0000271 Params.textDocument.uri.file,
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000272 Position{Params.position.line, Params.position.character});
273
274 if (!Highlights) {
Sam McCalld1a7a372018-01-31 13:40:48 +0000275 replyError(ErrorCode::InternalError,
Ilya Biryukov940901e2017-12-13 12:51:22 +0000276 llvm::toString(Highlights.takeError()));
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000277 return;
278 }
279
Sam McCalld1a7a372018-01-31 13:40:48 +0000280 reply(json::ary(Highlights->Value));
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000281}
282
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000283ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000284 bool StorePreamblesInMemory,
Sam McCalladccab62017-11-23 16:58:22 +0000285 const clangd::CodeCompleteOptions &CCOpts,
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000286 llvm::Optional<StringRef> ResourceDir,
Eric Liubfac8f72017-12-19 18:00:37 +0000287 llvm::Optional<Path> CompileCommandsDir,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000288 bool BuildDynamicSymbolIndex,
289 SymbolIndex *StaticIdx)
Ilya Biryukov940901e2017-12-13 12:51:22 +0000290 : Out(Out), CDB(std::move(CompileCommandsDir)), CCOpts(CCOpts),
291 Server(CDB, /*DiagConsumer=*/*this, FSProvider, AsyncThreadsCount,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000292 StorePreamblesInMemory, BuildDynamicSymbolIndex, StaticIdx,
293 ResourceDir) {}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000294
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000295bool ClangdLSPServer::run(std::istream &In) {
Ilya Biryukovafb55542017-05-16 14:40:30 +0000296 assert(!IsDone && "Run was called before");
Ilya Biryukov38d79772017-05-16 09:38:59 +0000297
Ilya Biryukovafb55542017-05-16 14:40:30 +0000298 // Set up JSONRPCDispatcher.
Sam McCalld1a7a372018-01-31 13:40:48 +0000299 JSONRPCDispatcher Dispatcher([](const json::Expr &Params) {
300 replyError(ErrorCode::MethodNotFound, "method not found");
Ilya Biryukov940901e2017-12-13 12:51:22 +0000301 });
Sam McCall4db732a2017-09-30 10:08:52 +0000302 registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000303
Ilya Biryukovafb55542017-05-16 14:40:30 +0000304 // Run the Language Server loop.
305 runLanguageServerLoop(In, Out, Dispatcher, IsDone);
306
307 // Make sure IsDone is set to true after this method exits to ensure assertion
308 // at the start of the method fires if it's ever executed again.
309 IsDone = true;
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000310
311 return ShutdownRequestReceived;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000312}
313
Sam McCall8111d3b2017-12-13 08:48:42 +0000314std::vector<TextEdit> ClangdLSPServer::getFixIts(StringRef File,
315 const clangd::Diagnostic &D) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000316 std::lock_guard<std::mutex> Lock(FixItsMutex);
317 auto DiagToFixItsIter = FixItsMap.find(File);
318 if (DiagToFixItsIter == FixItsMap.end())
319 return {};
320
321 const auto &DiagToFixItsMap = DiagToFixItsIter->second;
322 auto FixItsIter = DiagToFixItsMap.find(D);
323 if (FixItsIter == DiagToFixItsMap.end())
324 return {};
325
326 return FixItsIter->second;
327}
328
Sam McCall4db732a2017-09-30 10:08:52 +0000329void ClangdLSPServer::onDiagnosticsReady(
Sam McCalld1a7a372018-01-31 13:40:48 +0000330 PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000331 json::ary DiagnosticsJSON;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000332
333 DiagnosticToReplacementMap LocalFixIts; // Temporary storage
Sam McCall4db732a2017-09-30 10:08:52 +0000334 for (auto &DiagWithFixes : Diagnostics.Value) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000335 auto Diag = DiagWithFixes.Diag;
Sam McCalldd0566b2017-11-06 15:40:30 +0000336 DiagnosticsJSON.push_back(json::obj{
337 {"range", Diag.range},
338 {"severity", Diag.severity},
339 {"message", Diag.message},
340 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000341 // We convert to Replacements to become independent of the SourceManager.
342 auto &FixItsForDiagnostic = LocalFixIts[Diag];
343 std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(),
344 std::back_inserter(FixItsForDiagnostic));
345 }
346
347 // Cache FixIts
348 {
349 // FIXME(ibiryukov): should be deleted when documents are removed
350 std::lock_guard<std::mutex> Lock(FixItsMutex);
351 FixItsMap[File] = LocalFixIts;
352 }
353
354 // Publish diagnostics.
Sam McCalldd0566b2017-11-06 15:40:30 +0000355 Out.writeMessage(json::obj{
356 {"jsonrpc", "2.0"},
357 {"method", "textDocument/publishDiagnostics"},
358 {"params",
359 json::obj{
Eric Liu78ed91a72018-01-29 15:37:46 +0000360 {"uri", URIForFile{File}},
Sam McCalldd0566b2017-11-06 15:40:30 +0000361 {"diagnostics", std::move(DiagnosticsJSON)},
362 }},
363 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000364}