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