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