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 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 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" |
Kadir Cetinkaya | 689bf93 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ScopeExit.h" |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errc.h" |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FormatVariadic.h" |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 19 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 20 | using namespace clang::clangd; |
| 21 | using namespace clang; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 23 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 26 | /// \brief Supports a test URI scheme with relaxed constraints for lit tests. |
| 27 | /// The path in a test URI will be combined with a platform-specific fake |
| 28 | /// directory to form an absolute path. For example, test:///a.cpp is resolved |
| 29 | /// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix. |
| 30 | class TestScheme : public URIScheme { |
| 31 | public: |
| 32 | llvm::Expected<std::string> |
| 33 | getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body, |
| 34 | llvm::StringRef /*HintPath*/) const override { |
| 35 | using namespace llvm::sys; |
| 36 | // Still require "/" in body to mimic file scheme, as we want lengths of an |
| 37 | // equivalent URI in both schemes to be the same. |
| 38 | if (!Body.startswith("/")) |
| 39 | return llvm::make_error<llvm::StringError>( |
| 40 | "Expect URI body to be an absolute path starting with '/': " + Body, |
| 41 | llvm::inconvertibleErrorCode()); |
| 42 | Body = Body.ltrim('/'); |
Nico Weber | 0da2290 | 2018-04-10 13:14:03 +0000 | [diff] [blame] | 43 | #ifdef _WIN32 |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 44 | constexpr char TestDir[] = "C:\\clangd-test"; |
| 45 | #else |
| 46 | constexpr char TestDir[] = "/clangd-test"; |
| 47 | #endif |
| 48 | llvm::SmallVector<char, 16> Path(Body.begin(), Body.end()); |
| 49 | path::native(Path); |
| 50 | auto Err = fs::make_absolute(TestDir, Path); |
Eric Liu | cda2526 | 2018-02-01 12:44:52 +0000 | [diff] [blame] | 51 | if (Err) |
| 52 | llvm_unreachable("Failed to make absolute path in test scheme."); |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 53 | return std::string(Path.begin(), Path.end()); |
| 54 | } |
| 55 | |
| 56 | llvm::Expected<URI> |
| 57 | uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override { |
| 58 | llvm_unreachable("Clangd must never create a test URI."); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | static URISchemeRegistry::Add<TestScheme> |
| 63 | X("test", "Test scheme for clangd lit tests."); |
| 64 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 65 | SymbolKindBitset defaultSymbolKinds() { |
| 66 | SymbolKindBitset Defaults; |
| 67 | for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array); |
| 68 | ++I) |
| 69 | Defaults.set(I); |
| 70 | return Defaults; |
| 71 | } |
| 72 | |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 73 | CompletionItemKindBitset defaultCompletionItemKinds() { |
| 74 | CompletionItemKindBitset Defaults; |
| 75 | for (size_t I = CompletionItemKindMin; |
| 76 | I <= static_cast<size_t>(CompletionItemKind::Reference); ++I) |
| 77 | Defaults.set(I); |
| 78 | return Defaults; |
| 79 | } |
| 80 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 81 | } // namespace |
| 82 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 83 | void ClangdLSPServer::onInitialize(InitializeParams &Params) { |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 84 | if (Params.initializationOptions) |
| 85 | applyConfiguration(*Params.initializationOptions); |
| 86 | |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 87 | if (Params.rootUri && *Params.rootUri) |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 88 | Server->setRootPath(Params.rootUri->file()); |
Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 89 | else if (Params.rootPath && !Params.rootPath->empty()) |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 90 | Server->setRootPath(*Params.rootPath); |
Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 91 | |
| 92 | CCOpts.EnableSnippets = |
| 93 | Params.capabilities.textDocument.completion.completionItem.snippetSupport; |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 94 | DiagOpts.EmbedFixesInDiagnostics = |
| 95 | Params.capabilities.textDocument.publishDiagnostics.clangdFixSupport; |
Alex Lorenz | 0ce8a7a | 2018-08-22 20:30:06 +0000 | [diff] [blame] | 96 | DiagOpts.SendDiagnosticCategory = |
| 97 | Params.capabilities.textDocument.publishDiagnostics.categorySupport; |
Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 98 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 99 | if (Params.capabilities.workspace && Params.capabilities.workspace->symbol && |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 100 | Params.capabilities.workspace->symbol->symbolKind && |
| 101 | Params.capabilities.workspace->symbol->symbolKind->valueSet) { |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 102 | for (SymbolKind Kind : |
| 103 | *Params.capabilities.workspace->symbol->symbolKind->valueSet) { |
| 104 | SupportedSymbolKinds.set(static_cast<size_t>(Kind)); |
| 105 | } |
| 106 | } |
| 107 | |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 108 | if (Params.capabilities.textDocument.completion.completionItemKind && |
| 109 | Params.capabilities.textDocument.completion.completionItemKind->valueSet) |
| 110 | for (CompletionItemKind Kind : *Params.capabilities.textDocument.completion |
| 111 | .completionItemKind->valueSet) |
| 112 | SupportedCompletionItemKinds.set(static_cast<size_t>(Kind)); |
| 113 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 114 | reply(json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 115 | {{"capabilities", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 116 | json::Object{ |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 117 | {"textDocumentSync", (int)TextDocumentSyncKind::Incremental}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 118 | {"documentFormattingProvider", true}, |
| 119 | {"documentRangeFormattingProvider", true}, |
| 120 | {"documentOnTypeFormattingProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 121 | json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 122 | {"firstTriggerCharacter", "}"}, |
| 123 | {"moreTriggerCharacter", {}}, |
| 124 | }}, |
| 125 | {"codeActionProvider", true}, |
| 126 | {"completionProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 127 | json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 128 | {"resolveProvider", false}, |
| 129 | {"triggerCharacters", {".", ">", ":"}}, |
| 130 | }}, |
| 131 | {"signatureHelpProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 132 | json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 133 | {"triggerCharacters", {"(", ","}}, |
| 134 | }}, |
| 135 | {"definitionProvider", true}, |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 136 | {"documentHighlightProvider", true}, |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 137 | {"hoverProvider", true}, |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 138 | {"renameProvider", true}, |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 139 | {"documentSymbolProvider", true}, |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 140 | {"workspaceSymbolProvider", true}, |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 141 | {"referencesProvider", true}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 142 | {"executeCommandProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 143 | json::Object{ |
Eric Liu | 2c19053 | 2018-05-15 15:23:53 +0000 | [diff] [blame] | 144 | {"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 145 | }}, |
| 146 | }}}}); |
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::onShutdown(ShutdownParams &Params) { |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 150 | // Do essentially nothing, just say we're ready to exit. |
| 151 | ShutdownRequestReceived = true; |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 152 | reply(nullptr); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 153 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 154 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 155 | void ClangdLSPServer::onExit(ExitParams &Params) { IsDone = true; } |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 156 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 157 | void ClangdLSPServer::onDocumentDidOpen(DidOpenTextDocumentParams &Params) { |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 158 | PathRef File = Params.textDocument.uri.file(); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 159 | if (Params.metadata && !Params.metadata->extraFlags.empty()) |
| 160 | CDB.setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags)); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 161 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 162 | std::string &Contents = Params.textDocument.text; |
| 163 | |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 164 | DraftMgr.addDraft(File, Contents); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 165 | Server->addDocument(File, Contents, WantDiagnostics::Yes); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 168 | void ClangdLSPServer::onDocumentDidChange(DidChangeTextDocumentParams &Params) { |
Eric Liu | 51fed18 | 2018-02-22 18:40:39 +0000 | [diff] [blame] | 169 | auto WantDiags = WantDiagnostics::Auto; |
| 170 | if (Params.wantDiagnostics.hasValue()) |
| 171 | WantDiags = Params.wantDiagnostics.getValue() ? WantDiagnostics::Yes |
| 172 | : WantDiagnostics::No; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 173 | |
| 174 | PathRef File = Params.textDocument.uri.file(); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 175 | llvm::Expected<std::string> Contents = |
| 176 | DraftMgr.updateDraft(File, Params.contentChanges); |
| 177 | if (!Contents) { |
| 178 | // If this fails, we are most likely going to be not in sync anymore with |
| 179 | // the client. It is better to remove the draft and let further operations |
| 180 | // fail rather than giving wrong results. |
| 181 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 182 | Server->removeDocument(File); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 183 | CDB.invalidate(File); |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 184 | elog("Failed to update {0}: {1}", File, Contents.takeError()); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 187 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 188 | Server->addDocument(File, *Contents, WantDiags); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 191 | void ClangdLSPServer::onFileEvent(DidChangeWatchedFilesParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 192 | Server->onFileEvent(Params); |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 195 | void ClangdLSPServer::onCommand(ExecuteCommandParams &Params) { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 196 | auto ApplyEdit = [](WorkspaceEdit WE) { |
| 197 | ApplyWorkspaceEditParams Edit; |
| 198 | Edit.edit = std::move(WE); |
| 199 | // We don't need the response so id == 1 is OK. |
| 200 | // Ideally, we would wait for the response and if there is no error, we |
| 201 | // would reply success/failure to the original RPC. |
| 202 | call("workspace/applyEdit", Edit); |
| 203 | }; |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 204 | if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && |
| 205 | Params.workspaceEdit) { |
| 206 | // The flow for "apply-fix" : |
| 207 | // 1. We publish a diagnostic, including fixits |
| 208 | // 2. The user clicks on the diagnostic, the editor asks us for code actions |
| 209 | // 3. We send code actions, with the fixit embedded as context |
| 210 | // 4. The user selects the fixit, the editor asks us to apply it |
| 211 | // 5. We unwrap the changes and send them back to the editor |
| 212 | // 6. The editor applies the changes (applyEdit), and sends us a reply (but |
| 213 | // we ignore it) |
| 214 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 215 | reply("Fix applied."); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 216 | ApplyEdit(*Params.workspaceEdit); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 217 | } else { |
| 218 | // We should not get here because ExecuteCommandParams would not have |
| 219 | // parsed in the first place and this handler should not be called. But if |
| 220 | // more commands are added, this will be here has a safe guard. |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 221 | replyError( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 222 | ErrorCode::InvalidParams, |
Haojian Wu | 2375c92 | 2017-11-07 10:21:02 +0000 | [diff] [blame] | 223 | llvm::formatv("Unsupported command \"{0}\".", Params.command).str()); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 227 | void ClangdLSPServer::onWorkspaceSymbol(WorkspaceSymbolParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 228 | Server->workspaceSymbols( |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 229 | Params.query, CCOpts.Limit, |
| 230 | [this](llvm::Expected<std::vector<SymbolInformation>> Items) { |
| 231 | if (!Items) |
| 232 | return replyError(ErrorCode::InternalError, |
| 233 | llvm::toString(Items.takeError())); |
| 234 | for (auto &Sym : *Items) |
| 235 | Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds); |
| 236 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 237 | reply(json::Array(*Items)); |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 238 | }); |
| 239 | } |
| 240 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 241 | void ClangdLSPServer::onRename(RenameParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 242 | Path File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 243 | llvm::Optional<std::string> Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 244 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 245 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 246 | "onRename called for non-added file"); |
| 247 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 248 | Server->rename( |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 249 | File, Params.position, Params.newName, |
| 250 | [File, Code, |
| 251 | Params](llvm::Expected<std::vector<tooling::Replacement>> Replacements) { |
| 252 | if (!Replacements) |
| 253 | return replyError(ErrorCode::InternalError, |
| 254 | llvm::toString(Replacements.takeError())); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 255 | |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 256 | // Turn the replacements into the format specified by the Language |
| 257 | // Server Protocol. Fuse them into one big JSON array. |
| 258 | std::vector<TextEdit> Edits; |
| 259 | for (const auto &R : *Replacements) |
| 260 | Edits.push_back(replacementToEdit(*Code, R)); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 261 | WorkspaceEdit WE; |
| 262 | WE.changes = {{Params.textDocument.uri.uri(), Edits}}; |
| 263 | reply(WE); |
| 264 | }); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 267 | void ClangdLSPServer::onDocumentDidClose(DidCloseTextDocumentParams &Params) { |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 268 | PathRef File = Params.textDocument.uri.file(); |
| 269 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 270 | Server->removeDocument(File); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 271 | CDB.invalidate(File); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 274 | void ClangdLSPServer::onDocumentOnTypeFormatting( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 275 | DocumentOnTypeFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 276 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 277 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 278 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 279 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 280 | "onDocumentOnTypeFormatting called for non-added file"); |
| 281 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 282 | auto ReplacementsOrError = Server->formatOnType(*Code, File, Params.position); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 283 | if (ReplacementsOrError) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 284 | reply(json::Array(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 285 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 286 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 287 | llvm::toString(ReplacementsOrError.takeError())); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 290 | void ClangdLSPServer::onDocumentRangeFormatting( |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 291 | DocumentRangeFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 292 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 293 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 294 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 295 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 296 | "onDocumentRangeFormatting called for non-added file"); |
| 297 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 298 | auto ReplacementsOrError = Server->formatRange(*Code, File, Params.range); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 299 | if (ReplacementsOrError) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 300 | reply(json::Array(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 301 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 302 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 303 | llvm::toString(ReplacementsOrError.takeError())); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 306 | void ClangdLSPServer::onDocumentFormatting(DocumentFormattingParams &Params) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 307 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 308 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 309 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 310 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 311 | "onDocumentFormatting called for non-added file"); |
| 312 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 313 | auto ReplacementsOrError = Server->formatFile(*Code, File); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 314 | if (ReplacementsOrError) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 315 | reply(json::Array(replacementsToEdits(*Code, ReplacementsOrError.get()))); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 316 | else |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 317 | replyError(ErrorCode::UnknownErrorCode, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 318 | llvm::toString(ReplacementsOrError.takeError())); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 321 | void ClangdLSPServer::onDocumentSymbol(DocumentSymbolParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 322 | Server->documentSymbols( |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 323 | Params.textDocument.uri.file(), |
| 324 | [this](llvm::Expected<std::vector<SymbolInformation>> Items) { |
| 325 | if (!Items) |
| 326 | return replyError(ErrorCode::InvalidParams, |
| 327 | llvm::toString(Items.takeError())); |
| 328 | for (auto &Sym : *Items) |
| 329 | Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 330 | reply(json::Array(*Items)); |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 331 | }); |
| 332 | } |
| 333 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 334 | void ClangdLSPServer::onCodeAction(CodeActionParams &Params) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 335 | // We provide a code action for each diagnostic at the requested location |
| 336 | // which has FixIts available. |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 337 | auto Code = DraftMgr.getDraft(Params.textDocument.uri.file()); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 338 | if (!Code) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 339 | return replyError(ErrorCode::InvalidParams, |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 340 | "onCodeAction called for non-added file"); |
| 341 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 342 | json::Array Commands; |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 343 | for (Diagnostic &D : Params.context.diagnostics) { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 344 | for (auto &F : getFixes(Params.textDocument.uri.file(), D)) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 345 | WorkspaceEdit WE; |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 346 | std::vector<TextEdit> Edits(F.Edits.begin(), F.Edits.end()); |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 347 | WE.changes = {{Params.textDocument.uri.uri(), std::move(Edits)}}; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 348 | Commands.push_back(json::Object{ |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 349 | {"title", llvm::formatv("Apply fix: {0}", F.Message)}, |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 350 | {"command", ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}, |
| 351 | {"arguments", {WE}}, |
| 352 | }); |
| 353 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 354 | } |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 355 | reply(std::move(Commands)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 358 | void ClangdLSPServer::onCompletion(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 359 | Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, |
| 360 | [this](llvm::Expected<CodeCompleteResult> List) { |
| 361 | if (!List) |
| 362 | return replyError(List.takeError()); |
| 363 | CompletionList LSPList; |
| 364 | LSPList.isIncomplete = List->HasMore; |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 365 | for (const auto &R : List->Completions) { |
| 366 | CompletionItem C = R.render(CCOpts); |
| 367 | C.kind = adjustKindToCapability( |
| 368 | C.kind, SupportedCompletionItemKinds); |
| 369 | LSPList.items.push_back(std::move(C)); |
| 370 | } |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 371 | return reply(std::move(LSPList)); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 372 | }); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 375 | void ClangdLSPServer::onSignatureHelp(TextDocumentPositionParams &Params) { |
| 376 | Server->signatureHelp(Params.textDocument.uri.file(), Params.position, |
| 377 | [](llvm::Expected<SignatureHelp> SignatureHelp) { |
| 378 | if (!SignatureHelp) |
| 379 | return replyError( |
| 380 | ErrorCode::InvalidParams, |
| 381 | llvm::toString(SignatureHelp.takeError())); |
| 382 | reply(*SignatureHelp); |
| 383 | }); |
| 384 | } |
| 385 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 386 | void ClangdLSPServer::onGoToDefinition(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 387 | Server->findDefinitions(Params.textDocument.uri.file(), Params.position, |
| 388 | [](llvm::Expected<std::vector<Location>> Items) { |
| 389 | if (!Items) |
| 390 | return replyError( |
| 391 | ErrorCode::InvalidParams, |
| 392 | llvm::toString(Items.takeError())); |
| 393 | reply(json::Array(*Items)); |
| 394 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 397 | void ClangdLSPServer::onSwitchSourceHeader(TextDocumentIdentifier &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 398 | llvm::Optional<Path> Result = Server->switchSourceHeader(Params.uri.file()); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 399 | reply(Result ? URI::createFile(*Result).toString() : ""); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 402 | void ClangdLSPServer::onDocumentHighlight(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 403 | Server->findDocumentHighlights( |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 404 | Params.textDocument.uri.file(), Params.position, |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 405 | [](llvm::Expected<std::vector<DocumentHighlight>> Highlights) { |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 406 | if (!Highlights) |
| 407 | return replyError(ErrorCode::InternalError, |
| 408 | llvm::toString(Highlights.takeError())); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 409 | reply(json::Array(*Highlights)); |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 410 | }); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 413 | void ClangdLSPServer::onHover(TextDocumentPositionParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 414 | Server->findHover(Params.textDocument.uri.file(), Params.position, |
| 415 | [](llvm::Expected<llvm::Optional<Hover>> H) { |
| 416 | if (!H) { |
| 417 | replyError(ErrorCode::InternalError, |
| 418 | llvm::toString(H.takeError())); |
| 419 | return; |
| 420 | } |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 421 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 422 | reply(*H); |
| 423 | }); |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 426 | void ClangdLSPServer::applyConfiguration( |
| 427 | const ClangdConfigurationParamsChange &Settings) { |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 428 | // Compilation database change. |
| 429 | if (Settings.compilationDatabasePath.hasValue()) { |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 430 | CDB.setCompileCommandsDir(Settings.compilationDatabasePath.getValue()); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 431 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 432 | reparseOpenedFiles(); |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 433 | } |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 434 | |
| 435 | // Update to the compilation database. |
| 436 | if (Settings.compilationDatabaseChanges) { |
| 437 | const auto &CompileCommandUpdates = *Settings.compilationDatabaseChanges; |
| 438 | bool ShouldReparseOpenFiles = false; |
| 439 | for (auto &Entry : CompileCommandUpdates) { |
| 440 | /// The opened files need to be reparsed only when some existing |
| 441 | /// entries are changed. |
| 442 | PathRef File = Entry.first; |
| 443 | if (!CDB.setCompilationCommandForFile( |
| 444 | File, tooling::CompileCommand( |
| 445 | std::move(Entry.second.workingDirectory), File, |
| 446 | std::move(Entry.second.compilationCommand), |
| 447 | /*Output=*/""))) |
| 448 | ShouldReparseOpenFiles = true; |
| 449 | } |
| 450 | if (ShouldReparseOpenFiles) |
| 451 | reparseOpenedFiles(); |
| 452 | } |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 455 | // FIXME: This function needs to be properly tested. |
| 456 | void ClangdLSPServer::onChangeConfiguration( |
| 457 | DidChangeConfigurationParams &Params) { |
| 458 | applyConfiguration(Params.settings); |
| 459 | } |
| 460 | |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 461 | void ClangdLSPServer::onReference(ReferenceParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 462 | Server->findReferences(Params.textDocument.uri.file(), Params.position, |
| 463 | [](llvm::Expected<std::vector<Location>> Locations) { |
| 464 | if (!Locations) |
| 465 | return replyError( |
| 466 | ErrorCode::InternalError, |
| 467 | llvm::toString(Locations.takeError())); |
| 468 | reply(llvm::json::Array(*Locations)); |
| 469 | }); |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 472 | ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 473 | const clangd::CodeCompleteOptions &CCOpts, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 474 | llvm::Optional<Path> CompileCommandsDir, |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 475 | bool ShouldUseInMemoryCDB, |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 476 | const ClangdServer::Options &Opts) |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 477 | : Out(Out), CDB(ShouldUseInMemoryCDB ? CompilationDB::makeInMemory() |
| 478 | : CompilationDB::makeDirectoryBased( |
| 479 | std::move(CompileCommandsDir))), |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 480 | CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 481 | SupportedCompletionItemKinds(defaultCompletionItemKinds()), |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 482 | Server(new ClangdServer(CDB.getCDB(), FSProvider, /*DiagConsumer=*/*this, |
| 483 | Opts)) {} |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 484 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 485 | bool ClangdLSPServer::run(std::FILE *In, JSONStreamStyle InputStyle) { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 486 | assert(!IsDone && "Run was called before"); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 487 | assert(Server); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 488 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 489 | // Set up JSONRPCDispatcher. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 490 | JSONRPCDispatcher Dispatcher([](const json::Value &Params) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 491 | replyError(ErrorCode::MethodNotFound, "method not found"); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 492 | }); |
Simon Marchi | 6e8eb9d | 2018-03-07 21:47:25 +0000 | [diff] [blame] | 493 | registerCallbackHandlers(Dispatcher, /*Callbacks=*/*this); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 494 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 495 | // Run the Language Server loop. |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 496 | runLanguageServerLoop(In, Out, InputStyle, Dispatcher, IsDone); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 497 | |
| 498 | // Make sure IsDone is set to true after this method exits to ensure assertion |
| 499 | // at the start of the method fires if it's ever executed again. |
| 500 | IsDone = true; |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 501 | // Destroy ClangdServer to ensure all worker threads finish. |
| 502 | Server.reset(); |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 503 | |
| 504 | return ShutdownRequestReceived; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 507 | std::vector<Fix> ClangdLSPServer::getFixes(StringRef File, |
| 508 | const clangd::Diagnostic &D) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 509 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 510 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 511 | if (DiagToFixItsIter == FixItsMap.end()) |
| 512 | return {}; |
| 513 | |
| 514 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 515 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 516 | if (FixItsIter == DiagToFixItsMap.end()) |
| 517 | return {}; |
| 518 | |
| 519 | return FixItsIter->second; |
| 520 | } |
| 521 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 522 | void ClangdLSPServer::onDiagnosticsReady(PathRef File, |
| 523 | std::vector<Diag> Diagnostics) { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 524 | json::Array DiagnosticsJSON; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 525 | |
| 526 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 527 | for (auto &Diag : Diagnostics) { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 528 | toLSPDiags(Diag, [&](clangd::Diagnostic Diag, llvm::ArrayRef<Fix> Fixes) { |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 529 | json::Object LSPDiag({ |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 530 | {"range", Diag.range}, |
| 531 | {"severity", Diag.severity}, |
| 532 | {"message", Diag.message}, |
| 533 | }); |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 534 | // LSP extension: embed the fixes in the diagnostic. |
| 535 | if (DiagOpts.EmbedFixesInDiagnostics && !Fixes.empty()) { |
| 536 | json::Array ClangdFixes; |
| 537 | for (const auto &Fix : Fixes) { |
| 538 | WorkspaceEdit WE; |
| 539 | URIForFile URI{File}; |
| 540 | WE.changes = {{URI.uri(), std::vector<TextEdit>(Fix.Edits.begin(), |
| 541 | Fix.Edits.end())}}; |
| 542 | ClangdFixes.push_back( |
| 543 | json::Object{{"edit", toJSON(WE)}, {"title", Fix.Message}}); |
| 544 | } |
| 545 | LSPDiag["clangd_fixes"] = std::move(ClangdFixes); |
| 546 | } |
Alex Lorenz | 0ce8a7a | 2018-08-22 20:30:06 +0000 | [diff] [blame] | 547 | if (DiagOpts.SendDiagnosticCategory && !Diag.category.empty()) |
Alex Lorenz | 3714643 | 2018-08-14 22:21:40 +0000 | [diff] [blame] | 548 | LSPDiag["category"] = Diag.category; |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 549 | DiagnosticsJSON.push_back(std::move(LSPDiag)); |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 550 | |
| 551 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame^] | 552 | llvm::copy(Fixes, std::back_inserter(FixItsForDiagnostic)); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 553 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // Cache FixIts |
| 557 | { |
| 558 | // FIXME(ibiryukov): should be deleted when documents are removed |
| 559 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 560 | FixItsMap[File] = LocalFixIts; |
| 561 | } |
| 562 | |
| 563 | // Publish diagnostics. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 564 | Out.writeMessage(json::Object{ |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 565 | {"jsonrpc", "2.0"}, |
| 566 | {"method", "textDocument/publishDiagnostics"}, |
| 567 | {"params", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 568 | json::Object{ |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 569 | {"uri", URIForFile{File}}, |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 570 | {"diagnostics", std::move(DiagnosticsJSON)}, |
| 571 | }}, |
| 572 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 573 | } |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 574 | |
| 575 | void ClangdLSPServer::reparseOpenedFiles() { |
| 576 | for (const Path &FilePath : DraftMgr.getActiveFiles()) |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 577 | Server->addDocument(FilePath, *DraftMgr.getDraft(FilePath), |
| 578 | WantDiagnostics::Auto); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 579 | } |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 580 | |
| 581 | ClangdLSPServer::CompilationDB ClangdLSPServer::CompilationDB::makeInMemory() { |
| 582 | return CompilationDB(llvm::make_unique<InMemoryCompilationDb>(), nullptr, |
| 583 | /*IsDirectoryBased=*/false); |
| 584 | } |
| 585 | |
| 586 | ClangdLSPServer::CompilationDB |
| 587 | ClangdLSPServer::CompilationDB::makeDirectoryBased( |
| 588 | llvm::Optional<Path> CompileCommandsDir) { |
| 589 | auto CDB = llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>( |
| 590 | std::move(CompileCommandsDir)); |
| 591 | auto CachingCDB = llvm::make_unique<CachingCompilationDb>(*CDB); |
| 592 | return CompilationDB(std::move(CDB), std::move(CachingCDB), |
| 593 | /*IsDirectoryBased=*/true); |
| 594 | } |
| 595 | |
| 596 | void ClangdLSPServer::CompilationDB::invalidate(PathRef File) { |
| 597 | if (!IsDirectoryBased) |
| 598 | static_cast<InMemoryCompilationDb *>(CDB.get())->invalidate(File); |
| 599 | else |
| 600 | CachingCDB->invalidate(File); |
| 601 | } |
| 602 | |
| 603 | bool ClangdLSPServer::CompilationDB::setCompilationCommandForFile( |
| 604 | PathRef File, tooling::CompileCommand CompilationCommand) { |
| 605 | if (IsDirectoryBased) { |
| 606 | elog("Trying to set compile command for {0} while using directory-based " |
| 607 | "compilation database", |
| 608 | File); |
| 609 | return false; |
| 610 | } |
| 611 | return static_cast<InMemoryCompilationDb *>(CDB.get()) |
| 612 | ->setCompilationCommandForFile(File, std::move(CompilationCommand)); |
| 613 | } |
| 614 | |
| 615 | void ClangdLSPServer::CompilationDB::setExtraFlagsForFile( |
| 616 | PathRef File, std::vector<std::string> ExtraFlags) { |
| 617 | if (!IsDirectoryBased) { |
| 618 | elog("Trying to set extra flags for {0} while using in-memory compilation " |
| 619 | "database", |
| 620 | File); |
| 621 | return; |
| 622 | } |
| 623 | static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get()) |
| 624 | ->setExtraFlagsForFile(File, std::move(ExtraFlags)); |
| 625 | CachingCDB->invalidate(File); |
| 626 | } |
| 627 | |
| 628 | void ClangdLSPServer::CompilationDB::setCompileCommandsDir(Path P) { |
| 629 | if (!IsDirectoryBased) { |
| 630 | elog("Trying to set compile commands dir while using in-memory compilation " |
| 631 | "database"); |
| 632 | return; |
| 633 | } |
| 634 | static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get()) |
| 635 | ->setCompileCommandsDir(P); |
| 636 | CachingCDB->clear(); |
| 637 | } |
| 638 | |
| 639 | GlobalCompilationDatabase &ClangdLSPServer::CompilationDB::getCDB() { |
| 640 | if (CachingCDB) |
| 641 | return *CachingCDB; |
| 642 | return *CDB; |
| 643 | } |