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