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