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