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