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"); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 152 | // We only support full syncing right now. |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 153 | Server.addDocument(Params.textDocument.uri.file(), |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame] | 154 | Params.contentChanges[0].text, WantDiagnostics::Auto); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 157 | void ClangdLSPServer::onFileEvent(DidChangeWatchedFilesParams &Params) { |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 158 | Server.onFileEvent(Params); |
| 159 | } |
| 160 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 161 | void ClangdLSPServer::onCommand(ExecuteCommandParams &Params) { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 162 | auto ApplyEdit = [](WorkspaceEdit WE) { |
| 163 | ApplyWorkspaceEditParams Edit; |
| 164 | Edit.edit = std::move(WE); |
| 165 | // We don't need the response so id == 1 is OK. |
| 166 | // Ideally, we would wait for the response and if there is no error, we |
| 167 | // would reply success/failure to the original RPC. |
| 168 | call("workspace/applyEdit", Edit); |
| 169 | }; |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 170 | if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && |
| 171 | Params.workspaceEdit) { |
| 172 | // The flow for "apply-fix" : |
| 173 | // 1. We publish a diagnostic, including fixits |
| 174 | // 2. The user clicks on the diagnostic, the editor asks us for code actions |
| 175 | // 3. We send code actions, with the fixit embedded as context |
| 176 | // 4. The user selects the fixit, the editor asks us to apply it |
| 177 | // 5. We unwrap the changes and send them back to the editor |
| 178 | // 6. The editor applies the changes (applyEdit), and sends us a reply (but |
| 179 | // we ignore it) |
| 180 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 181 | reply("Fix applied."); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 182 | ApplyEdit(*Params.workspaceEdit); |
| 183 | } else if (Params.command == |
| 184 | ExecuteCommandParams::CLANGD_INSERT_HEADER_INCLUDE) { |
| 185 | auto &FileURI = Params.includeInsertion->textDocument.uri; |
| 186 | auto Code = Server.getDocument(FileURI.file()); |
| 187 | if (!Code) |
| 188 | return replyError(ErrorCode::InvalidParams, |
| 189 | ("command " + |
| 190 | ExecuteCommandParams::CLANGD_INSERT_HEADER_INCLUDE + |
| 191 | " called on non-added file " + FileURI.file()) |
| 192 | .str()); |
| 193 | auto Replaces = Server.insertInclude(FileURI.file(), *Code, |
| 194 | Params.includeInsertion->header); |
| 195 | if (!Replaces) { |
| 196 | std::string ErrMsg = |
| 197 | ("Failed to generate include insertion edits for adding " + |
| 198 | Params.includeInsertion->header + " into " + FileURI.file()) |
| 199 | .str(); |
| 200 | log(ErrMsg + ":" + llvm::toString(Replaces.takeError())); |
| 201 | replyError(ErrorCode::InternalError, ErrMsg); |
| 202 | return; |
| 203 | } |
| 204 | auto Edits = replacementsToEdits(*Code, *Replaces); |
| 205 | WorkspaceEdit WE; |
| 206 | WE.changes = {{FileURI.uri(), Edits}}; |
| 207 | |
| 208 | reply("Inserted header " + Params.includeInsertion->header); |
| 209 | ApplyEdit(std::move(WE)); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 210 | } else { |
| 211 | // We should not get here because ExecuteCommandParams would not have |
| 212 | // parsed in the first place and this handler should not be called. But if |
| 213 | // more commands are added, this will be here has a safe guard. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 214 | replyError( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 215 | ErrorCode::InvalidParams, |
Haojian Wu | 2375c92 | 2017-11-07 10:21:02 +0000 | [diff] [blame] | 216 | llvm::formatv("Unsupported command \"{0}\".", Params.command).str()); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 220 | void ClangdLSPServer::onRename(RenameParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 221 | Path File = Params.textDocument.uri.file(); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 222 | llvm::Optional<std::string> Code = Server.getDocument(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 223 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 224 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 225 | "onRename called for non-added file"); |
| 226 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 227 | Server.rename( |
| 228 | File, Params.position, Params.newName, |
| 229 | [File, Code, |
| 230 | Params](llvm::Expected<std::vector<tooling::Replacement>> Replacements) { |
| 231 | if (!Replacements) |
| 232 | return replyError(ErrorCode::InternalError, |
| 233 | llvm::toString(Replacements.takeError())); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 234 | |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 235 | std::vector<TextEdit> Edits = replacementsToEdits(*Code, *Replacements); |
| 236 | WorkspaceEdit WE; |
| 237 | WE.changes = {{Params.textDocument.uri.uri(), Edits}}; |
| 238 | reply(WE); |
| 239 | }); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 242 | void ClangdLSPServer::onDocumentDidClose(DidCloseTextDocumentParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 243 | Server.removeDocument(Params.textDocument.uri.file()); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 246 | void ClangdLSPServer::onDocumentOnTypeFormatting( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 247 | DocumentOnTypeFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 248 | auto File = Params.textDocument.uri.file(); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 249 | auto Code = Server.getDocument(File); |
| 250 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 251 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 252 | "onDocumentOnTypeFormatting called for non-added file"); |
| 253 | |
| 254 | auto ReplacementsOrError = Server.formatOnType(*Code, File, Params.position); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 255 | if (ReplacementsOrError) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 256 | reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 257 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 258 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 259 | llvm::toString(ReplacementsOrError.takeError())); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 262 | void ClangdLSPServer::onDocumentRangeFormatting( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 263 | DocumentRangeFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 264 | auto File = Params.textDocument.uri.file(); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 265 | auto Code = Server.getDocument(File); |
| 266 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 267 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 268 | "onDocumentRangeFormatting called for non-added file"); |
| 269 | |
| 270 | auto ReplacementsOrError = Server.formatRange(*Code, File, Params.range); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 271 | if (ReplacementsOrError) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 272 | reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 273 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 274 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 275 | llvm::toString(ReplacementsOrError.takeError())); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 278 | void ClangdLSPServer::onDocumentFormatting(DocumentFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 279 | auto File = Params.textDocument.uri.file(); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 280 | auto Code = Server.getDocument(File); |
| 281 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 282 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 283 | "onDocumentFormatting called for non-added file"); |
| 284 | |
| 285 | auto ReplacementsOrError = Server.formatFile(*Code, File); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 286 | if (ReplacementsOrError) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 287 | reply(json::ary(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 288 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 289 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 290 | llvm::toString(ReplacementsOrError.takeError())); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 293 | void ClangdLSPServer::onCodeAction(CodeActionParams &Params) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 294 | // We provide a code action for each diagnostic at the requested location |
| 295 | // which has FixIts available. |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 296 | auto Code = Server.getDocument(Params.textDocument.uri.file()); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 297 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 298 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 299 | "onCodeAction called for non-added file"); |
| 300 | |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 301 | json::ary Commands; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 302 | for (Diagnostic &D : Params.context.diagnostics) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 303 | auto Edits = getFixIts(Params.textDocument.uri.file(), D); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 304 | if (!Edits.empty()) { |
| 305 | WorkspaceEdit WE; |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 306 | WE.changes = {{Params.textDocument.uri.uri(), std::move(Edits)}}; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 307 | Commands.push_back(json::obj{ |
| 308 | {"title", llvm::formatv("Apply FixIt {0}", D.message)}, |
| 309 | {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}, |
| 310 | {"arguments", {WE}}, |
| 311 | }); |
| 312 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 313 | } |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 314 | reply(std::move(Commands)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 317 | void ClangdLSPServer::onCompletion(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 318 | Server.codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 319 | [](Tagged<CompletionList> List) { reply(List.Value); }); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 322 | void ClangdLSPServer::onSignatureHelp(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 323 | Server.signatureHelp(Params.textDocument.uri.file(), Params.position, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 324 | [](llvm::Expected<Tagged<SignatureHelp>> SignatureHelp) { |
| 325 | if (!SignatureHelp) |
| 326 | return replyError( |
| 327 | ErrorCode::InvalidParams, |
| 328 | llvm::toString(SignatureHelp.takeError())); |
| 329 | reply(SignatureHelp->Value); |
| 330 | }); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 333 | void ClangdLSPServer::onGoToDefinition(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 334 | Server.findDefinitions( |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 335 | Params.textDocument.uri.file(), Params.position, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 336 | [](llvm::Expected<Tagged<std::vector<Location>>> Items) { |
| 337 | if (!Items) |
| 338 | return replyError(ErrorCode::InvalidParams, |
| 339 | llvm::toString(Items.takeError())); |
| 340 | reply(json::ary(Items->Value)); |
| 341 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 344 | void ClangdLSPServer::onSwitchSourceHeader(TextDocumentIdentifier &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 345 | llvm::Optional<Path> Result = Server.switchSourceHeader(Params.uri.file()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 346 | reply(Result ? URI::createFile(*Result).toString() : ""); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 349 | void ClangdLSPServer::onDocumentHighlight(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 350 | Server.findDocumentHighlights( |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 351 | Params.textDocument.uri.file(), Params.position, |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 352 | [](llvm::Expected<Tagged<std::vector<DocumentHighlight>>> Highlights) { |
| 353 | if (!Highlights) |
| 354 | return replyError(ErrorCode::InternalError, |
| 355 | llvm::toString(Highlights.takeError())); |
| 356 | reply(json::ary(Highlights->Value)); |
| 357 | }); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 360 | void ClangdLSPServer::onHover(TextDocumentPositionParams &Params) { |
| 361 | Server.findHover(Params.textDocument.uri.file(), Params.position, |
| 362 | [](llvm::Expected<Tagged<Hover>> H) { |
| 363 | if (!H) { |
| 364 | replyError(ErrorCode::InternalError, |
| 365 | llvm::toString(H.takeError())); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | reply(H->Value); |
| 370 | }); |
| 371 | } |
| 372 | |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame^] | 373 | // FIXME: This function needs to be properly tested. |
| 374 | void ClangdLSPServer::onChangeConfiguration( |
| 375 | DidChangeConfigurationParams &Params) { |
| 376 | ClangdConfigurationParamsChange &Settings = Params.settings; |
| 377 | |
| 378 | // Compilation database change. |
| 379 | if (Settings.compilationDatabasePath.hasValue()) { |
| 380 | CDB.setCompileCommandsDir(Settings.compilationDatabasePath.getValue()); |
| 381 | Server.reparseOpenedFiles(); |
| 382 | } |
| 383 | } |
| 384 | |
Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 385 | ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount, |
Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 386 | bool StorePreamblesInMemory, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 387 | const clangd::CodeCompleteOptions &CCOpts, |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 388 | llvm::Optional<StringRef> ResourceDir, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 389 | llvm::Optional<Path> CompileCommandsDir, |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 390 | bool BuildDynamicSymbolIndex, |
| 391 | SymbolIndex *StaticIdx) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 392 | : Out(Out), CDB(std::move(CompileCommandsDir)), CCOpts(CCOpts), |
| 393 | Server(CDB, /*DiagConsumer=*/*this, FSProvider, AsyncThreadsCount, |
Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 394 | StorePreamblesInMemory, BuildDynamicSymbolIndex, StaticIdx, |
| 395 | ResourceDir) {} |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 396 | |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 397 | bool ClangdLSPServer::run(std::istream &In, JSONStreamStyle InputStyle) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 398 | assert(!IsDone && "Run was called before"); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 399 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 400 | // Set up JSONRPCDispatcher. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 401 | JSONRPCDispatcher Dispatcher([](const json::Expr &Params) { |
| 402 | replyError(ErrorCode::MethodNotFound, "method not found"); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 403 | }); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 404 | registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 405 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 406 | // Run the Language Server loop. |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 407 | runLanguageServerLoop(In, Out, InputStyle, Dispatcher, IsDone); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 408 | |
| 409 | // Make sure IsDone is set to true after this method exits to ensure assertion |
| 410 | // at the start of the method fires if it's ever executed again. |
| 411 | IsDone = true; |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 412 | |
| 413 | return ShutdownRequestReceived; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 416 | std::vector<TextEdit> ClangdLSPServer::getFixIts(StringRef File, |
| 417 | const clangd::Diagnostic &D) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 418 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 419 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 420 | if (DiagToFixItsIter == FixItsMap.end()) |
| 421 | return {}; |
| 422 | |
| 423 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 424 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 425 | if (FixItsIter == DiagToFixItsMap.end()) |
| 426 | return {}; |
| 427 | |
| 428 | return FixItsIter->second; |
| 429 | } |
| 430 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 431 | void ClangdLSPServer::onDiagnosticsReady( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 432 | PathRef File, Tagged<std::vector<DiagWithFixIts>> Diagnostics) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 433 | json::ary DiagnosticsJSON; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 434 | |
| 435 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 436 | for (auto &DiagWithFixes : Diagnostics.Value) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 437 | auto Diag = DiagWithFixes.Diag; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 438 | DiagnosticsJSON.push_back(json::obj{ |
| 439 | {"range", Diag.range}, |
| 440 | {"severity", Diag.severity}, |
| 441 | {"message", Diag.message}, |
| 442 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 443 | // We convert to Replacements to become independent of the SourceManager. |
| 444 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
| 445 | std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(), |
| 446 | std::back_inserter(FixItsForDiagnostic)); |
| 447 | } |
| 448 | |
| 449 | // Cache FixIts |
| 450 | { |
| 451 | // FIXME(ibiryukov): should be deleted when documents are removed |
| 452 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 453 | FixItsMap[File] = LocalFixIts; |
| 454 | } |
| 455 | |
| 456 | // Publish diagnostics. |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 457 | Out.writeMessage(json::obj{ |
| 458 | {"jsonrpc", "2.0"}, |
| 459 | {"method", "textDocument/publishDiagnostics"}, |
| 460 | {"params", |
| 461 | json::obj{ |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 462 | {"uri", URIForFile{File}}, |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 463 | {"diagnostics", std::move(DiagnosticsJSON)}, |
| 464 | }}, |
| 465 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 466 | } |