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