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