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" |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 16 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 17 | using namespace clang::clangd; |
| 18 | using namespace clang; |
| 19 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 22 | /// \brief Supports a test URI scheme with relaxed constraints for lit tests. |
| 23 | /// The path in a test URI will be combined with a platform-specific fake |
| 24 | /// directory to form an absolute path. For example, test:///a.cpp is resolved |
| 25 | /// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix. |
| 26 | class TestScheme : public URIScheme { |
| 27 | public: |
| 28 | llvm::Expected<std::string> |
| 29 | getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body, |
| 30 | llvm::StringRef /*HintPath*/) const override { |
| 31 | using namespace llvm::sys; |
| 32 | // Still require "/" in body to mimic file scheme, as we want lengths of an |
| 33 | // equivalent URI in both schemes to be the same. |
| 34 | if (!Body.startswith("/")) |
| 35 | return llvm::make_error<llvm::StringError>( |
| 36 | "Expect URI body to be an absolute path starting with '/': " + Body, |
| 37 | llvm::inconvertibleErrorCode()); |
| 38 | Body = Body.ltrim('/'); |
| 39 | #ifdef LLVM_ON_WIN32 |
| 40 | constexpr char TestDir[] = "C:\\clangd-test"; |
| 41 | #else |
| 42 | constexpr char TestDir[] = "/clangd-test"; |
| 43 | #endif |
| 44 | llvm::SmallVector<char, 16> Path(Body.begin(), Body.end()); |
| 45 | path::native(Path); |
| 46 | auto Err = fs::make_absolute(TestDir, Path); |
Eric Liu | cda2526 | 2018-02-01 12:44:52 +0000 | [diff] [blame] | 47 | if (Err) |
| 48 | llvm_unreachable("Failed to make absolute path in test scheme."); |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 49 | return std::string(Path.begin(), Path.end()); |
| 50 | } |
| 51 | |
| 52 | llvm::Expected<URI> |
| 53 | uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override { |
| 54 | llvm_unreachable("Clangd must never create a test URI."); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | static URISchemeRegistry::Add<TestScheme> |
| 59 | X("test", "Test scheme for clangd lit tests."); |
| 60 | |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 61 | TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R) { |
| 62 | Range ReplacementRange = { |
| 63 | offsetToPosition(Code, R.getOffset()), |
| 64 | offsetToPosition(Code, R.getOffset() + R.getLength())}; |
| 65 | return {ReplacementRange, R.getReplacementText()}; |
| 66 | } |
| 67 | |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 68 | std::vector<TextEdit> |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 69 | replacementsToEdits(StringRef Code, |
| 70 | const std::vector<tooling::Replacement> &Replacements) { |
| 71 | // Turn the replacements into the format specified by the Language Server |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 72 | // Protocol. Fuse them into one big JSON array. |
| 73 | std::vector<TextEdit> Edits; |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 74 | for (const auto &R : Replacements) |
| 75 | Edits.push_back(replacementToEdit(Code, R)); |
| 76 | return Edits; |
| 77 | } |
| 78 | |
| 79 | std::vector<TextEdit> replacementsToEdits(StringRef Code, |
| 80 | const tooling::Replacements &Repls) { |
| 81 | std::vector<TextEdit> Edits; |
| 82 | for (const auto &R : Repls) |
| 83 | Edits.push_back(replacementToEdit(Code, R)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 84 | return Edits; |
| 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 89 | void ClangdLSPServer::onInitialize(InitializeParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 90 | if (Params.rootUri && *Params.rootUri) |
| 91 | Server.setRootPath(Params.rootUri->file()); |
Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 92 | else if (Params.rootPath && !Params.rootPath->empty()) |
| 93 | Server.setRootPath(*Params.rootPath); |
| 94 | |
| 95 | CCOpts.EnableSnippets = |
| 96 | Params.capabilities.textDocument.completion.completionItem.snippetSupport; |
| 97 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 98 | reply(json::obj{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 99 | {{"capabilities", |
| 100 | json::obj{ |
| 101 | {"textDocumentSync", 1}, |
| 102 | {"documentFormattingProvider", true}, |
| 103 | {"documentRangeFormattingProvider", true}, |
| 104 | {"documentOnTypeFormattingProvider", |
| 105 | json::obj{ |
| 106 | {"firstTriggerCharacter", "}"}, |
| 107 | {"moreTriggerCharacter", {}}, |
| 108 | }}, |
| 109 | {"codeActionProvider", true}, |
| 110 | {"completionProvider", |
| 111 | json::obj{ |
| 112 | {"resolveProvider", false}, |
| 113 | {"triggerCharacters", {".", ">", ":"}}, |
| 114 | }}, |
| 115 | {"signatureHelpProvider", |
| 116 | json::obj{ |
| 117 | {"triggerCharacters", {"(", ","}}, |
| 118 | }}, |
| 119 | {"definitionProvider", true}, |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 120 | {"documentHighlightProvider", true}, |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 121 | {"hoverProvider", true}, |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 122 | {"renameProvider", true}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 123 | {"executeCommandProvider", |
| 124 | json::obj{ |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 125 | {"commands", |
| 126 | {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND, |
| 127 | ExecuteCommandParams::CLANGD_INSERT_HEADER_INCLUDE}}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 128 | }}, |
| 129 | }}}}); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 132 | void ClangdLSPServer::onShutdown(ShutdownParams &Params) { |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 133 | // Do essentially nothing, just say we're ready to exit. |
| 134 | ShutdownRequestReceived = true; |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 135 | reply(nullptr); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 136 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 137 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 138 | void ClangdLSPServer::onExit(ExitParams &Params) { IsDone = true; } |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 139 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 140 | void ClangdLSPServer::onDocumentDidOpen(DidOpenTextDocumentParams &Params) { |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 141 | if (Params.metadata && !Params.metadata->extraFlags.empty()) |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 142 | CDB.setExtraFlagsForFile(Params.textDocument.uri.file(), |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 143 | std::move(Params.metadata->extraFlags)); |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame] | 144 | Server.addDocument(Params.textDocument.uri.file(), Params.textDocument.text, |
| 145 | WantDiagnostics::Yes); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 148 | void ClangdLSPServer::onDocumentDidChange(DidChangeTextDocumentParams &Params) { |
Benjamin Kramer | b560a9a | 2017-10-26 10:36:20 +0000 | [diff] [blame] | 149 | if (Params.contentChanges.size() != 1) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 150 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 151 | "can only apply one change at a time"); |
Eric Liu | 51fed18 | 2018-02-22 18:40:39 +0000 | [diff] [blame] | 152 | auto WantDiags = WantDiagnostics::Auto; |
| 153 | if (Params.wantDiagnostics.hasValue()) |
| 154 | WantDiags = Params.wantDiagnostics.getValue() ? WantDiagnostics::Yes |
| 155 | : WantDiagnostics::No; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 156 | // We only support full syncing right now. |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 157 | Server.addDocument(Params.textDocument.uri.file(), |
Eric Liu | 51fed18 | 2018-02-22 18:40:39 +0000 | [diff] [blame] | 158 | Params.contentChanges[0].text, WantDiags); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 161 | void ClangdLSPServer::onFileEvent(DidChangeWatchedFilesParams &Params) { |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 162 | Server.onFileEvent(Params); |
| 163 | } |
| 164 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 165 | void ClangdLSPServer::onCommand(ExecuteCommandParams &Params) { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 166 | auto ApplyEdit = [](WorkspaceEdit WE) { |
| 167 | ApplyWorkspaceEditParams Edit; |
| 168 | Edit.edit = std::move(WE); |
| 169 | // We don't need the response so id == 1 is OK. |
| 170 | // Ideally, we would wait for the response and if there is no error, we |
| 171 | // would reply success/failure to the original RPC. |
| 172 | call("workspace/applyEdit", Edit); |
| 173 | }; |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 174 | if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && |
| 175 | Params.workspaceEdit) { |
| 176 | // The flow for "apply-fix" : |
| 177 | // 1. We publish a diagnostic, including fixits |
| 178 | // 2. The user clicks on the diagnostic, the editor asks us for code actions |
| 179 | // 3. We send code actions, with the fixit embedded as context |
| 180 | // 4. The user selects the fixit, the editor asks us to apply it |
| 181 | // 5. We unwrap the changes and send them back to the editor |
| 182 | // 6. The editor applies the changes (applyEdit), and sends us a reply (but |
| 183 | // we ignore it) |
| 184 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 185 | reply("Fix applied."); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 186 | ApplyEdit(*Params.workspaceEdit); |
| 187 | } else if (Params.command == |
| 188 | ExecuteCommandParams::CLANGD_INSERT_HEADER_INCLUDE) { |
| 189 | auto &FileURI = Params.includeInsertion->textDocument.uri; |
| 190 | auto Code = Server.getDocument(FileURI.file()); |
| 191 | if (!Code) |
| 192 | return replyError(ErrorCode::InvalidParams, |
| 193 | ("command " + |
| 194 | ExecuteCommandParams::CLANGD_INSERT_HEADER_INCLUDE + |
| 195 | " called on non-added file " + FileURI.file()) |
| 196 | .str()); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 197 | llvm::StringRef DeclaringHeader = Params.includeInsertion->declaringHeader; |
| 198 | if (DeclaringHeader.empty()) |
| 199 | return replyError( |
| 200 | ErrorCode::InvalidParams, |
| 201 | "declaringHeader must be provided for include insertion."); |
| 202 | llvm::StringRef PreferredHeader = Params.includeInsertion->preferredHeader; |
| 203 | auto Replaces = Server.insertInclude( |
| 204 | FileURI.file(), *Code, DeclaringHeader, |
| 205 | PreferredHeader.empty() ? DeclaringHeader : PreferredHeader); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 206 | if (!Replaces) { |
| 207 | std::string ErrMsg = |
| 208 | ("Failed to generate include insertion edits for adding " + |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 209 | DeclaringHeader + " (" + PreferredHeader + ") into " + |
| 210 | FileURI.file()) |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 211 | .str(); |
| 212 | log(ErrMsg + ":" + llvm::toString(Replaces.takeError())); |
| 213 | replyError(ErrorCode::InternalError, ErrMsg); |
| 214 | return; |
| 215 | } |
| 216 | auto Edits = replacementsToEdits(*Code, *Replaces); |
| 217 | WorkspaceEdit WE; |
| 218 | WE.changes = {{FileURI.uri(), Edits}}; |
| 219 | |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 220 | reply(("Inserted header " + DeclaringHeader + " (" + PreferredHeader + ")") |
| 221 | .str()); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 222 | ApplyEdit(std::move(WE)); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 223 | } else { |
| 224 | // We should not get here because ExecuteCommandParams would not have |
| 225 | // parsed in the first place and this handler should not be called. But if |
| 226 | // more commands are added, this will be here has a safe guard. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 227 | replyError( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 228 | ErrorCode::InvalidParams, |
Haojian Wu | 2375c92 | 2017-11-07 10:21:02 +0000 | [diff] [blame] | 229 | llvm::formatv("Unsupported command \"{0}\".", Params.command).str()); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 233 | void ClangdLSPServer::onRename(RenameParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 234 | Path File = Params.textDocument.uri.file(); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 235 | llvm::Optional<std::string> Code = Server.getDocument(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 236 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 237 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 238 | "onRename called for non-added file"); |
| 239 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 240 | Server.rename( |
| 241 | File, Params.position, Params.newName, |
| 242 | [File, Code, |
| 243 | Params](llvm::Expected<std::vector<tooling::Replacement>> Replacements) { |
| 244 | if (!Replacements) |
| 245 | return replyError(ErrorCode::InternalError, |
| 246 | llvm::toString(Replacements.takeError())); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 247 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 248 | std::vector<TextEdit> Edits = replacementsToEdits(*Code, *Replacements); |
| 249 | WorkspaceEdit WE; |
| 250 | WE.changes = {{Params.textDocument.uri.uri(), Edits}}; |
| 251 | reply(WE); |
| 252 | }); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 255 | void ClangdLSPServer::onDocumentDidClose(DidCloseTextDocumentParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 256 | Server.removeDocument(Params.textDocument.uri.file()); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 259 | void ClangdLSPServer::onDocumentOnTypeFormatting( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 260 | DocumentOnTypeFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 261 | auto File = Params.textDocument.uri.file(); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 262 | auto Code = Server.getDocument(File); |
| 263 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 264 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 265 | "onDocumentOnTypeFormatting called for non-added file"); |
| 266 | |
| 267 | auto ReplacementsOrError = Server.formatOnType(*Code, File, Params.position); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 268 | if (ReplacementsOrError) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 269 | reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 270 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 271 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 272 | llvm::toString(ReplacementsOrError.takeError())); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 275 | void ClangdLSPServer::onDocumentRangeFormatting( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 276 | DocumentRangeFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 277 | auto File = Params.textDocument.uri.file(); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 278 | auto Code = Server.getDocument(File); |
| 279 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 280 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 281 | "onDocumentRangeFormatting called for non-added file"); |
| 282 | |
| 283 | auto ReplacementsOrError = Server.formatRange(*Code, File, Params.range); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 284 | if (ReplacementsOrError) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 285 | reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 286 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 287 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 288 | llvm::toString(ReplacementsOrError.takeError())); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 291 | void ClangdLSPServer::onDocumentFormatting(DocumentFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 292 | auto File = Params.textDocument.uri.file(); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 293 | auto Code = Server.getDocument(File); |
| 294 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 295 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 296 | "onDocumentFormatting called for non-added file"); |
| 297 | |
| 298 | auto ReplacementsOrError = Server.formatFile(*Code, File); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 299 | if (ReplacementsOrError) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 300 | reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 301 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 302 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 303 | llvm::toString(ReplacementsOrError.takeError())); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 306 | void ClangdLSPServer::onCodeAction(CodeActionParams &Params) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 307 | // We provide a code action for each diagnostic at the requested location |
| 308 | // which has FixIts available. |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 309 | auto Code = Server.getDocument(Params.textDocument.uri.file()); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 310 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 311 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 312 | "onCodeAction called for non-added file"); |
| 313 | |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 314 | json::ary Commands; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 315 | for (Diagnostic &D : Params.context.diagnostics) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 316 | auto Edits = getFixIts(Params.textDocument.uri.file(), D); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 317 | if (!Edits.empty()) { |
| 318 | WorkspaceEdit WE; |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 319 | WE.changes = {{Params.textDocument.uri.uri(), std::move(Edits)}}; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 320 | Commands.push_back(json::obj{ |
| 321 | {"title", llvm::formatv("Apply FixIt {0}", D.message)}, |
| 322 | {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}, |
| 323 | {"arguments", {WE}}, |
| 324 | }); |
| 325 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 326 | } |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 327 | reply(std::move(Commands)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 330 | void ClangdLSPServer::onCompletion(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 331 | Server.codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 332 | [](Tagged<CompletionList> List) { reply(List.Value); }); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 335 | void ClangdLSPServer::onSignatureHelp(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 336 | Server.signatureHelp(Params.textDocument.uri.file(), Params.position, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 337 | [](llvm::Expected<Tagged<SignatureHelp>> SignatureHelp) { |
| 338 | if (!SignatureHelp) |
| 339 | return replyError( |
| 340 | ErrorCode::InvalidParams, |
| 341 | llvm::toString(SignatureHelp.takeError())); |
| 342 | reply(SignatureHelp->Value); |
| 343 | }); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 346 | void ClangdLSPServer::onGoToDefinition(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 347 | Server.findDefinitions( |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 348 | Params.textDocument.uri.file(), Params.position, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 349 | [](llvm::Expected<Tagged<std::vector<Location>>> Items) { |
| 350 | if (!Items) |
| 351 | return replyError(ErrorCode::InvalidParams, |
| 352 | llvm::toString(Items.takeError())); |
| 353 | reply(json::ary(Items->Value)); |
| 354 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 357 | void ClangdLSPServer::onSwitchSourceHeader(TextDocumentIdentifier &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 358 | llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 359 | reply(Result ? URI::createFile(*Result).toString() : ""); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 362 | void ClangdLSPServer::onDocumentHighlight(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 363 | Server.findDocumentHighlights( |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 364 | Params.textDocument.uri.file(), Params.position, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 365 | [](llvm::Expected<Tagged<std::vector<DocumentHighlight>>> Highlights) { |
| 366 | if (!Highlights) |
| 367 | return replyError(ErrorCode::InternalError, |
| 368 | llvm::toString(Highlights.takeError())); |
| 369 | reply(json::ary(Highlights->Value)); |
| 370 | }); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 373 | void ClangdLSPServer::onHover(TextDocumentPositionParams &Params) { |
| 374 | Server.findHover(Params.textDocument.uri.file(), Params.position, |
| 375 | [](llvm::Expected<Tagged<Hover>> H) { |
| 376 | if (!H) { |
| 377 | replyError(ErrorCode::InternalError, |
| 378 | llvm::toString(H.takeError())); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | reply(H->Value); |
| 383 | }); |
| 384 | } |
| 385 | |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 386 | // FIXME: This function needs to be properly tested. |
| 387 | void ClangdLSPServer::onChangeConfiguration( |
| 388 | DidChangeConfigurationParams &Params) { |
| 389 | ClangdConfigurationParamsChange &Settings = Params.settings; |
| 390 | |
| 391 | // Compilation database change. |
| 392 | if (Settings.compilationDatabasePath.hasValue()) { |
| 393 | CDB.setCompileCommandsDir(Settings.compilationDatabasePath.getValue()); |
| 394 | Server.reparseOpenedFiles(); |
| 395 | } |
| 396 | } |
| 397 | |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 398 | ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 399 | const clangd::CodeCompleteOptions &CCOpts, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 400 | llvm::Optional<Path> CompileCommandsDir, |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 401 | const ClangdServer::Options &Opts) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 402 | : Out(Out), CDB(std::move(CompileCommandsDir)), CCOpts(CCOpts), |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 403 | Server(CDB, FSProvider, /*DiagConsumer=*/*this, Opts) {} |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 404 | |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 405 | bool ClangdLSPServer::run(std::istream &In, JSONStreamStyle InputStyle) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 406 | assert(!IsDone && "Run was called before"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 407 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 408 | // Set up JSONRPCDispatcher. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 409 | JSONRPCDispatcher Dispatcher([](const json::Expr &Params) { |
| 410 | replyError(ErrorCode::MethodNotFound, "method not found"); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 411 | }); |
Simon Marchi | 6e8eb9d | 2018-03-07 21:47:25 +0000 | [diff] [blame^] | 412 | registerCallbackHandlers(Dispatcher, /*Callbacks=*/*this); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 413 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 414 | // Run the Language Server loop. |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 415 | runLanguageServerLoop(In, Out, InputStyle, Dispatcher, IsDone); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 416 | |
| 417 | // Make sure IsDone is set to true after this method exits to ensure assertion |
| 418 | // at the start of the method fires if it's ever executed again. |
| 419 | IsDone = true; |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 420 | |
| 421 | return ShutdownRequestReceived; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 424 | std::vector<TextEdit> ClangdLSPServer::getFixIts(StringRef File, |
| 425 | const clangd::Diagnostic &D) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 426 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 427 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 428 | if (DiagToFixItsIter == FixItsMap.end()) |
| 429 | return {}; |
| 430 | |
| 431 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 432 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 433 | if (FixItsIter == DiagToFixItsMap.end()) |
| 434 | return {}; |
| 435 | |
| 436 | return FixItsIter->second; |
| 437 | } |
| 438 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 439 | void ClangdLSPServer::onDiagnosticsReady( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 440 | PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 441 | json::ary DiagnosticsJSON; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 442 | |
| 443 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 444 | for (auto &DiagWithFixes : Diagnostics.Value) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 445 | auto Diag = DiagWithFixes.Diag; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 446 | DiagnosticsJSON.push_back(json::obj{ |
| 447 | {"range", Diag.range}, |
| 448 | {"severity", Diag.severity}, |
| 449 | {"message", Diag.message}, |
| 450 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 451 | // We convert to Replacements to become independent of the SourceManager. |
| 452 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
| 453 | std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(), |
| 454 | std::back_inserter(FixItsForDiagnostic)); |
| 455 | } |
| 456 | |
| 457 | // Cache FixIts |
| 458 | { |
| 459 | // FIXME(ibiryukov): should be deleted when documents are removed |
| 460 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 461 | FixItsMap[File] = LocalFixIts; |
| 462 | } |
| 463 | |
| 464 | // Publish diagnostics. |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 465 | Out.writeMessage(json::obj{ |
| 466 | {"jsonrpc", "2.0"}, |
| 467 | {"method", "textDocument/publishDiagnostics"}, |
| 468 | {"params", |
| 469 | json::obj{ |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 470 | {"uri", URIForFile{File}}, |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 471 | {"diagnostics", std::move(DiagnosticsJSON)}, |
| 472 | }}, |
| 473 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 474 | } |