Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- 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 Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FormatVariadic.h" |
| 14 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 15 | using namespace clang::clangd; |
| 16 | using namespace clang; |
| 17 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 20 | std::vector<TextEdit> |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 21 | replacementsToEdits(StringRef Code, |
| 22 | const std::vector<tooling::Replacement> &Replacements) { |
| 23 | // Turn the replacements into the format specified by the Language Server |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 24 | // Protocol. Fuse them into one big JSON array. |
| 25 | std::vector<TextEdit> Edits; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 26 | for (auto &R : Replacements) { |
| 27 | Range ReplacementRange = { |
| 28 | offsetToPosition(Code, R.getOffset()), |
| 29 | offsetToPosition(Code, R.getOffset() + R.getLength())}; |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 30 | Edits.push_back({ReplacementRange, R.getReplacementText()}); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 31 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 32 | return Edits; |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 37 | void ClangdLSPServer::onInitialize(Ctx C, InitializeParams &Params) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 38 | 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 McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 63 | 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 Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 69 | void ClangdLSPServer::onShutdown(Ctx C, ShutdownParams &Params) { |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 70 | // Do essentially nothing, just say we're ready to exit. |
| 71 | ShutdownRequestReceived = true; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 72 | C.reply(nullptr); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 73 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 74 | |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 75 | void ClangdLSPServer::onExit(Ctx C, ExitParams &Params) { IsDone = true; } |
| 76 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 77 | void ClangdLSPServer::onDocumentDidOpen(Ctx C, |
| 78 | DidOpenTextDocumentParams &Params) { |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 79 | if (Params.metadata && !Params.metadata->extraFlags.empty()) |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 80 | CDB.setExtraFlagsForFile(Params.textDocument.uri.file, |
| 81 | std::move(Params.metadata->extraFlags)); |
| 82 | Server.addDocument(Params.textDocument.uri.file, Params.textDocument.text); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 85 | void ClangdLSPServer::onDocumentDidChange(Ctx C, |
| 86 | DidChangeTextDocumentParams &Params) { |
Benjamin Kramer | b560a9a | 2017-10-26 10:36:20 +0000 | [diff] [blame] | 87 | if (Params.contentChanges.size() != 1) |
| 88 | return C.replyError(-32602, "can only apply one change at a time"); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 89 | // We only support full syncing right now. |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 90 | Server.addDocument(Params.textDocument.uri.file, |
| 91 | Params.contentChanges[0].text); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 94 | void ClangdLSPServer::onFileEvent(Ctx C, DidChangeWatchedFilesParams &Params) { |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 95 | Server.onFileEvent(Params); |
| 96 | } |
| 97 | |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 98 | void ClangdLSPServer::onCommand(Ctx C, ExecuteCommandParams &Params) { |
| 99 | if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && |
| 100 | Params.workspaceEdit) { |
| 101 | // The flow for "apply-fix" : |
| 102 | // 1. We publish a diagnostic, including fixits |
| 103 | // 2. The user clicks on the diagnostic, the editor asks us for code actions |
| 104 | // 3. We send code actions, with the fixit embedded as context |
| 105 | // 4. The user selects the fixit, the editor asks us to apply it |
| 106 | // 5. We unwrap the changes and send them back to the editor |
| 107 | // 6. The editor applies the changes (applyEdit), and sends us a reply (but |
| 108 | // we ignore it) |
| 109 | |
| 110 | ApplyWorkspaceEditParams ApplyEdit; |
| 111 | ApplyEdit.edit = *Params.workspaceEdit; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 112 | C.reply("Fix applied."); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 113 | // We don't need the response so id == 1 is OK. |
| 114 | // Ideally, we would wait for the response and if there is no error, we |
| 115 | // would reply success/failure to the original RPC. |
| 116 | C.call("workspace/applyEdit", ApplyWorkspaceEditParams::unparse(ApplyEdit)); |
| 117 | } else { |
| 118 | // We should not get here because ExecuteCommandParams would not have |
| 119 | // parsed in the first place and this handler should not be called. But if |
| 120 | // more commands are added, this will be here has a safe guard. |
| 121 | C.replyError( |
| 122 | 1, llvm::formatv("Unsupported command \"{0}\".", Params.command).str()); |
| 123 | } |
| 124 | } |
| 125 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 126 | void ClangdLSPServer::onDocumentDidClose(Ctx C, |
| 127 | DidCloseTextDocumentParams &Params) { |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 128 | Server.removeDocument(Params.textDocument.uri.file); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 131 | void ClangdLSPServer::onDocumentOnTypeFormatting( |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 132 | Ctx C, DocumentOnTypeFormattingParams &Params) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 133 | auto File = Params.textDocument.uri.file; |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 134 | std::string Code = Server.getDocument(File); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 135 | C.reply(json::ary( |
| 136 | replacementsToEdits(Code, Server.formatOnType(File, Params.position)))); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 139 | void ClangdLSPServer::onDocumentRangeFormatting( |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 140 | Ctx C, DocumentRangeFormattingParams &Params) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 141 | auto File = Params.textDocument.uri.file; |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 142 | std::string Code = Server.getDocument(File); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 143 | C.reply(json::ary( |
| 144 | replacementsToEdits(Code, Server.formatRange(File, Params.range)))); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 147 | void ClangdLSPServer::onDocumentFormatting(Ctx C, |
| 148 | DocumentFormattingParams &Params) { |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 149 | auto File = Params.textDocument.uri.file; |
| 150 | std::string Code = Server.getDocument(File); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 151 | C.reply(json::ary(replacementsToEdits(Code, Server.formatFile(File)))); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 154 | void ClangdLSPServer::onCodeAction(Ctx C, CodeActionParams &Params) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 155 | // We provide a code action for each diagnostic at the requested location |
| 156 | // which has FixIts available. |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 157 | std::string Code = Server.getDocument(Params.textDocument.uri.file); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 158 | json::ary Commands; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 159 | for (Diagnostic &D : Params.context.diagnostics) { |
| 160 | std::vector<clang::tooling::Replacement> Fixes = |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 161 | getFixIts(Params.textDocument.uri.file, D); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 162 | auto Edits = replacementsToEdits(Code, Fixes); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 163 | if (!Edits.empty()) { |
| 164 | WorkspaceEdit WE; |
| 165 | WE.changes = {{Params.textDocument.uri.uri, std::move(Edits)}}; |
| 166 | Commands.push_back(json::obj{ |
| 167 | {"title", llvm::formatv("Apply FixIt {0}", D.message)}, |
| 168 | {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}, |
| 169 | {"arguments", {WE}}, |
| 170 | }); |
| 171 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 172 | } |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 173 | C.reply(std::move(Commands)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 176 | void ClangdLSPServer::onCompletion(Ctx C, TextDocumentPositionParams &Params) { |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 177 | auto Items = Server |
Ilya Biryukov | 574b753 | 2017-08-02 09:08:39 +0000 | [diff] [blame] | 178 | .codeComplete(Params.textDocument.uri.file, |
| 179 | Position{Params.position.line, |
| 180 | Params.position.character}) |
Ilya Biryukov | dcd2169 | 2017-10-05 17:04:13 +0000 | [diff] [blame] | 181 | .get() // FIXME(ibiryukov): This could be made async if we |
| 182 | // had an API that would allow to attach callbacks to |
| 183 | // futures returned by ClangdServer. |
Ilya Biryukov | 574b753 | 2017-08-02 09:08:39 +0000 | [diff] [blame] | 184 | .Value; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 185 | C.reply(json::ary(Items)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 188 | void ClangdLSPServer::onSignatureHelp(Ctx C, |
| 189 | TextDocumentPositionParams &Params) { |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 190 | auto SignatureHelp = Server.signatureHelp( |
| 191 | Params.textDocument.uri.file, |
| 192 | Position{Params.position.line, Params.position.character}); |
| 193 | if (!SignatureHelp) |
| 194 | return C.replyError(-32602, llvm::toString(SignatureHelp.takeError())); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 195 | C.reply(SignatureHelp->Value); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 198 | void ClangdLSPServer::onGoToDefinition(Ctx C, |
| 199 | TextDocumentPositionParams &Params) { |
Benjamin Kramer | ee19f16 | 2017-10-26 12:28:13 +0000 | [diff] [blame] | 200 | auto Items = Server.findDefinitions( |
| 201 | Params.textDocument.uri.file, |
| 202 | Position{Params.position.line, Params.position.character}); |
| 203 | if (!Items) |
| 204 | return C.replyError(-32602, llvm::toString(Items.takeError())); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 205 | C.reply(json::ary(Items->Value)); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 208 | void ClangdLSPServer::onSwitchSourceHeader(Ctx C, |
| 209 | TextDocumentIdentifier &Params) { |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 210 | llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 211 | std::string ResultUri; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 212 | C.reply(Result ? URI::fromFile(*Result).uri : ""); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 215 | ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount, |
Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame] | 216 | bool SnippetCompletions, |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 217 | llvm::Optional<StringRef> ResourceDir, |
| 218 | llvm::Optional<Path> CompileCommandsDir) |
| 219 | : Out(Out), CDB(/*Logger=*/Out, std::move(CompileCommandsDir)), |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 220 | Server(CDB, /*DiagConsumer=*/*this, FSProvider, AsyncThreadsCount, |
Ilya Biryukov | b080cb1 | 2017-10-23 14:46:48 +0000 | [diff] [blame] | 221 | clangd::CodeCompleteOptions( |
| 222 | /*EnableSnippetsAndCodePatterns=*/SnippetCompletions), |
| 223 | /*Logger=*/Out, ResourceDir) {} |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 224 | |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 225 | bool ClangdLSPServer::run(std::istream &In) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 226 | assert(!IsDone && "Run was called before"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 227 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 228 | // Set up JSONRPCDispatcher. |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 229 | JSONRPCDispatcher Dispatcher( |
| 230 | [](RequestContext Ctx, llvm::yaml::MappingNode *Params) { |
| 231 | Ctx.replyError(-32601, "method not found"); |
| 232 | }); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 233 | registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 234 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 235 | // Run the Language Server loop. |
| 236 | runLanguageServerLoop(In, Out, Dispatcher, IsDone); |
| 237 | |
| 238 | // Make sure IsDone is set to true after this method exits to ensure assertion |
| 239 | // at the start of the method fires if it's ever executed again. |
| 240 | IsDone = true; |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 241 | |
| 242 | return ShutdownRequestReceived; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | std::vector<clang::tooling::Replacement> |
| 246 | ClangdLSPServer::getFixIts(StringRef File, const clangd::Diagnostic &D) { |
| 247 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 248 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 249 | if (DiagToFixItsIter == FixItsMap.end()) |
| 250 | return {}; |
| 251 | |
| 252 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 253 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 254 | if (FixItsIter == DiagToFixItsMap.end()) |
| 255 | return {}; |
| 256 | |
| 257 | return FixItsIter->second; |
| 258 | } |
| 259 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 260 | void ClangdLSPServer::onDiagnosticsReady( |
| 261 | PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 262 | json::ary DiagnosticsJSON; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 263 | |
| 264 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 265 | for (auto &DiagWithFixes : Diagnostics.Value) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 266 | auto Diag = DiagWithFixes.Diag; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 267 | DiagnosticsJSON.push_back(json::obj{ |
| 268 | {"range", Diag.range}, |
| 269 | {"severity", Diag.severity}, |
| 270 | {"message", Diag.message}, |
| 271 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 272 | // We convert to Replacements to become independent of the SourceManager. |
| 273 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
| 274 | std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(), |
| 275 | std::back_inserter(FixItsForDiagnostic)); |
| 276 | } |
| 277 | |
| 278 | // Cache FixIts |
| 279 | { |
| 280 | // FIXME(ibiryukov): should be deleted when documents are removed |
| 281 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 282 | FixItsMap[File] = LocalFixIts; |
| 283 | } |
| 284 | |
| 285 | // Publish diagnostics. |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 286 | Out.writeMessage(json::obj{ |
| 287 | {"jsonrpc", "2.0"}, |
| 288 | {"method", "textDocument/publishDiagnostics"}, |
| 289 | {"params", |
| 290 | json::obj{ |
| 291 | {"uri", URI::fromFile(File)}, |
| 292 | {"diagnostics", std::move(DiagnosticsJSON)}, |
| 293 | }}, |
| 294 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 295 | } |