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