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