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" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 12 | #include "SourceCode.h" |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 13 | #include "Trace.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" |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ScopedPrinter.h" |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 20 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 22 | namespace clang { |
| 23 | namespace clangd { |
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: |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 32 | Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body, |
| 33 | StringRef /*HintPath*/) const override { |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 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("/")) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 38 | return make_error<StringError>( |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 39 | "Expect URI body to be an absolute path starting with '/': " + Body, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 40 | inconvertibleErrorCode()); |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 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 |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 47 | SmallVector<char, 16> Path(Body.begin(), Body.end()); |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 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 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 55 | Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override { |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 56 | llvm_unreachable("Clangd must never create a test URI."); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | static URISchemeRegistry::Add<TestScheme> |
| 61 | X("test", "Test scheme for clangd lit tests."); |
| 62 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 63 | SymbolKindBitset defaultSymbolKinds() { |
| 64 | SymbolKindBitset Defaults; |
| 65 | for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array); |
| 66 | ++I) |
| 67 | Defaults.set(I); |
| 68 | return Defaults; |
| 69 | } |
| 70 | |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 71 | CompletionItemKindBitset defaultCompletionItemKinds() { |
| 72 | CompletionItemKindBitset Defaults; |
| 73 | for (size_t I = CompletionItemKindMin; |
| 74 | I <= static_cast<size_t>(CompletionItemKind::Reference); ++I) |
| 75 | Defaults.set(I); |
| 76 | return Defaults; |
| 77 | } |
| 78 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 79 | } // namespace |
| 80 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 81 | // MessageHandler dispatches incoming LSP messages. |
| 82 | // It handles cross-cutting concerns: |
| 83 | // - serializes/deserializes protocol objects to JSON |
| 84 | // - logging of inbound messages |
| 85 | // - cancellation handling |
| 86 | // - basic call tracing |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 87 | // MessageHandler ensures that initialize() is called before any other handler. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 88 | class ClangdLSPServer::MessageHandler : public Transport::MessageHandler { |
| 89 | public: |
| 90 | MessageHandler(ClangdLSPServer &Server) : Server(Server) {} |
| 91 | |
| 92 | bool onNotify(StringRef Method, json::Value Params) override { |
| 93 | log("<-- {0}", Method); |
| 94 | if (Method == "exit") |
| 95 | return false; |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 96 | if (!Server.Server) |
| 97 | elog("Notification {0} before initialization", Method); |
| 98 | else if (Method == "$/cancelRequest") |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 99 | onCancel(std::move(Params)); |
| 100 | else if (auto Handler = Notifications.lookup(Method)) |
| 101 | Handler(std::move(Params)); |
| 102 | else |
| 103 | log("unhandled notification {0}", Method); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool onCall(StringRef Method, json::Value Params, json::Value ID) override { |
| 108 | log("<-- {0}({1})", Method, ID); |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 109 | if (!Server.Server && Method != "initialize") { |
| 110 | elog("Call {0} before initialization.", Method); |
| 111 | Server.reply(ID, make_error<LSPError>("server not initialized", |
| 112 | ErrorCode::ServerNotInitialized)); |
| 113 | } else if (auto Handler = Calls.lookup(Method)) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 114 | Handler(std::move(Params), std::move(ID)); |
| 115 | else |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 116 | Server.reply(ID, make_error<LSPError>("method not found", |
| 117 | ErrorCode::MethodNotFound)); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | bool onReply(json::Value ID, Expected<json::Value> Result) override { |
| 122 | // We ignore replies, just log them. |
| 123 | if (Result) |
| 124 | log("<-- reply({0})", ID); |
| 125 | else |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 126 | log("<-- reply({0}) error: {1}", ID, toString(Result.takeError())); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | // Bind an LSP method name to a call. |
| 131 | template <typename Param, typename Reply> |
| 132 | void bind(const char *Method, |
| 133 | void (ClangdLSPServer::*Handler)(const Param &, Callback<Reply>)) { |
| 134 | Calls[Method] = [Method, Handler, this](json::Value RawParams, |
| 135 | json::Value ID) { |
| 136 | Param P; |
| 137 | if (!fromJSON(RawParams, P)) { |
| 138 | elog("Failed to decode {0} request.", Method); |
| 139 | Server.reply(ID, make_error<LSPError>("failed to decode request", |
| 140 | ErrorCode::InvalidRequest)); |
| 141 | return; |
| 142 | } |
| 143 | trace::Span Tracer(Method); |
| 144 | SPAN_ATTACH(Tracer, "Params", RawParams); |
| 145 | auto *Trace = Tracer.Args; // We attach reply from another thread. |
| 146 | // Calls can be canceled by the client. Add cancellation context. |
| 147 | WithContext WithCancel(cancelableRequestContext(ID)); |
| 148 | // FIXME: this function should assert it's called exactly once. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 149 | (Server.*Handler)(P, [this, ID, Trace](Expected<Reply> Result) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 150 | if (Result) { |
| 151 | if (Trace) |
| 152 | (*Trace)["Reply"] = *Result; |
| 153 | Server.reply(ID, json::Value(std::move(*Result))); |
| 154 | } else { |
| 155 | auto Err = Result.takeError(); |
| 156 | if (Trace) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 157 | (*Trace)["Error"] = to_string(Err); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 158 | Server.reply(ID, std::move(Err)); |
| 159 | } |
| 160 | }); |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | // Bind an LSP method name to a notification. |
| 165 | template <typename Param> |
| 166 | void bind(const char *Method, |
| 167 | void (ClangdLSPServer::*Handler)(const Param &)) { |
| 168 | Notifications[Method] = [Method, Handler, this](json::Value RawParams) { |
| 169 | Param P; |
| 170 | if (!fromJSON(RawParams, P)) { |
| 171 | elog("Failed to decode {0} request.", Method); |
| 172 | return; |
| 173 | } |
| 174 | trace::Span Tracer(Method); |
| 175 | SPAN_ATTACH(Tracer, "Params", RawParams); |
| 176 | (Server.*Handler)(P); |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | private: |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 181 | StringMap<std::function<void(json::Value)>> Notifications; |
| 182 | StringMap<std::function<void(json::Value, json::Value)>> Calls; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 183 | |
| 184 | // Method calls may be cancelled by ID, so keep track of their state. |
| 185 | // This needs a mutex: handlers may finish on a different thread, and that's |
| 186 | // when we clean up entries in the map. |
| 187 | mutable std::mutex RequestCancelersMutex; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 188 | StringMap<std::pair<Canceler, /*Cookie*/ unsigned>> RequestCancelers; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 189 | unsigned NextRequestCookie = 0; // To disambiguate reused IDs, see below. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 190 | void onCancel(const json::Value &Params) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 191 | const json::Value *ID = nullptr; |
| 192 | if (auto *O = Params.getAsObject()) |
| 193 | ID = O->get("id"); |
| 194 | if (!ID) { |
| 195 | elog("Bad cancellation request: {0}", Params); |
| 196 | return; |
| 197 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 198 | auto StrID = to_string(*ID); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 199 | std::lock_guard<std::mutex> Lock(RequestCancelersMutex); |
| 200 | auto It = RequestCancelers.find(StrID); |
| 201 | if (It != RequestCancelers.end()) |
| 202 | It->second.first(); // Invoke the canceler. |
| 203 | } |
| 204 | // We run cancelable requests in a context that does two things: |
| 205 | // - allows cancellation using RequestCancelers[ID] |
| 206 | // - cleans up the entry in RequestCancelers when it's no longer needed |
| 207 | // If a client reuses an ID, the last wins and the first cannot be canceled. |
| 208 | Context cancelableRequestContext(const json::Value &ID) { |
| 209 | auto Task = cancelableTask(); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 210 | auto StrID = to_string(ID); // JSON-serialize ID for map key. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 211 | auto Cookie = NextRequestCookie++; // No lock, only called on main thread. |
| 212 | { |
| 213 | std::lock_guard<std::mutex> Lock(RequestCancelersMutex); |
| 214 | RequestCancelers[StrID] = {std::move(Task.second), Cookie}; |
| 215 | } |
| 216 | // When the request ends, we can clean up the entry we just added. |
| 217 | // The cookie lets us check that it hasn't been overwritten due to ID |
| 218 | // reuse. |
| 219 | return Task.first.derive(make_scope_exit([this, StrID, Cookie] { |
| 220 | std::lock_guard<std::mutex> Lock(RequestCancelersMutex); |
| 221 | auto It = RequestCancelers.find(StrID); |
| 222 | if (It != RequestCancelers.end() && It->second.second == Cookie) |
| 223 | RequestCancelers.erase(It); |
| 224 | })); |
| 225 | } |
| 226 | |
| 227 | ClangdLSPServer &Server; |
| 228 | }; |
| 229 | |
| 230 | // call(), notify(), and reply() wrap the Transport, adding logging and locking. |
| 231 | void ClangdLSPServer::call(StringRef Method, json::Value Params) { |
| 232 | auto ID = NextCallID++; |
| 233 | log("--> {0}({1})", Method, ID); |
| 234 | // We currently don't handle responses, so no need to store ID anywhere. |
| 235 | std::lock_guard<std::mutex> Lock(TranspWriter); |
| 236 | Transp.call(Method, std::move(Params), ID); |
| 237 | } |
| 238 | |
| 239 | void ClangdLSPServer::notify(StringRef Method, json::Value Params) { |
| 240 | log("--> {0}", Method); |
| 241 | std::lock_guard<std::mutex> Lock(TranspWriter); |
| 242 | Transp.notify(Method, std::move(Params)); |
| 243 | } |
| 244 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 245 | void ClangdLSPServer::reply(json::Value ID, Expected<json::Value> Result) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 246 | if (Result) { |
| 247 | log("--> reply({0})", ID); |
| 248 | std::lock_guard<std::mutex> Lock(TranspWriter); |
| 249 | Transp.reply(std::move(ID), std::move(Result)); |
| 250 | } else { |
| 251 | Error Err = Result.takeError(); |
| 252 | log("--> reply({0}) error: {1}", ID, Err); |
| 253 | std::lock_guard<std::mutex> Lock(TranspWriter); |
| 254 | Transp.reply(std::move(ID), std::move(Err)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void ClangdLSPServer::onInitialize(const InitializeParams &Params, |
| 259 | Callback<json::Value> Reply) { |
Sam McCall | 0d9b40f | 2018-10-19 15:42:23 +0000 | [diff] [blame] | 260 | if (Params.rootUri && *Params.rootUri) |
| 261 | ClangdServerOpts.WorkspaceRoot = Params.rootUri->file(); |
| 262 | else if (Params.rootPath && !Params.rootPath->empty()) |
| 263 | ClangdServerOpts.WorkspaceRoot = *Params.rootPath; |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 264 | if (Server) |
| 265 | return Reply(make_error<LSPError>("server already initialized", |
| 266 | ErrorCode::InvalidRequest)); |
| 267 | Server.emplace(CDB.getCDB(), FSProvider, |
| 268 | static_cast<DiagnosticsConsumer &>(*this), ClangdServerOpts); |
Simon Marchi | abeed66 | 2018-10-16 15:55:03 +0000 | [diff] [blame] | 269 | if (Params.initializationOptions) { |
| 270 | const ClangdInitializationOptions &Opts = *Params.initializationOptions; |
| 271 | |
| 272 | // Explicit compilation database path. |
| 273 | if (Opts.compilationDatabasePath.hasValue()) { |
| 274 | CDB.setCompileCommandsDir(Opts.compilationDatabasePath.getValue()); |
| 275 | } |
| 276 | |
| 277 | applyConfiguration(Opts.ParamsChange); |
| 278 | } |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 279 | |
Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 280 | CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets; |
| 281 | DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes; |
| 282 | DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory; |
| 283 | if (Params.capabilities.WorkspaceSymbolKinds) |
| 284 | SupportedSymbolKinds |= *Params.capabilities.WorkspaceSymbolKinds; |
| 285 | if (Params.capabilities.CompletionItemKinds) |
| 286 | SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds; |
| 287 | SupportsCodeAction = Params.capabilities.CodeActionStructure; |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 288 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 289 | Reply(json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 290 | {{"capabilities", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 291 | json::Object{ |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 292 | {"textDocumentSync", (int)TextDocumentSyncKind::Incremental}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 293 | {"documentFormattingProvider", true}, |
| 294 | {"documentRangeFormattingProvider", true}, |
| 295 | {"documentOnTypeFormattingProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 296 | json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 297 | {"firstTriggerCharacter", "}"}, |
| 298 | {"moreTriggerCharacter", {}}, |
| 299 | }}, |
| 300 | {"codeActionProvider", true}, |
| 301 | {"completionProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 302 | json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 303 | {"resolveProvider", false}, |
| 304 | {"triggerCharacters", {".", ">", ":"}}, |
| 305 | }}, |
| 306 | {"signatureHelpProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 307 | json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 308 | {"triggerCharacters", {"(", ","}}, |
| 309 | }}, |
| 310 | {"definitionProvider", true}, |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 311 | {"documentHighlightProvider", true}, |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 312 | {"hoverProvider", true}, |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 313 | {"renameProvider", true}, |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 314 | {"documentSymbolProvider", true}, |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 315 | {"workspaceSymbolProvider", true}, |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 316 | {"referencesProvider", true}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 317 | {"executeCommandProvider", |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 318 | json::Object{ |
Eric Liu | 2c19053 | 2018-05-15 15:23:53 +0000 | [diff] [blame] | 319 | {"commands", {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND}}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 320 | }}, |
| 321 | }}}}); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 324 | void ClangdLSPServer::onShutdown(const ShutdownParams &Params, |
| 325 | Callback<std::nullptr_t> Reply) { |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 326 | // Do essentially nothing, just say we're ready to exit. |
| 327 | ShutdownRequestReceived = true; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 328 | Reply(nullptr); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 329 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 330 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 331 | void ClangdLSPServer::onDocumentDidOpen( |
| 332 | const DidOpenTextDocumentParams &Params) { |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 333 | PathRef File = Params.textDocument.uri.file(); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 334 | if (Params.metadata && !Params.metadata->extraFlags.empty()) |
| 335 | CDB.setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags)); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 336 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 337 | const std::string &Contents = Params.textDocument.text; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 338 | |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 339 | DraftMgr.addDraft(File, Contents); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 340 | Server->addDocument(File, Contents, WantDiagnostics::Yes); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 343 | void ClangdLSPServer::onDocumentDidChange( |
| 344 | const DidChangeTextDocumentParams &Params) { |
Eric Liu | 51fed18 | 2018-02-22 18:40:39 +0000 | [diff] [blame] | 345 | auto WantDiags = WantDiagnostics::Auto; |
| 346 | if (Params.wantDiagnostics.hasValue()) |
| 347 | WantDiags = Params.wantDiagnostics.getValue() ? WantDiagnostics::Yes |
| 348 | : WantDiagnostics::No; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 349 | |
| 350 | PathRef File = Params.textDocument.uri.file(); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 351 | Expected<std::string> Contents = |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 352 | DraftMgr.updateDraft(File, Params.contentChanges); |
| 353 | if (!Contents) { |
| 354 | // If this fails, we are most likely going to be not in sync anymore with |
| 355 | // the client. It is better to remove the draft and let further operations |
| 356 | // fail rather than giving wrong results. |
| 357 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 358 | Server->removeDocument(File); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 359 | CDB.invalidate(File); |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 360 | elog("Failed to update {0}: {1}", File, Contents.takeError()); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 361 | return; |
| 362 | } |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 363 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 364 | Server->addDocument(File, *Contents, WantDiags); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 367 | void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 368 | Server->onFileEvent(Params); |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 371 | void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params, |
| 372 | Callback<json::Value> Reply) { |
| 373 | auto ApplyEdit = [&](WorkspaceEdit WE) { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 374 | ApplyWorkspaceEditParams Edit; |
| 375 | Edit.edit = std::move(WE); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 376 | // Ideally, we would wait for the response and if there is no error, we |
| 377 | // would reply success/failure to the original RPC. |
| 378 | call("workspace/applyEdit", Edit); |
| 379 | }; |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 380 | if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && |
| 381 | Params.workspaceEdit) { |
| 382 | // The flow for "apply-fix" : |
| 383 | // 1. We publish a diagnostic, including fixits |
| 384 | // 2. The user clicks on the diagnostic, the editor asks us for code actions |
| 385 | // 3. We send code actions, with the fixit embedded as context |
| 386 | // 4. The user selects the fixit, the editor asks us to apply it |
| 387 | // 5. We unwrap the changes and send them back to the editor |
| 388 | // 6. The editor applies the changes (applyEdit), and sends us a reply (but |
| 389 | // we ignore it) |
| 390 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 391 | Reply("Fix applied."); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 392 | ApplyEdit(*Params.workspaceEdit); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 393 | } else { |
| 394 | // We should not get here because ExecuteCommandParams would not have |
| 395 | // parsed in the first place and this handler should not be called. But if |
| 396 | // more commands are added, this will be here has a safe guard. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 397 | Reply(make_error<LSPError>( |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 398 | formatv("Unsupported command \"{0}\".", Params.command).str(), |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 399 | ErrorCode::InvalidParams)); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 403 | void ClangdLSPServer::onWorkspaceSymbol( |
| 404 | const WorkspaceSymbolParams &Params, |
| 405 | Callback<std::vector<SymbolInformation>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 406 | Server->workspaceSymbols( |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 407 | Params.query, CCOpts.Limit, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 408 | Bind( |
| 409 | [this](decltype(Reply) Reply, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 410 | Expected<std::vector<SymbolInformation>> Items) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 411 | if (!Items) |
| 412 | return Reply(Items.takeError()); |
| 413 | for (auto &Sym : *Items) |
| 414 | Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds); |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 415 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 416 | Reply(std::move(*Items)); |
| 417 | }, |
| 418 | std::move(Reply))); |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 421 | void ClangdLSPServer::onRename(const RenameParams &Params, |
| 422 | Callback<WorkspaceEdit> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 423 | Path File = Params.textDocument.uri.file(); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 424 | Optional<std::string> Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 425 | if (!Code) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 426 | return Reply(make_error<LSPError>("onRename called for non-added file", |
| 427 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 428 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 429 | Server->rename( |
Ilya Biryukov | 2c5e8e8 | 2018-02-15 13:15:47 +0000 | [diff] [blame] | 430 | File, Params.position, Params.newName, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 431 | Bind( |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 432 | [File, Code, |
| 433 | Params](decltype(Reply) Reply, |
| 434 | Expected<std::vector<tooling::Replacement>> Replacements) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 435 | if (!Replacements) |
| 436 | return Reply(Replacements.takeError()); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 437 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 438 | // Turn the replacements into the format specified by the Language |
| 439 | // Server Protocol. Fuse them into one big JSON array. |
| 440 | std::vector<TextEdit> Edits; |
| 441 | for (const auto &R : *Replacements) |
| 442 | Edits.push_back(replacementToEdit(*Code, R)); |
| 443 | WorkspaceEdit WE; |
| 444 | WE.changes = {{Params.textDocument.uri.uri(), Edits}}; |
| 445 | Reply(WE); |
| 446 | }, |
| 447 | std::move(Reply))); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 450 | void ClangdLSPServer::onDocumentDidClose( |
| 451 | const DidCloseTextDocumentParams &Params) { |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 452 | PathRef File = Params.textDocument.uri.file(); |
| 453 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 454 | Server->removeDocument(File); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 455 | CDB.invalidate(File); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 458 | void ClangdLSPServer::onDocumentOnTypeFormatting( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 459 | const DocumentOnTypeFormattingParams &Params, |
| 460 | Callback<std::vector<TextEdit>> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 461 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 462 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 463 | if (!Code) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 464 | return Reply(make_error<LSPError>( |
| 465 | "onDocumentOnTypeFormatting called for non-added file", |
| 466 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 467 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 468 | auto ReplacementsOrError = Server->formatOnType(*Code, File, Params.position); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 469 | if (ReplacementsOrError) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 470 | Reply(replacementsToEdits(*Code, ReplacementsOrError.get())); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 471 | else |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 472 | Reply(ReplacementsOrError.takeError()); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 475 | void ClangdLSPServer::onDocumentRangeFormatting( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 476 | const DocumentRangeFormattingParams &Params, |
| 477 | Callback<std::vector<TextEdit>> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 478 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 479 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 480 | if (!Code) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 481 | return Reply(make_error<LSPError>( |
| 482 | "onDocumentRangeFormatting called for non-added file", |
| 483 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 484 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 485 | auto ReplacementsOrError = Server->formatRange(*Code, File, Params.range); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 486 | if (ReplacementsOrError) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 487 | Reply(replacementsToEdits(*Code, ReplacementsOrError.get())); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 488 | else |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 489 | Reply(ReplacementsOrError.takeError()); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 492 | void ClangdLSPServer::onDocumentFormatting( |
| 493 | const DocumentFormattingParams &Params, |
| 494 | Callback<std::vector<TextEdit>> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 495 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 496 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 497 | if (!Code) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 498 | return Reply( |
| 499 | make_error<LSPError>("onDocumentFormatting called for non-added file", |
| 500 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 501 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 502 | auto ReplacementsOrError = Server->formatFile(*Code, File); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 503 | if (ReplacementsOrError) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 504 | Reply(replacementsToEdits(*Code, ReplacementsOrError.get())); |
Raoul Wols | 212bcf8 | 2017-12-12 20:25:06 +0000 | [diff] [blame] | 505 | else |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 506 | Reply(ReplacementsOrError.takeError()); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 509 | void ClangdLSPServer::onDocumentSymbol( |
| 510 | const DocumentSymbolParams &Params, |
| 511 | Callback<std::vector<SymbolInformation>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 512 | Server->documentSymbols( |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 513 | Params.textDocument.uri.file(), |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 514 | Bind( |
| 515 | [this](decltype(Reply) Reply, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 516 | Expected<std::vector<SymbolInformation>> Items) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 517 | if (!Items) |
| 518 | return Reply(Items.takeError()); |
| 519 | for (auto &Sym : *Items) |
| 520 | Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds); |
| 521 | Reply(std::move(*Items)); |
| 522 | }, |
| 523 | std::move(Reply))); |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 526 | static Optional<Command> asCommand(const CodeAction &Action) { |
| 527 | Command Cmd; |
| 528 | if (Action.command && Action.edit) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 529 | return None; // Not representable. (We never emit these anyway). |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 530 | if (Action.command) { |
| 531 | Cmd = *Action.command; |
| 532 | } else if (Action.edit) { |
| 533 | Cmd.command = Command::CLANGD_APPLY_FIX_COMMAND; |
| 534 | Cmd.workspaceEdit = *Action.edit; |
| 535 | } else { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 536 | return None; |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 537 | } |
| 538 | Cmd.title = Action.title; |
| 539 | if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) |
| 540 | Cmd.title = "Apply fix: " + Cmd.title; |
| 541 | return Cmd; |
| 542 | } |
| 543 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 544 | void ClangdLSPServer::onCodeAction(const CodeActionParams &Params, |
| 545 | Callback<json::Value> Reply) { |
| 546 | auto Code = DraftMgr.getDraft(Params.textDocument.uri.file()); |
| 547 | if (!Code) |
| 548 | return Reply(make_error<LSPError>("onCodeAction called for non-added file", |
| 549 | ErrorCode::InvalidParams)); |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 550 | // We provide a code action for Fixes on the specified diagnostics. |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 551 | std::vector<CodeAction> Actions; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 552 | for (const Diagnostic &D : Params.context.diagnostics) { |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 553 | for (auto &F : getFixes(Params.textDocument.uri.file(), D)) { |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 554 | Actions.emplace_back(); |
| 555 | Actions.back().title = F.Message; |
| 556 | Actions.back().kind = CodeAction::QUICKFIX_KIND; |
| 557 | Actions.back().diagnostics = {D}; |
| 558 | Actions.back().edit.emplace(); |
| 559 | Actions.back().edit->changes.emplace(); |
| 560 | (*Actions.back().edit->changes)[Params.textDocument.uri.uri()] = { |
| 561 | F.Edits.begin(), F.Edits.end()}; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 562 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 563 | } |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 564 | |
| 565 | if (SupportsCodeAction) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 566 | Reply(json::Array(Actions)); |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 567 | else { |
| 568 | std::vector<Command> Commands; |
| 569 | for (const auto &Action : Actions) |
| 570 | if (auto Command = asCommand(Action)) |
| 571 | Commands.push_back(std::move(*Command)); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 572 | Reply(json::Array(Commands)); |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 573 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 576 | void ClangdLSPServer::onCompletion(const TextDocumentPositionParams &Params, |
| 577 | Callback<CompletionList> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 578 | Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 579 | Bind( |
| 580 | [this](decltype(Reply) Reply, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 581 | Expected<CodeCompleteResult> List) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 582 | if (!List) |
| 583 | return Reply(List.takeError()); |
| 584 | CompletionList LSPList; |
| 585 | LSPList.isIncomplete = List->HasMore; |
| 586 | for (const auto &R : List->Completions) { |
| 587 | CompletionItem C = R.render(CCOpts); |
| 588 | C.kind = adjustKindToCapability( |
| 589 | C.kind, SupportedCompletionItemKinds); |
| 590 | LSPList.items.push_back(std::move(C)); |
| 591 | } |
| 592 | return Reply(std::move(LSPList)); |
| 593 | }, |
| 594 | std::move(Reply))); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 597 | void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params, |
| 598 | Callback<SignatureHelp> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 599 | Server->signatureHelp(Params.textDocument.uri.file(), Params.position, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 600 | std::move(Reply)); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 603 | void ClangdLSPServer::onGoToDefinition(const TextDocumentPositionParams &Params, |
| 604 | Callback<std::vector<Location>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 605 | Server->findDefinitions(Params.textDocument.uri.file(), Params.position, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 606 | std::move(Reply)); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 609 | void ClangdLSPServer::onSwitchSourceHeader(const TextDocumentIdentifier &Params, |
| 610 | Callback<std::string> Reply) { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 611 | Optional<Path> Result = Server->switchSourceHeader(Params.uri.file()); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 612 | Reply(Result ? URI::createFile(*Result).toString() : ""); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 615 | void ClangdLSPServer::onDocumentHighlight( |
| 616 | const TextDocumentPositionParams &Params, |
| 617 | Callback<std::vector<DocumentHighlight>> Reply) { |
| 618 | Server->findDocumentHighlights(Params.textDocument.uri.file(), |
| 619 | Params.position, std::move(Reply)); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 622 | void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 623 | Callback<Optional<Hover>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 624 | Server->findHover(Params.textDocument.uri.file(), Params.position, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 625 | std::move(Reply)); |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 628 | void ClangdLSPServer::applyConfiguration( |
Simon Marchi | abeed66 | 2018-10-16 15:55:03 +0000 | [diff] [blame] | 629 | const ClangdConfigurationParamsChange &Params) { |
| 630 | // Per-file update to the compilation database. |
| 631 | if (Params.compilationDatabaseChanges) { |
| 632 | const auto &CompileCommandUpdates = *Params.compilationDatabaseChanges; |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 633 | bool ShouldReparseOpenFiles = false; |
| 634 | for (auto &Entry : CompileCommandUpdates) { |
| 635 | /// The opened files need to be reparsed only when some existing |
| 636 | /// entries are changed. |
| 637 | PathRef File = Entry.first; |
| 638 | if (!CDB.setCompilationCommandForFile( |
| 639 | File, tooling::CompileCommand( |
| 640 | std::move(Entry.second.workingDirectory), File, |
| 641 | std::move(Entry.second.compilationCommand), |
| 642 | /*Output=*/""))) |
| 643 | ShouldReparseOpenFiles = true; |
| 644 | } |
| 645 | if (ShouldReparseOpenFiles) |
| 646 | reparseOpenedFiles(); |
| 647 | } |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 650 | // FIXME: This function needs to be properly tested. |
| 651 | void ClangdLSPServer::onChangeConfiguration( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 652 | const DidChangeConfigurationParams &Params) { |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 653 | applyConfiguration(Params.settings); |
| 654 | } |
| 655 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 656 | void ClangdLSPServer::onReference(const ReferenceParams &Params, |
| 657 | Callback<std::vector<Location>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 658 | Server->findReferences(Params.textDocument.uri.file(), Params.position, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 659 | std::move(Reply)); |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 662 | ClangdLSPServer::ClangdLSPServer(class Transport &Transp, |
Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 663 | const clangd::CodeCompleteOptions &CCOpts, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 664 | Optional<Path> CompileCommandsDir, |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 665 | bool ShouldUseInMemoryCDB, |
Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 666 | const ClangdServer::Options &Opts) |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 667 | : Transp(Transp), MsgHandler(new MessageHandler(*this)), |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 668 | CDB(ShouldUseInMemoryCDB ? CompilationDB::makeInMemory() |
| 669 | : CompilationDB::makeDirectoryBased( |
| 670 | std::move(CompileCommandsDir))), |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 671 | CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 672 | SupportedCompletionItemKinds(defaultCompletionItemKinds()), |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 673 | ClangdServerOpts(Opts) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 674 | // clang-format off |
| 675 | MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize); |
| 676 | MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown); |
| 677 | MsgHandler->bind("textDocument/rangeFormatting", &ClangdLSPServer::onDocumentRangeFormatting); |
| 678 | MsgHandler->bind("textDocument/onTypeFormatting", &ClangdLSPServer::onDocumentOnTypeFormatting); |
| 679 | MsgHandler->bind("textDocument/formatting", &ClangdLSPServer::onDocumentFormatting); |
| 680 | MsgHandler->bind("textDocument/codeAction", &ClangdLSPServer::onCodeAction); |
| 681 | MsgHandler->bind("textDocument/completion", &ClangdLSPServer::onCompletion); |
| 682 | MsgHandler->bind("textDocument/signatureHelp", &ClangdLSPServer::onSignatureHelp); |
| 683 | MsgHandler->bind("textDocument/definition", &ClangdLSPServer::onGoToDefinition); |
| 684 | MsgHandler->bind("textDocument/references", &ClangdLSPServer::onReference); |
| 685 | MsgHandler->bind("textDocument/switchSourceHeader", &ClangdLSPServer::onSwitchSourceHeader); |
| 686 | MsgHandler->bind("textDocument/rename", &ClangdLSPServer::onRename); |
| 687 | MsgHandler->bind("textDocument/hover", &ClangdLSPServer::onHover); |
| 688 | MsgHandler->bind("textDocument/documentSymbol", &ClangdLSPServer::onDocumentSymbol); |
| 689 | MsgHandler->bind("workspace/executeCommand", &ClangdLSPServer::onCommand); |
| 690 | MsgHandler->bind("textDocument/documentHighlight", &ClangdLSPServer::onDocumentHighlight); |
| 691 | MsgHandler->bind("workspace/symbol", &ClangdLSPServer::onWorkspaceSymbol); |
| 692 | MsgHandler->bind("textDocument/didOpen", &ClangdLSPServer::onDocumentDidOpen); |
| 693 | MsgHandler->bind("textDocument/didClose", &ClangdLSPServer::onDocumentDidClose); |
| 694 | MsgHandler->bind("textDocument/didChange", &ClangdLSPServer::onDocumentDidChange); |
| 695 | MsgHandler->bind("workspace/didChangeWatchedFiles", &ClangdLSPServer::onFileEvent); |
| 696 | MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration); |
| 697 | // clang-format on |
| 698 | } |
| 699 | |
| 700 | ClangdLSPServer::~ClangdLSPServer() = default; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 701 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 702 | bool ClangdLSPServer::run() { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 703 | // Run the Language Server loop. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 704 | bool CleanExit = true; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 705 | if (auto Err = Transp.loop(*MsgHandler)) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 706 | elog("Transport error: {0}", std::move(Err)); |
| 707 | CleanExit = false; |
| 708 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 709 | |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 710 | // Destroy ClangdServer to ensure all worker threads finish. |
| 711 | Server.reset(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 712 | return CleanExit && ShutdownRequestReceived; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 715 | std::vector<Fix> ClangdLSPServer::getFixes(StringRef File, |
| 716 | const clangd::Diagnostic &D) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 717 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 718 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 719 | if (DiagToFixItsIter == FixItsMap.end()) |
| 720 | return {}; |
| 721 | |
| 722 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 723 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 724 | if (FixItsIter == DiagToFixItsMap.end()) |
| 725 | return {}; |
| 726 | |
| 727 | return FixItsIter->second; |
| 728 | } |
| 729 | |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 730 | void ClangdLSPServer::onDiagnosticsReady(PathRef File, |
| 731 | std::vector<Diag> Diagnostics) { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 732 | json::Array DiagnosticsJSON; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 733 | |
| 734 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 735 | for (auto &Diag : Diagnostics) { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 736 | toLSPDiags(Diag, [&](clangd::Diagnostic Diag, ArrayRef<Fix> Fixes) { |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 737 | json::Object LSPDiag({ |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 738 | {"range", Diag.range}, |
| 739 | {"severity", Diag.severity}, |
| 740 | {"message", Diag.message}, |
| 741 | }); |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 742 | // LSP extension: embed the fixes in the diagnostic. |
| 743 | if (DiagOpts.EmbedFixesInDiagnostics && !Fixes.empty()) { |
| 744 | json::Array ClangdFixes; |
| 745 | for (const auto &Fix : Fixes) { |
| 746 | WorkspaceEdit WE; |
| 747 | URIForFile URI{File}; |
| 748 | WE.changes = {{URI.uri(), std::vector<TextEdit>(Fix.Edits.begin(), |
| 749 | Fix.Edits.end())}}; |
| 750 | ClangdFixes.push_back( |
| 751 | json::Object{{"edit", toJSON(WE)}, {"title", Fix.Message}}); |
| 752 | } |
| 753 | LSPDiag["clangd_fixes"] = std::move(ClangdFixes); |
| 754 | } |
Alex Lorenz | 0ce8a7a | 2018-08-22 20:30:06 +0000 | [diff] [blame] | 755 | if (DiagOpts.SendDiagnosticCategory && !Diag.category.empty()) |
Alex Lorenz | 3714643 | 2018-08-14 22:21:40 +0000 | [diff] [blame] | 756 | LSPDiag["category"] = Diag.category; |
Alex Lorenz | 8626d36 | 2018-08-10 17:25:07 +0000 | [diff] [blame] | 757 | DiagnosticsJSON.push_back(std::move(LSPDiag)); |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 758 | |
| 759 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
Kirill Bobyrev | 4a5ff88 | 2018-10-07 14:49:41 +0000 | [diff] [blame] | 760 | llvm::copy(Fixes, std::back_inserter(FixItsForDiagnostic)); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 761 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | // Cache FixIts |
| 765 | { |
| 766 | // FIXME(ibiryukov): should be deleted when documents are removed |
| 767 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 768 | FixItsMap[File] = LocalFixIts; |
| 769 | } |
| 770 | |
| 771 | // Publish diagnostics. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 772 | notify("textDocument/publishDiagnostics", |
| 773 | json::Object{ |
| 774 | {"uri", URIForFile{File}}, |
| 775 | {"diagnostics", std::move(DiagnosticsJSON)}, |
| 776 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 777 | } |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 778 | |
| 779 | void ClangdLSPServer::reparseOpenedFiles() { |
| 780 | for (const Path &FilePath : DraftMgr.getActiveFiles()) |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 781 | Server->addDocument(FilePath, *DraftMgr.getDraft(FilePath), |
| 782 | WantDiagnostics::Auto); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 783 | } |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 784 | |
| 785 | ClangdLSPServer::CompilationDB ClangdLSPServer::CompilationDB::makeInMemory() { |
Sam McCall | 2172ee9 | 2018-10-23 13:14:02 +0000 | [diff] [blame^] | 786 | return CompilationDB(llvm::make_unique<InMemoryCompilationDb>(), |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 787 | /*IsDirectoryBased=*/false); |
| 788 | } |
| 789 | |
| 790 | ClangdLSPServer::CompilationDB |
| 791 | ClangdLSPServer::CompilationDB::makeDirectoryBased( |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 792 | Optional<Path> CompileCommandsDir) { |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 793 | auto CDB = llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>( |
| 794 | std::move(CompileCommandsDir)); |
Sam McCall | 2172ee9 | 2018-10-23 13:14:02 +0000 | [diff] [blame^] | 795 | return CompilationDB(std::move(CDB), |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 796 | /*IsDirectoryBased=*/true); |
| 797 | } |
| 798 | |
| 799 | void ClangdLSPServer::CompilationDB::invalidate(PathRef File) { |
| 800 | if (!IsDirectoryBased) |
| 801 | static_cast<InMemoryCompilationDb *>(CDB.get())->invalidate(File); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | bool ClangdLSPServer::CompilationDB::setCompilationCommandForFile( |
| 805 | PathRef File, tooling::CompileCommand CompilationCommand) { |
| 806 | if (IsDirectoryBased) { |
| 807 | elog("Trying to set compile command for {0} while using directory-based " |
| 808 | "compilation database", |
| 809 | File); |
| 810 | return false; |
| 811 | } |
| 812 | return static_cast<InMemoryCompilationDb *>(CDB.get()) |
| 813 | ->setCompilationCommandForFile(File, std::move(CompilationCommand)); |
| 814 | } |
| 815 | |
| 816 | void ClangdLSPServer::CompilationDB::setExtraFlagsForFile( |
| 817 | PathRef File, std::vector<std::string> ExtraFlags) { |
| 818 | if (!IsDirectoryBased) { |
| 819 | elog("Trying to set extra flags for {0} while using in-memory compilation " |
| 820 | "database", |
| 821 | File); |
| 822 | return; |
| 823 | } |
| 824 | static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get()) |
| 825 | ->setExtraFlagsForFile(File, std::move(ExtraFlags)); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | void ClangdLSPServer::CompilationDB::setCompileCommandsDir(Path P) { |
| 829 | if (!IsDirectoryBased) { |
| 830 | elog("Trying to set compile commands dir while using in-memory compilation " |
| 831 | "database"); |
| 832 | return; |
| 833 | } |
| 834 | static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get()) |
| 835 | ->setCompileCommandsDir(P); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 836 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 837 | |
| 838 | } // namespace clangd |
| 839 | } // namespace clang |