Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdLSPServer.cpp - LSP server ------------------------*- C++-*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 6 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 8 | |
| 9 | #include "ClangdLSPServer.h" |
Sam McCall | 032727f | 2020-05-06 01:39:59 +0200 | [diff] [blame] | 10 | #include "CodeComplete.h" |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 11 | #include "Diagnostics.h" |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 12 | #include "DraftStore.h" |
Kadir Cetinkaya | 256247c | 2019-06-26 07:45:27 +0000 | [diff] [blame] | 13 | #include "GlobalCompilationDatabase.h" |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 14 | #include "Protocol.h" |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 15 | #include "SemanticHighlighting.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 16 | #include "SourceCode.h" |
Kadir Cetinkaya | 6b85032 | 2020-03-17 19:08:23 +0100 | [diff] [blame] | 17 | #include "TUScheduler.h" |
Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 18 | #include "URI.h" |
Sam McCall | 395fde7 | 2019-06-18 13:37:54 +0000 | [diff] [blame] | 19 | #include "refactor/Tweak.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 20 | #include "support/Context.h" |
| 21 | #include "support/Trace.h" |
Sam McCall | 6f7dca9 | 2020-03-03 12:25:46 +0100 | [diff] [blame] | 22 | #include "clang/Basic/Version.h" |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 23 | #include "clang/Tooling/Core/Replacement.h" |
Kadir Cetinkaya | 256247c | 2019-06-26 07:45:27 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/ArrayRef.h" |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Optional.h" |
Kadir Cetinkaya | 689bf93 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ScopeExit.h" |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringRef.h" |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/iterator_range.h" |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Errc.h" |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Error.h" |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FormatVariadic.h" |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 32 | #include "llvm/Support/JSON.h" |
Eric Liu | 5740ff5 | 2018-01-31 16:26:27 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Path.h" |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 34 | #include "llvm/Support/SHA1.h" |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ScopedPrinter.h" |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 36 | #include <cstddef> |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 37 | #include <memory> |
Sam McCall | 7d20e80 | 2020-01-22 19:41:45 +0100 | [diff] [blame] | 38 | #include <mutex> |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 39 | #include <string> |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 40 | #include <vector> |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 41 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 42 | namespace clang { |
| 43 | namespace clangd { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 44 | namespace { |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 45 | |
Kadir Cetinkaya | e64f99c | 2020-04-16 23:12:09 +0200 | [diff] [blame] | 46 | // Tracks end-to-end latency of high level lsp calls. Measurements are in |
| 47 | // seconds. |
| 48 | constexpr trace::Metric LSPLatency("lsp_latency", trace::Metric::Distribution, |
| 49 | "method_name"); |
| 50 | |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 51 | // LSP defines file versions as numbers that increase. |
| 52 | // ClangdServer treats them as opaque and therefore uses strings instead. |
| 53 | std::string encodeVersion(int64_t LSPVersion) { |
| 54 | return llvm::to_string(LSPVersion); |
| 55 | } |
| 56 | llvm::Optional<int64_t> decodeVersion(llvm::StringRef Encoded) { |
| 57 | int64_t Result; |
| 58 | if (llvm::to_integer(Encoded, Result, 10)) |
| 59 | return Result; |
| 60 | else if (!Encoded.empty()) // Empty can be e.g. diagnostics on close. |
| 61 | elog("unexpected non-numeric version {0}", Encoded); |
| 62 | return llvm::None; |
| 63 | } |
| 64 | |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 65 | /// Transforms a tweak into a code action that would apply it if executed. |
| 66 | /// EXPECTS: T.prepare() was called and returned true. |
| 67 | CodeAction toCodeAction(const ClangdServer::TweakRef &T, const URIForFile &File, |
| 68 | Range Selection) { |
| 69 | CodeAction CA; |
| 70 | CA.title = T.Title; |
Sam McCall | 395fde7 | 2019-06-18 13:37:54 +0000 | [diff] [blame] | 71 | switch (T.Intent) { |
| 72 | case Tweak::Refactor: |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 73 | CA.kind = std::string(CodeAction::REFACTOR_KIND); |
Sam McCall | 395fde7 | 2019-06-18 13:37:54 +0000 | [diff] [blame] | 74 | break; |
| 75 | case Tweak::Info: |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 76 | CA.kind = std::string(CodeAction::INFO_KIND); |
Sam McCall | 395fde7 | 2019-06-18 13:37:54 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 79 | // This tweak may have an expensive second stage, we only run it if the user |
| 80 | // actually chooses it in the UI. We reply with a command that would run the |
| 81 | // corresponding tweak. |
| 82 | // FIXME: for some tweaks, computing the edits is cheap and we could send them |
| 83 | // directly. |
| 84 | CA.command.emplace(); |
| 85 | CA.command->title = T.Title; |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 86 | CA.command->command = std::string(Command::CLANGD_APPLY_TWEAK); |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 87 | CA.command->tweakArgs.emplace(); |
| 88 | CA.command->tweakArgs->file = File; |
| 89 | CA.command->tweakArgs->tweakID = T.ID; |
| 90 | CA.command->tweakArgs->selection = Selection; |
| 91 | return CA; |
Simon Pilgrim | e9a136b | 2019-02-03 14:08:30 +0000 | [diff] [blame] | 92 | } |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 93 | |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 94 | void adjustSymbolKinds(llvm::MutableArrayRef<DocumentSymbol> Syms, |
| 95 | SymbolKindBitset Kinds) { |
| 96 | for (auto &S : Syms) { |
| 97 | S.kind = adjustKindToCapability(S.kind, Kinds); |
| 98 | adjustSymbolKinds(S.children, Kinds); |
| 99 | } |
| 100 | } |
| 101 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 102 | SymbolKindBitset defaultSymbolKinds() { |
| 103 | SymbolKindBitset Defaults; |
| 104 | for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array); |
| 105 | ++I) |
| 106 | Defaults.set(I); |
| 107 | return Defaults; |
| 108 | } |
| 109 | |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 110 | CompletionItemKindBitset defaultCompletionItemKinds() { |
| 111 | CompletionItemKindBitset Defaults; |
| 112 | for (size_t I = CompletionItemKindMin; |
| 113 | I <= static_cast<size_t>(CompletionItemKind::Reference); ++I) |
| 114 | Defaults.set(I); |
| 115 | return Defaults; |
| 116 | } |
| 117 | |
Haojian Wu | 1ca2ee4 | 2019-07-04 12:27:21 +0000 | [diff] [blame] | 118 | // Build a lookup table (HighlightingKind => {TextMate Scopes}), which is sent |
| 119 | // to the LSP client. |
| 120 | std::vector<std::vector<std::string>> buildHighlightScopeLookupTable() { |
| 121 | std::vector<std::vector<std::string>> LookupTable; |
| 122 | // HighlightingKind is using as the index. |
Ilya Biryukov | 63d5d16 | 2019-09-09 08:57:17 +0000 | [diff] [blame] | 123 | for (int KindValue = 0; KindValue <= (int)HighlightingKind::LastKind; |
Haojian Wu | 1ca2ee4 | 2019-07-04 12:27:21 +0000 | [diff] [blame] | 124 | ++KindValue) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 125 | LookupTable.push_back( |
| 126 | {std::string(toTextMateScope((HighlightingKind)(KindValue)))}); |
Haojian Wu | 1ca2ee4 | 2019-07-04 12:27:21 +0000 | [diff] [blame] | 127 | return LookupTable; |
| 128 | } |
| 129 | |
Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 130 | // Makes sure edits in \p FE are applicable to latest file contents reported by |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 131 | // editor. If not generates an error message containing information about files |
| 132 | // that needs to be saved. |
Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 133 | llvm::Error validateEdits(const DraftStore &DraftMgr, const FileEdits &FE) { |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 134 | size_t InvalidFileCount = 0; |
| 135 | llvm::StringRef LastInvalidFile; |
Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 136 | for (const auto &It : FE) { |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 137 | if (auto Draft = DraftMgr.getDraft(It.first())) { |
| 138 | // If the file is open in user's editor, make sure the version we |
| 139 | // saw and current version are compatible as this is the text that |
| 140 | // will be replaced by editors. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 141 | if (!It.second.canApplyTo(Draft->Contents)) { |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 142 | ++InvalidFileCount; |
| 143 | LastInvalidFile = It.first(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | if (!InvalidFileCount) |
| 148 | return llvm::Error::success(); |
| 149 | if (InvalidFileCount == 1) |
| 150 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 151 | "File must be saved first: " + |
| 152 | LastInvalidFile); |
| 153 | return llvm::createStringError( |
| 154 | llvm::inconvertibleErrorCode(), |
| 155 | "Files must be saved first: " + LastInvalidFile + " (and " + |
| 156 | llvm::to_string(InvalidFileCount - 1) + " others)"); |
| 157 | } |
| 158 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 159 | } // namespace |
| 160 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 161 | // MessageHandler dispatches incoming LSP messages. |
| 162 | // It handles cross-cutting concerns: |
| 163 | // - serializes/deserializes protocol objects to JSON |
| 164 | // - logging of inbound messages |
| 165 | // - cancellation handling |
| 166 | // - basic call tracing |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 167 | // MessageHandler ensures that initialize() is called before any other handler. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 168 | class ClangdLSPServer::MessageHandler : public Transport::MessageHandler { |
| 169 | public: |
| 170 | MessageHandler(ClangdLSPServer &Server) : Server(Server) {} |
| 171 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 172 | bool onNotify(llvm::StringRef Method, llvm::json::Value Params) override { |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 173 | WithContext HandlerContext(handlerContext()); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 174 | log("<-- {0}", Method); |
| 175 | if (Method == "exit") |
| 176 | return false; |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 177 | if (!Server.Server) |
| 178 | elog("Notification {0} before initialization", Method); |
| 179 | else if (Method == "$/cancelRequest") |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 180 | onCancel(std::move(Params)); |
| 181 | else if (auto Handler = Notifications.lookup(Method)) |
| 182 | Handler(std::move(Params)); |
| 183 | else |
| 184 | log("unhandled notification {0}", Method); |
| 185 | return true; |
| 186 | } |
| 187 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 188 | bool onCall(llvm::StringRef Method, llvm::json::Value Params, |
| 189 | llvm::json::Value ID) override { |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 190 | WithContext HandlerContext(handlerContext()); |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 191 | // Calls can be canceled by the client. Add cancellation context. |
| 192 | WithContext WithCancel(cancelableRequestContext(ID)); |
Kadir Cetinkaya | e64f99c | 2020-04-16 23:12:09 +0200 | [diff] [blame] | 193 | trace::Span Tracer(Method, LSPLatency); |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 194 | SPAN_ATTACH(Tracer, "Params", Params); |
| 195 | ReplyOnce Reply(ID, Method, &Server, Tracer.Args); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 196 | log("<-- {0}({1})", Method, ID); |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 197 | if (!Server.Server && Method != "initialize") { |
| 198 | elog("Call {0} before initialization.", Method); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 199 | Reply(llvm::make_error<LSPError>("server not initialized", |
| 200 | ErrorCode::ServerNotInitialized)); |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 201 | } else if (auto Handler = Calls.lookup(Method)) |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 202 | Handler(std::move(Params), std::move(Reply)); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 203 | else |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 204 | Reply(llvm::make_error<LSPError>("method not found", |
| 205 | ErrorCode::MethodNotFound)); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 209 | bool onReply(llvm::json::Value ID, |
| 210 | llvm::Expected<llvm::json::Value> Result) override { |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 211 | WithContext HandlerContext(handlerContext()); |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 212 | |
| 213 | Callback<llvm::json::Value> ReplyHandler = nullptr; |
| 214 | if (auto IntID = ID.getAsInteger()) { |
| 215 | std::lock_guard<std::mutex> Mutex(CallMutex); |
| 216 | // Find a corresponding callback for the request ID; |
| 217 | for (size_t Index = 0; Index < ReplyCallbacks.size(); ++Index) { |
| 218 | if (ReplyCallbacks[Index].first == *IntID) { |
| 219 | ReplyHandler = std::move(ReplyCallbacks[Index].second); |
| 220 | ReplyCallbacks.erase(ReplyCallbacks.begin() + |
| 221 | Index); // remove the entry |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (!ReplyHandler) { |
| 228 | // No callback being found, use a default log callback. |
| 229 | ReplyHandler = [&ID](llvm::Expected<llvm::json::Value> Result) { |
| 230 | elog("received a reply with ID {0}, but there was no such call", ID); |
| 231 | if (!Result) |
| 232 | llvm::consumeError(Result.takeError()); |
| 233 | }; |
| 234 | } |
| 235 | |
| 236 | // Log and run the reply handler. |
| 237 | if (Result) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 238 | log("<-- reply({0})", ID); |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 239 | ReplyHandler(std::move(Result)); |
| 240 | } else { |
| 241 | auto Err = Result.takeError(); |
| 242 | log("<-- reply({0}) error: {1}", ID, Err); |
| 243 | ReplyHandler(std::move(Err)); |
| 244 | } |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
| 248 | // Bind an LSP method name to a call. |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 249 | template <typename Param, typename Result> |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 250 | void bind(const char *Method, |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 251 | void (ClangdLSPServer::*Handler)(const Param &, Callback<Result>)) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 252 | Calls[Method] = [Method, Handler, this](llvm::json::Value RawParams, |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 253 | ReplyOnce Reply) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 254 | Param P; |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 255 | if (fromJSON(RawParams, P)) { |
| 256 | (Server.*Handler)(P, std::move(Reply)); |
| 257 | } else { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 258 | elog("Failed to decode {0} request.", Method); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 259 | Reply(llvm::make_error<LSPError>("failed to decode request", |
| 260 | ErrorCode::InvalidRequest)); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 261 | } |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 262 | }; |
| 263 | } |
| 264 | |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 265 | // Bind a reply callback to a request. The callback will be invoked when |
| 266 | // clangd receives the reply from the LSP client. |
| 267 | // Return a call id of the request. |
| 268 | llvm::json::Value bindReply(Callback<llvm::json::Value> Reply) { |
| 269 | llvm::Optional<std::pair<int, Callback<llvm::json::Value>>> OldestCB; |
| 270 | int ID; |
| 271 | { |
| 272 | std::lock_guard<std::mutex> Mutex(CallMutex); |
| 273 | ID = NextCallID++; |
| 274 | ReplyCallbacks.emplace_back(ID, std::move(Reply)); |
| 275 | |
| 276 | // If the queue overflows, we assume that the client didn't reply the |
| 277 | // oldest request, and run the corresponding callback which replies an |
| 278 | // error to the client. |
| 279 | if (ReplyCallbacks.size() > MaxReplayCallbacks) { |
| 280 | elog("more than {0} outstanding LSP calls, forgetting about {1}", |
| 281 | MaxReplayCallbacks, ReplyCallbacks.front().first); |
| 282 | OldestCB = std::move(ReplyCallbacks.front()); |
| 283 | ReplyCallbacks.pop_front(); |
| 284 | } |
| 285 | } |
| 286 | if (OldestCB) |
| 287 | OldestCB->second(llvm::createStringError( |
| 288 | llvm::inconvertibleErrorCode(), |
| 289 | llvm::formatv("failed to receive a client reply for request ({0})", |
| 290 | OldestCB->first))); |
| 291 | return ID; |
| 292 | } |
| 293 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 294 | // Bind an LSP method name to a notification. |
| 295 | template <typename Param> |
| 296 | void bind(const char *Method, |
| 297 | void (ClangdLSPServer::*Handler)(const Param &)) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 298 | Notifications[Method] = [Method, Handler, |
| 299 | this](llvm::json::Value RawParams) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 300 | Param P; |
| 301 | if (!fromJSON(RawParams, P)) { |
| 302 | elog("Failed to decode {0} request.", Method); |
| 303 | return; |
| 304 | } |
Kadir Cetinkaya | e64f99c | 2020-04-16 23:12:09 +0200 | [diff] [blame] | 305 | trace::Span Tracer(Method, LSPLatency); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 306 | SPAN_ATTACH(Tracer, "Params", RawParams); |
| 307 | (Server.*Handler)(P); |
| 308 | }; |
| 309 | } |
| 310 | |
| 311 | private: |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 312 | // Function object to reply to an LSP call. |
| 313 | // Each instance must be called exactly once, otherwise: |
| 314 | // - the bug is logged, and (in debug mode) an assert will fire |
| 315 | // - if there was no reply, an error reply is sent |
| 316 | // - if there were multiple replies, only the first is sent |
| 317 | class ReplyOnce { |
| 318 | std::atomic<bool> Replied = {false}; |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 319 | std::chrono::steady_clock::time_point Start; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 320 | llvm::json::Value ID; |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 321 | std::string Method; |
| 322 | ClangdLSPServer *Server; // Null when moved-from. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 323 | llvm::json::Object *TraceArgs; |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 324 | |
| 325 | public: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 326 | ReplyOnce(const llvm::json::Value &ID, llvm::StringRef Method, |
| 327 | ClangdLSPServer *Server, llvm::json::Object *TraceArgs) |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 328 | : Start(std::chrono::steady_clock::now()), ID(ID), Method(Method), |
| 329 | Server(Server), TraceArgs(TraceArgs) { |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 330 | assert(Server); |
| 331 | } |
| 332 | ReplyOnce(ReplyOnce &&Other) |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 333 | : Replied(Other.Replied.load()), Start(Other.Start), |
| 334 | ID(std::move(Other.ID)), Method(std::move(Other.Method)), |
| 335 | Server(Other.Server), TraceArgs(Other.TraceArgs) { |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 336 | Other.Server = nullptr; |
| 337 | } |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 338 | ReplyOnce &operator=(ReplyOnce &&) = delete; |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 339 | ReplyOnce(const ReplyOnce &) = delete; |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 340 | ReplyOnce &operator=(const ReplyOnce &) = delete; |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 341 | |
| 342 | ~ReplyOnce() { |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 343 | // There's one legitimate reason to never reply to a request: clangd's |
| 344 | // request handler send a call to the client (e.g. applyEdit) and the |
| 345 | // client never replied. In this case, the ReplyOnce is owned by |
| 346 | // ClangdLSPServer's reply callback table and is destroyed along with the |
| 347 | // server. We don't attempt to send a reply in this case, there's little |
| 348 | // to be gained from doing so. |
| 349 | if (Server && !Server->IsBeingDestroyed && !Replied) { |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 350 | elog("No reply to message {0}({1})", Method, ID); |
| 351 | assert(false && "must reply to all calls!"); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 352 | (*this)(llvm::make_error<LSPError>("server failed to reply", |
| 353 | ErrorCode::InternalError)); |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 357 | void operator()(llvm::Expected<llvm::json::Value> Reply) { |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 358 | assert(Server && "moved-from!"); |
| 359 | if (Replied.exchange(true)) { |
| 360 | elog("Replied twice to message {0}({1})", Method, ID); |
| 361 | assert(false && "must reply to each call only once!"); |
| 362 | return; |
| 363 | } |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 364 | auto Duration = std::chrono::steady_clock::now() - Start; |
| 365 | if (Reply) { |
| 366 | log("--> reply:{0}({1}) {2:ms}", Method, ID, Duration); |
| 367 | if (TraceArgs) |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 368 | (*TraceArgs)["Reply"] = *Reply; |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 369 | std::lock_guard<std::mutex> Lock(Server->TranspWriter); |
| 370 | Server->Transp.reply(std::move(ID), std::move(Reply)); |
| 371 | } else { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 372 | llvm::Error Err = Reply.takeError(); |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 373 | log("--> reply:{0}({1}) {2:ms}, error: {3}", Method, ID, Duration, Err); |
| 374 | if (TraceArgs) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 375 | (*TraceArgs)["Error"] = llvm::to_string(Err); |
Sam McCall | d7babe4 | 2018-10-24 15:18:40 +0000 | [diff] [blame] | 376 | std::lock_guard<std::mutex> Lock(Server->TranspWriter); |
| 377 | Server->Transp.reply(std::move(ID), std::move(Err)); |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 378 | } |
Sam McCall | e2f3a73 | 2018-10-24 14:26:26 +0000 | [diff] [blame] | 379 | } |
| 380 | }; |
| 381 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 382 | llvm::StringMap<std::function<void(llvm::json::Value)>> Notifications; |
| 383 | llvm::StringMap<std::function<void(llvm::json::Value, ReplyOnce)>> Calls; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 384 | |
| 385 | // Method calls may be cancelled by ID, so keep track of their state. |
| 386 | // This needs a mutex: handlers may finish on a different thread, and that's |
| 387 | // when we clean up entries in the map. |
| 388 | mutable std::mutex RequestCancelersMutex; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 389 | llvm::StringMap<std::pair<Canceler, /*Cookie*/ unsigned>> RequestCancelers; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 390 | unsigned NextRequestCookie = 0; // To disambiguate reused IDs, see below. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 391 | void onCancel(const llvm::json::Value &Params) { |
| 392 | const llvm::json::Value *ID = nullptr; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 393 | if (auto *O = Params.getAsObject()) |
| 394 | ID = O->get("id"); |
| 395 | if (!ID) { |
| 396 | elog("Bad cancellation request: {0}", Params); |
| 397 | return; |
| 398 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 399 | auto StrID = llvm::to_string(*ID); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 400 | std::lock_guard<std::mutex> Lock(RequestCancelersMutex); |
| 401 | auto It = RequestCancelers.find(StrID); |
| 402 | if (It != RequestCancelers.end()) |
| 403 | It->second.first(); // Invoke the canceler. |
| 404 | } |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 405 | |
| 406 | Context handlerContext() const { |
| 407 | return Context::current().derive( |
| 408 | kCurrentOffsetEncoding, |
| 409 | Server.NegotiatedOffsetEncoding.getValueOr(OffsetEncoding::UTF16)); |
| 410 | } |
| 411 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 412 | // We run cancelable requests in a context that does two things: |
| 413 | // - allows cancellation using RequestCancelers[ID] |
| 414 | // - cleans up the entry in RequestCancelers when it's no longer needed |
| 415 | // If a client reuses an ID, the last wins and the first cannot be canceled. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 416 | Context cancelableRequestContext(const llvm::json::Value &ID) { |
Sam McCall | 31db1e0 | 2020-04-11 18:19:50 +0200 | [diff] [blame] | 417 | auto Task = cancelableTask( |
| 418 | /*Reason=*/static_cast<int>(ErrorCode::RequestCancelled)); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 419 | auto StrID = llvm::to_string(ID); // JSON-serialize ID for map key. |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 420 | auto Cookie = NextRequestCookie++; // No lock, only called on main thread. |
| 421 | { |
| 422 | std::lock_guard<std::mutex> Lock(RequestCancelersMutex); |
| 423 | RequestCancelers[StrID] = {std::move(Task.second), Cookie}; |
| 424 | } |
| 425 | // When the request ends, we can clean up the entry we just added. |
| 426 | // The cookie lets us check that it hasn't been overwritten due to ID |
| 427 | // reuse. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 428 | return Task.first.derive(llvm::make_scope_exit([this, StrID, Cookie] { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 429 | std::lock_guard<std::mutex> Lock(RequestCancelersMutex); |
| 430 | auto It = RequestCancelers.find(StrID); |
| 431 | if (It != RequestCancelers.end() && It->second.second == Cookie) |
| 432 | RequestCancelers.erase(It); |
| 433 | })); |
| 434 | } |
| 435 | |
Kadir Cetinkaya | 9a3a87d | 2019-10-09 13:59:31 +0000 | [diff] [blame] | 436 | // The maximum number of callbacks held in clangd. |
| 437 | // |
| 438 | // We bound the maximum size to the pending map to prevent memory leakage |
| 439 | // for cases where LSP clients don't reply for the request. |
| 440 | // This has to go after RequestCancellers and RequestCancellersMutex since it |
| 441 | // can contain a callback that has a cancelable context. |
| 442 | static constexpr int MaxReplayCallbacks = 100; |
| 443 | mutable std::mutex CallMutex; |
| 444 | int NextCallID = 0; /* GUARDED_BY(CallMutex) */ |
| 445 | std::deque<std::pair</*RequestID*/ int, |
| 446 | /*ReplyHandler*/ Callback<llvm::json::Value>>> |
| 447 | ReplyCallbacks; /* GUARDED_BY(CallMutex) */ |
| 448 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 449 | ClangdLSPServer &Server; |
| 450 | }; |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 451 | constexpr int ClangdLSPServer::MessageHandler::MaxReplayCallbacks; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 452 | |
| 453 | // call(), notify(), and reply() wrap the Transport, adding logging and locking. |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 454 | void ClangdLSPServer::callRaw(StringRef Method, llvm::json::Value Params, |
| 455 | Callback<llvm::json::Value> CB) { |
| 456 | auto ID = MsgHandler->bindReply(std::move(CB)); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 457 | log("--> {0}({1})", Method, ID); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 458 | std::lock_guard<std::mutex> Lock(TranspWriter); |
| 459 | Transp.call(Method, std::move(Params), ID); |
| 460 | } |
| 461 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 462 | void ClangdLSPServer::notify(llvm::StringRef Method, llvm::json::Value Params) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 463 | log("--> {0}", Method); |
| 464 | std::lock_guard<std::mutex> Lock(TranspWriter); |
| 465 | Transp.notify(Method, std::move(Params)); |
| 466 | } |
| 467 | |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 468 | static std::vector<llvm::StringRef> semanticTokenTypes() { |
| 469 | std::vector<llvm::StringRef> Types; |
| 470 | for (unsigned I = 0; I <= static_cast<unsigned>(HighlightingKind::LastKind); |
| 471 | ++I) |
| 472 | Types.push_back(toSemanticTokenType(static_cast<HighlightingKind>(I))); |
| 473 | return Types; |
| 474 | } |
| 475 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 476 | void ClangdLSPServer::onInitialize(const InitializeParams &Params, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 477 | Callback<llvm::json::Value> Reply) { |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 478 | // Determine character encoding first as it affects constructed ClangdServer. |
| 479 | if (Params.capabilities.offsetEncoding && !NegotiatedOffsetEncoding) { |
| 480 | NegotiatedOffsetEncoding = OffsetEncoding::UTF16; // fallback |
| 481 | for (OffsetEncoding Supported : *Params.capabilities.offsetEncoding) |
| 482 | if (Supported != OffsetEncoding::UnsupportedEncoding) { |
| 483 | NegotiatedOffsetEncoding = Supported; |
| 484 | break; |
| 485 | } |
| 486 | } |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 487 | |
Sam McCall | edf6a19 | 2020-03-24 00:31:14 +0100 | [diff] [blame] | 488 | ClangdServerOpts.TheiaSemanticHighlighting = |
| 489 | Params.capabilities.TheiaSemanticHighlighting; |
Sam McCall | fc83010 | 2020-04-01 12:02:28 +0200 | [diff] [blame] | 490 | if (Params.capabilities.TheiaSemanticHighlighting && |
| 491 | Params.capabilities.SemanticTokens) { |
| 492 | log("Client supports legacy semanticHighlights notification and standard " |
| 493 | "semanticTokens request, choosing the latter (no notifications)."); |
| 494 | ClangdServerOpts.TheiaSemanticHighlighting = false; |
| 495 | } |
| 496 | |
Sam McCall | 0d9b40f | 2018-10-19 15:42:23 +0000 | [diff] [blame] | 497 | if (Params.rootUri && *Params.rootUri) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 498 | ClangdServerOpts.WorkspaceRoot = std::string(Params.rootUri->file()); |
Sam McCall | 0d9b40f | 2018-10-19 15:42:23 +0000 | [diff] [blame] | 499 | else if (Params.rootPath && !Params.rootPath->empty()) |
| 500 | ClangdServerOpts.WorkspaceRoot = *Params.rootPath; |
Sam McCall | 3d0adbe | 2018-10-18 14:41:50 +0000 | [diff] [blame] | 501 | if (Server) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 502 | return Reply(llvm::make_error<LSPError>("server already initialized", |
| 503 | ErrorCode::InvalidRequest)); |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 504 | if (const auto &Dir = Params.initializationOptions.compilationDatabasePath) |
| 505 | CompileCommandsDir = Dir; |
Kadir Cetinkaya | 256247c | 2019-06-26 07:45:27 +0000 | [diff] [blame] | 506 | if (UseDirBasedCDB) { |
Jonas Devlieghere | 1c705d9 | 2019-08-14 23:52:23 +0000 | [diff] [blame] | 507 | BaseCDB = std::make_unique<DirectoryBasedGlobalCompilationDatabase>( |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 508 | CompileCommandsDir); |
Kadir Cetinkaya | 256247c | 2019-06-26 07:45:27 +0000 | [diff] [blame] | 509 | BaseCDB = getQueryDriverDatabase( |
| 510 | llvm::makeArrayRef(ClangdServerOpts.QueryDriverGlobs), |
| 511 | std::move(BaseCDB)); |
| 512 | } |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 513 | auto Mangler = CommandMangler::detect(); |
| 514 | if (ClangdServerOpts.ResourceDir) |
| 515 | Mangler.ResourceDir = *ClangdServerOpts.ResourceDir; |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 516 | CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags, |
Sam McCall | 2a3ac01 | 2020-06-09 22:54:42 +0200 | [diff] [blame] | 517 | tooling::ArgumentsAdjuster(std::move(Mangler))); |
Kadir Cetinkaya | 9d66247 | 2019-10-15 14:20:52 +0000 | [diff] [blame] | 518 | { |
| 519 | // Switch caller's context with LSPServer's background context. Since we |
| 520 | // rather want to propagate information from LSPServer's context into the |
| 521 | // Server, CDB, etc. |
| 522 | WithContext MainContext(BackgroundContext.clone()); |
| 523 | llvm::Optional<WithContextValue> WithOffsetEncoding; |
| 524 | if (NegotiatedOffsetEncoding) |
| 525 | WithOffsetEncoding.emplace(kCurrentOffsetEncoding, |
| 526 | *NegotiatedOffsetEncoding); |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 527 | Server.emplace(*CDB, TFS, ClangdServerOpts, |
Sam McCall | 6ef1cce | 2020-01-24 14:08:56 +0100 | [diff] [blame] | 528 | static_cast<ClangdServer::Callbacks *>(this)); |
Kadir Cetinkaya | 9d66247 | 2019-10-15 14:20:52 +0000 | [diff] [blame] | 529 | } |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 530 | applyConfiguration(Params.initializationOptions.ConfigSettings); |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 531 | |
Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 532 | CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets; |
Sam McCall | 8d41294 | 2019-06-18 11:57:26 +0000 | [diff] [blame] | 533 | CCOpts.IncludeFixIts = Params.capabilities.CompletionFixes; |
Sam McCall | 5f092e3 | 2019-07-08 17:27:15 +0000 | [diff] [blame] | 534 | if (!CCOpts.BundleOverloads.hasValue()) |
| 535 | CCOpts.BundleOverloads = Params.capabilities.HasSignatureHelp; |
Sam McCall | a3a27a7 | 2020-04-30 10:49:32 +0200 | [diff] [blame] | 536 | CCOpts.DocumentationFormat = |
| 537 | Params.capabilities.CompletionDocumentationFormat; |
Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 538 | DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes; |
| 539 | DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory; |
Sam McCall | c9e4ee9 | 2019-04-18 15:17:07 +0000 | [diff] [blame] | 540 | DiagOpts.EmitRelatedLocations = |
| 541 | Params.capabilities.DiagnosticRelatedInformation; |
Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 542 | if (Params.capabilities.WorkspaceSymbolKinds) |
| 543 | SupportedSymbolKinds |= *Params.capabilities.WorkspaceSymbolKinds; |
| 544 | if (Params.capabilities.CompletionItemKinds) |
| 545 | SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds; |
| 546 | SupportsCodeAction = Params.capabilities.CodeActionStructure; |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 547 | SupportsHierarchicalDocumentSymbol = |
| 548 | Params.capabilities.HierarchicalDocumentSymbol; |
Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame] | 549 | SupportFileStatus = Params.initializationOptions.FileStatus; |
Ilya Biryukov | f9169d0 | 2019-05-29 10:01:00 +0000 | [diff] [blame] | 550 | HoverContentFormat = Params.capabilities.HoverContentFormat; |
Ilya Biryukov | 4ef0f82 | 2019-06-04 09:36:59 +0000 | [diff] [blame] | 551 | SupportsOffsetsInSignatureHelp = Params.capabilities.OffsetsInSignatureHelp; |
Sam McCall | 7d20e80 | 2020-01-22 19:41:45 +0100 | [diff] [blame] | 552 | if (Params.capabilities.WorkDoneProgress) |
| 553 | BackgroundIndexProgressState = BackgroundIndexProgress::Empty; |
| 554 | BackgroundIndexSkipCreate = Params.capabilities.ImplicitProgressCreation; |
Haojian Wu | f429ab6 | 2019-07-24 07:49:23 +0000 | [diff] [blame] | 555 | |
| 556 | // Per LSP, renameProvider can be either boolean or RenameOptions. |
| 557 | // RenameOptions will be specified if the client states it supports prepare. |
| 558 | llvm::json::Value RenameProvider = |
| 559 | llvm::json::Object{{"prepareProvider", true}}; |
| 560 | if (!Params.capabilities.RenamePrepareSupport) // Only boolean allowed per LSP |
| 561 | RenameProvider = true; |
| 562 | |
Haojian Wu | 08d93f1 | 2019-08-22 14:53:45 +0000 | [diff] [blame] | 563 | // Per LSP, codeActionProvide can be either boolean or CodeActionOptions. |
| 564 | // CodeActionOptions is only valid if the client supports action literal |
| 565 | // via textDocument.codeAction.codeActionLiteralSupport. |
| 566 | llvm::json::Value CodeActionProvider = true; |
| 567 | if (Params.capabilities.CodeActionStructure) |
| 568 | CodeActionProvider = llvm::json::Object{ |
| 569 | {"codeActionKinds", |
| 570 | {CodeAction::QUICKFIX_KIND, CodeAction::REFACTOR_KIND, |
| 571 | CodeAction::INFO_KIND}}}; |
| 572 | |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 573 | llvm::json::Object Result{ |
Sam McCall | 6f7dca9 | 2020-03-03 12:25:46 +0100 | [diff] [blame] | 574 | {{"serverInfo", |
| 575 | llvm::json::Object{{"name", "clangd"}, |
| 576 | {"version", getClangToolFullVersion("clangd")}}}, |
| 577 | {"capabilities", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 578 | llvm::json::Object{ |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 579 | {"textDocumentSync", |
| 580 | llvm::json::Object{ |
| 581 | {"openClose", true}, |
| 582 | {"change", (int)TextDocumentSyncKind::Incremental}, |
| 583 | {"save", true}, |
| 584 | }}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 585 | {"documentFormattingProvider", true}, |
| 586 | {"documentRangeFormattingProvider", true}, |
| 587 | {"documentOnTypeFormattingProvider", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 588 | llvm::json::Object{ |
Sam McCall | 25c6257 | 2019-06-10 14:26:21 +0000 | [diff] [blame] | 589 | {"firstTriggerCharacter", "\n"}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 590 | {"moreTriggerCharacter", {}}, |
| 591 | }}, |
Haojian Wu | 08d93f1 | 2019-08-22 14:53:45 +0000 | [diff] [blame] | 592 | {"codeActionProvider", std::move(CodeActionProvider)}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 593 | {"completionProvider", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 594 | llvm::json::Object{ |
Kirill Bobyrev | 9d11e67 | 2020-08-26 17:08:00 +0200 | [diff] [blame^] | 595 | {"allCommitCharacters", |
| 596 | {" ", "\t", "(", ")", "[", "]", "{", "}", "<", |
| 597 | ">", ":", ";", ",", "+", "-", "/", "*", "%", |
| 598 | "^", "&", "#", "?", ".", "=", "\"", "'", "|"}}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 599 | {"resolveProvider", false}, |
Sam McCall | 032727f | 2020-05-06 01:39:59 +0200 | [diff] [blame] | 600 | // We do extra checks, e.g. that > is part of ->. |
| 601 | {"triggerCharacters", {".", "<", ">", ":", "\"", "/"}}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 602 | }}, |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 603 | {"semanticTokensProvider", |
| 604 | llvm::json::Object{ |
Sam McCall | 5fea54b | 2020-07-10 16:08:14 +0200 | [diff] [blame] | 605 | {"full", llvm::json::Object{{"delta", true}}}, |
| 606 | {"range", false}, |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 607 | {"legend", |
| 608 | llvm::json::Object{{"tokenTypes", semanticTokenTypes()}, |
| 609 | {"tokenModifiers", llvm::json::Array()}}}, |
| 610 | }}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 611 | {"signatureHelpProvider", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 612 | llvm::json::Object{ |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 613 | {"triggerCharacters", {"(", ","}}, |
| 614 | }}, |
Sam McCall | 866ba2c | 2019-02-01 11:26:13 +0000 | [diff] [blame] | 615 | {"declarationProvider", true}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 616 | {"definitionProvider", true}, |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 617 | {"documentHighlightProvider", true}, |
Sam McCall | 8d7ecc1 | 2019-12-16 19:08:51 +0100 | [diff] [blame] | 618 | {"documentLinkProvider", |
| 619 | llvm::json::Object{ |
| 620 | {"resolveProvider", false}, |
| 621 | }}, |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 622 | {"hoverProvider", true}, |
Haojian Wu | f429ab6 | 2019-07-24 07:49:23 +0000 | [diff] [blame] | 623 | {"renameProvider", std::move(RenameProvider)}, |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 624 | {"selectionRangeProvider", true}, |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 625 | {"documentSymbolProvider", true}, |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 626 | {"workspaceSymbolProvider", true}, |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 627 | {"referencesProvider", true}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 628 | {"executeCommandProvider", |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 629 | llvm::json::Object{ |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 630 | {"commands", |
| 631 | {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND, |
| 632 | ExecuteCommandParams::CLANGD_APPLY_TWEAK}}, |
Sam McCall | 0930ab0 | 2017-11-07 15:49:35 +0000 | [diff] [blame] | 633 | }}, |
Kadir Cetinkaya | 8665802 | 2019-03-19 09:27:04 +0000 | [diff] [blame] | 634 | {"typeHierarchyProvider", true}, |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 635 | }}}}; |
| 636 | if (NegotiatedOffsetEncoding) |
| 637 | Result["offsetEncoding"] = *NegotiatedOffsetEncoding; |
Sam McCall | fc83010 | 2020-04-01 12:02:28 +0200 | [diff] [blame] | 638 | if (ClangdServerOpts.TheiaSemanticHighlighting) |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 639 | Result.getObject("capabilities") |
| 640 | ->insert( |
| 641 | {"semanticHighlighting", |
Haojian Wu | 1ca2ee4 | 2019-07-04 12:27:21 +0000 | [diff] [blame] | 642 | llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}}); |
Kirill Bobyrev | 7a514c9 | 2020-07-14 09:28:38 +0200 | [diff] [blame] | 643 | if (ClangdServerOpts.FoldingRanges) |
| 644 | Result.getObject("capabilities")->insert({"foldingRangeProvider", true}); |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 645 | Reply(std::move(Result)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Sam McCall | 8a2d294 | 2020-03-03 12:12:14 +0100 | [diff] [blame] | 648 | void ClangdLSPServer::onInitialized(const InitializedParams &Params) {} |
| 649 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 650 | void ClangdLSPServer::onShutdown(const ShutdownParams &Params, |
| 651 | Callback<std::nullptr_t> Reply) { |
Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 652 | // Do essentially nothing, just say we're ready to exit. |
| 653 | ShutdownRequestReceived = true; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 654 | Reply(nullptr); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 655 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 656 | |
Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame] | 657 | // sync is a clangd extension: it blocks until all background work completes. |
| 658 | // It blocks the calling thread, so no messages are processed until it returns! |
| 659 | void ClangdLSPServer::onSync(const NoParams &Params, |
| 660 | Callback<std::nullptr_t> Reply) { |
| 661 | if (Server->blockUntilIdleForTest(/*TimeoutSeconds=*/60)) |
| 662 | Reply(nullptr); |
| 663 | else |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 664 | Reply(llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 665 | "Not idle after a minute")); |
Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 668 | void ClangdLSPServer::onDocumentDidOpen( |
| 669 | const DidOpenTextDocumentParams &Params) { |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 670 | PathRef File = Params.textDocument.uri.file(); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 671 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 672 | const std::string &Contents = Params.textDocument.text; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 673 | |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 674 | auto Version = DraftMgr.addDraft(File, Params.textDocument.version, Contents); |
| 675 | Server->addDocument(File, Contents, encodeVersion(Version), |
| 676 | WantDiagnostics::Yes); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 679 | void ClangdLSPServer::onDocumentDidChange( |
| 680 | const DidChangeTextDocumentParams &Params) { |
Eric Liu | 51fed18 | 2018-02-22 18:40:39 +0000 | [diff] [blame] | 681 | auto WantDiags = WantDiagnostics::Auto; |
| 682 | if (Params.wantDiagnostics.hasValue()) |
| 683 | WantDiags = Params.wantDiagnostics.getValue() ? WantDiagnostics::Yes |
| 684 | : WantDiagnostics::No; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 685 | |
| 686 | PathRef File = Params.textDocument.uri.file(); |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 687 | llvm::Expected<DraftStore::Draft> Draft = DraftMgr.updateDraft( |
| 688 | File, Params.textDocument.version, Params.contentChanges); |
| 689 | if (!Draft) { |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 690 | // If this fails, we are most likely going to be not in sync anymore with |
| 691 | // the client. It is better to remove the draft and let further operations |
| 692 | // fail rather than giving wrong results. |
| 693 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 694 | Server->removeDocument(File); |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 695 | elog("Failed to update {0}: {1}", File, Draft.takeError()); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 696 | return; |
| 697 | } |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 698 | |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 699 | Server->addDocument(File, Draft->Contents, encodeVersion(Draft->Version), |
| 700 | WantDiags, Params.forceRebuild); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 703 | void ClangdLSPServer::onDocumentDidSave( |
| 704 | const DidSaveTextDocumentParams &Params) { |
| 705 | reparseOpenFilesIfNeeded([](llvm::StringRef) { return true; }); |
| 706 | } |
| 707 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 708 | void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 709 | // We could also reparse all open files here. However: |
| 710 | // - this could be frequent, and revalidating all the preambles isn't free |
| 711 | // - this is useful e.g. when switching git branches, but we're likely to see |
| 712 | // fresh headers but still have the old-branch main-file content |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 713 | Server->onFileEvent(Params); |
Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 716 | void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 717 | Callback<llvm::json::Value> Reply) { |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 718 | auto ApplyEdit = [this](WorkspaceEdit WE, std::string SuccessMessage, |
| 719 | decltype(Reply) Reply) { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 720 | ApplyWorkspaceEditParams Edit; |
| 721 | Edit.edit = std::move(WE); |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 722 | call<ApplyWorkspaceEditResponse>( |
| 723 | "workspace/applyEdit", std::move(Edit), |
| 724 | [Reply = std::move(Reply), SuccessMessage = std::move(SuccessMessage)]( |
| 725 | llvm::Expected<ApplyWorkspaceEditResponse> Response) mutable { |
| 726 | if (!Response) |
| 727 | return Reply(Response.takeError()); |
| 728 | if (!Response->applied) { |
| 729 | std::string Reason = Response->failureReason |
| 730 | ? *Response->failureReason |
| 731 | : "unknown reason"; |
| 732 | return Reply(llvm::createStringError( |
| 733 | llvm::inconvertibleErrorCode(), |
| 734 | ("edits were not applied: " + Reason).c_str())); |
| 735 | } |
| 736 | return Reply(SuccessMessage); |
| 737 | }); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 738 | }; |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 739 | |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 740 | if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && |
| 741 | Params.workspaceEdit) { |
| 742 | // The flow for "apply-fix" : |
| 743 | // 1. We publish a diagnostic, including fixits |
| 744 | // 2. The user clicks on the diagnostic, the editor asks us for code actions |
| 745 | // 3. We send code actions, with the fixit embedded as context |
| 746 | // 4. The user selects the fixit, the editor asks us to apply it |
| 747 | // 5. We unwrap the changes and send them back to the editor |
Haojian Wu | f251634 | 2019-08-05 12:48:09 +0000 | [diff] [blame] | 748 | // 6. The editor applies the changes (applyEdit), and sends us a reply |
| 749 | // 7. We unwrap the reply and send a reply to the editor. |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 750 | ApplyEdit(*Params.workspaceEdit, "Fix applied.", std::move(Reply)); |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 751 | } else if (Params.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK && |
| 752 | Params.tweakArgs) { |
| 753 | auto Code = DraftMgr.getDraft(Params.tweakArgs->file.file()); |
| 754 | if (!Code) |
| 755 | return Reply(llvm::createStringError( |
| 756 | llvm::inconvertibleErrorCode(), |
| 757 | "trying to apply a code action for a non-added file")); |
| 758 | |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 759 | auto Action = [this, ApplyEdit, Reply = std::move(Reply), |
| 760 | File = Params.tweakArgs->file, Code = std::move(*Code)]( |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 761 | llvm::Expected<Tweak::Effect> R) mutable { |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 762 | if (!R) |
| 763 | return Reply(R.takeError()); |
| 764 | |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 765 | assert(R->ShowMessage || |
| 766 | (!R->ApplyEdits.empty() && "tweak has no effect")); |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 767 | |
Sam McCall | 395fde7 | 2019-06-18 13:37:54 +0000 | [diff] [blame] | 768 | if (R->ShowMessage) { |
| 769 | ShowMessageParams Msg; |
| 770 | Msg.message = *R->ShowMessage; |
| 771 | Msg.type = MessageType::Info; |
| 772 | notify("window/showMessage", Msg); |
| 773 | } |
Ilya Biryukov | 1286400 | 2019-08-16 12:46:41 +0000 | [diff] [blame] | 774 | // When no edit is specified, make sure we Reply(). |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 775 | if (R->ApplyEdits.empty()) |
| 776 | return Reply("Tweak applied."); |
| 777 | |
Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 778 | if (auto Err = validateEdits(DraftMgr, R->ApplyEdits)) |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 779 | return Reply(std::move(Err)); |
| 780 | |
| 781 | WorkspaceEdit WE; |
| 782 | WE.changes.emplace(); |
| 783 | for (const auto &It : R->ApplyEdits) { |
Kadir Cetinkaya | e95e516 | 2019-10-02 09:12:01 +0000 | [diff] [blame] | 784 | (*WE.changes)[URI::createFile(It.first()).toString()] = |
Kadir Cetinkaya | 5b27093 | 2019-09-09 12:28:44 +0000 | [diff] [blame] | 785 | It.second.asTextEdits(); |
| 786 | } |
| 787 | // ApplyEdit will take care of calling Reply(). |
| 788 | return ApplyEdit(std::move(WE), "Tweak applied.", std::move(Reply)); |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 789 | }; |
| 790 | Server->applyTweak(Params.tweakArgs->file.file(), |
| 791 | Params.tweakArgs->selection, Params.tweakArgs->tweakID, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 792 | std::move(Action)); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 793 | } else { |
| 794 | // We should not get here because ExecuteCommandParams would not have |
| 795 | // parsed in the first place and this handler should not be called. But if |
| 796 | // more commands are added, this will be here has a safe guard. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 797 | Reply(llvm::make_error<LSPError>( |
| 798 | llvm::formatv("Unsupported command \"{0}\".", Params.command).str(), |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 799 | ErrorCode::InvalidParams)); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 803 | void ClangdLSPServer::onWorkspaceSymbol( |
| 804 | const WorkspaceSymbolParams &Params, |
| 805 | Callback<std::vector<SymbolInformation>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 806 | Server->workspaceSymbols( |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 807 | Params.query, CCOpts.Limit, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 808 | [Reply = std::move(Reply), |
| 809 | this](llvm::Expected<std::vector<SymbolInformation>> Items) mutable { |
| 810 | if (!Items) |
| 811 | return Reply(Items.takeError()); |
| 812 | for (auto &Sym : *Items) |
| 813 | Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds); |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 814 | |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 815 | Reply(std::move(*Items)); |
| 816 | }); |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Haojian Wu | f429ab6 | 2019-07-24 07:49:23 +0000 | [diff] [blame] | 819 | void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params, |
| 820 | Callback<llvm::Optional<Range>> Reply) { |
| 821 | Server->prepareRename(Params.textDocument.uri.file(), Params.position, |
Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 822 | RenameOpts, std::move(Reply)); |
Haojian Wu | f429ab6 | 2019-07-24 07:49:23 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 825 | void ClangdLSPServer::onRename(const RenameParams &Params, |
| 826 | Callback<WorkspaceEdit> Reply) { |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 827 | Path File = std::string(Params.textDocument.uri.file()); |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 828 | if (!DraftMgr.getDraft(File)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 829 | return Reply(llvm::make_error<LSPError>( |
| 830 | "onRename called for non-added file", ErrorCode::InvalidParams)); |
Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 831 | Server->rename( |
Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 832 | File, Params.position, Params.newName, RenameOpts, |
Haojian Wu | 852bafa | 2019-10-23 14:40:20 +0200 | [diff] [blame] | 833 | [File, Params, Reply = std::move(Reply), |
| 834 | this](llvm::Expected<FileEdits> Edits) mutable { |
| 835 | if (!Edits) |
| 836 | return Reply(Edits.takeError()); |
| 837 | if (auto Err = validateEdits(DraftMgr, *Edits)) |
| 838 | return Reply(std::move(Err)); |
| 839 | WorkspaceEdit Result; |
| 840 | Result.changes.emplace(); |
| 841 | for (const auto &Rep : *Edits) { |
| 842 | (*Result.changes)[URI::createFile(Rep.first()).toString()] = |
| 843 | Rep.second.asTextEdits(); |
| 844 | } |
| 845 | Reply(Result); |
| 846 | }); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 849 | void ClangdLSPServer::onDocumentDidClose( |
| 850 | const DidCloseTextDocumentParams &Params) { |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 851 | PathRef File = Params.textDocument.uri.file(); |
| 852 | DraftMgr.removeDraft(File); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 853 | Server->removeDocument(File); |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 854 | |
| 855 | { |
| 856 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 857 | FixItsMap.erase(File); |
| 858 | } |
Johan Vikstrom | c2653ef2 | 2019-08-01 08:08:44 +0000 | [diff] [blame] | 859 | { |
| 860 | std::lock_guard<std::mutex> HLock(HighlightingsMutex); |
| 861 | FileToHighlightings.erase(File); |
| 862 | } |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 863 | { |
| 864 | std::lock_guard<std::mutex> HLock(SemanticTokensMutex); |
| 865 | LastSemanticTokens.erase(File); |
| 866 | } |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 867 | // clangd will not send updates for this file anymore, so we empty out the |
| 868 | // list of diagnostics shown on the client (e.g. in the "Problems" pane of |
| 869 | // VSCode). Note that this cannot race with actual diagnostics responses |
| 870 | // because removeDocument() guarantees no diagnostic callbacks will be |
| 871 | // executed after it returns. |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 872 | PublishDiagnosticsParams Notification; |
| 873 | Notification.uri = URIForFile::canonicalize(File, /*TUPath=*/File); |
| 874 | publishDiagnostics(Notification); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 877 | void ClangdLSPServer::onDocumentOnTypeFormatting( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 878 | const DocumentOnTypeFormattingParams &Params, |
| 879 | Callback<std::vector<TextEdit>> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 880 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 881 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 882 | if (!Code) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 883 | return Reply(llvm::make_error<LSPError>( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 884 | "onDocumentOnTypeFormatting called for non-added file", |
| 885 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 886 | |
Sam McCall | ffa63dd | 2020-06-26 12:57:29 +0200 | [diff] [blame] | 887 | Server->formatOnType(File, Code->Contents, Params.position, Params.ch, |
| 888 | std::move(Reply)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 891 | void ClangdLSPServer::onDocumentRangeFormatting( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 892 | const DocumentRangeFormattingParams &Params, |
| 893 | Callback<std::vector<TextEdit>> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 894 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 895 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 896 | if (!Code) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 897 | return Reply(llvm::make_error<LSPError>( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 898 | "onDocumentRangeFormatting called for non-added file", |
| 899 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 900 | |
Sam McCall | ffa63dd | 2020-06-26 12:57:29 +0200 | [diff] [blame] | 901 | Server->formatRange( |
| 902 | File, Code->Contents, Params.range, |
| 903 | [Code = Code->Contents, Reply = std::move(Reply)]( |
| 904 | llvm::Expected<tooling::Replacements> Result) mutable { |
| 905 | if (Result) |
| 906 | Reply(replacementsToEdits(Code, Result.get())); |
| 907 | else |
| 908 | Reply(Result.takeError()); |
| 909 | }); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 912 | void ClangdLSPServer::onDocumentFormatting( |
| 913 | const DocumentFormattingParams &Params, |
| 914 | Callback<std::vector<TextEdit>> Reply) { |
Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 915 | auto File = Params.textDocument.uri.file(); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 916 | auto Code = DraftMgr.getDraft(File); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 917 | if (!Code) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 918 | return Reply(llvm::make_error<LSPError>( |
| 919 | "onDocumentFormatting called for non-added file", |
| 920 | ErrorCode::InvalidParams)); |
Ilya Biryukov | 261c72e | 2018-01-17 12:30:24 +0000 | [diff] [blame] | 921 | |
Sam McCall | ffa63dd | 2020-06-26 12:57:29 +0200 | [diff] [blame] | 922 | Server->formatFile(File, Code->Contents, |
| 923 | [Code = Code->Contents, Reply = std::move(Reply)]( |
| 924 | llvm::Expected<tooling::Replacements> Result) mutable { |
| 925 | if (Result) |
| 926 | Reply(replacementsToEdits(Code, Result.get())); |
| 927 | else |
| 928 | Reply(Result.takeError()); |
| 929 | }); |
Sam McCall | 4db732a | 2017-09-30 10:08:52 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 932 | /// The functions constructs a flattened view of the DocumentSymbol hierarchy. |
| 933 | /// Used by the clients that do not support the hierarchical view. |
| 934 | static std::vector<SymbolInformation> |
| 935 | flattenSymbolHierarchy(llvm::ArrayRef<DocumentSymbol> Symbols, |
| 936 | const URIForFile &FileURI) { |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 937 | std::vector<SymbolInformation> Results; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 938 | std::function<void(const DocumentSymbol &, llvm::StringRef)> Process = |
| 939 | [&](const DocumentSymbol &S, llvm::Optional<llvm::StringRef> ParentName) { |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 940 | SymbolInformation SI; |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 941 | SI.containerName = std::string(ParentName ? "" : *ParentName); |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 942 | SI.name = S.name; |
| 943 | SI.kind = S.kind; |
| 944 | SI.location.range = S.range; |
| 945 | SI.location.uri = FileURI; |
| 946 | |
| 947 | Results.push_back(std::move(SI)); |
| 948 | std::string FullName = |
| 949 | !ParentName ? S.name : (ParentName->str() + "::" + S.name); |
| 950 | for (auto &C : S.children) |
| 951 | Process(C, /*ParentName=*/FullName); |
| 952 | }; |
| 953 | for (auto &S : Symbols) |
| 954 | Process(S, /*ParentName=*/""); |
| 955 | return Results; |
| 956 | } |
| 957 | |
| 958 | void ClangdLSPServer::onDocumentSymbol(const DocumentSymbolParams &Params, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 959 | Callback<llvm::json::Value> Reply) { |
Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 960 | URIForFile FileURI = Params.textDocument.uri; |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 961 | Server->documentSymbols( |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 962 | Params.textDocument.uri.file(), |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 963 | [this, FileURI, Reply = std::move(Reply)]( |
| 964 | llvm::Expected<std::vector<DocumentSymbol>> Items) mutable { |
| 965 | if (!Items) |
| 966 | return Reply(Items.takeError()); |
| 967 | adjustSymbolKinds(*Items, SupportedSymbolKinds); |
| 968 | if (SupportsHierarchicalDocumentSymbol) |
| 969 | return Reply(std::move(*Items)); |
| 970 | else |
| 971 | return Reply(flattenSymbolHierarchy(*Items, FileURI)); |
| 972 | }); |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Kirill Bobyrev | 7a514c9 | 2020-07-14 09:28:38 +0200 | [diff] [blame] | 975 | void ClangdLSPServer::onFoldingRange( |
| 976 | const FoldingRangeParams &Params, |
| 977 | Callback<std::vector<FoldingRange>> Reply) { |
| 978 | Server->foldingRanges(Params.textDocument.uri.file(), std::move(Reply)); |
| 979 | } |
| 980 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 981 | static llvm::Optional<Command> asCommand(const CodeAction &Action) { |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 982 | Command Cmd; |
| 983 | if (Action.command && Action.edit) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 984 | return None; // Not representable. (We never emit these anyway). |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 985 | if (Action.command) { |
| 986 | Cmd = *Action.command; |
| 987 | } else if (Action.edit) { |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 988 | Cmd.command = std::string(Command::CLANGD_APPLY_FIX_COMMAND); |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 989 | Cmd.workspaceEdit = *Action.edit; |
| 990 | } else { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 991 | return None; |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 992 | } |
| 993 | Cmd.title = Action.title; |
| 994 | if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) |
| 995 | Cmd.title = "Apply fix: " + Cmd.title; |
| 996 | return Cmd; |
| 997 | } |
| 998 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 999 | void ClangdLSPServer::onCodeAction(const CodeActionParams &Params, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 1000 | Callback<llvm::json::Value> Reply) { |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 1001 | URIForFile File = Params.textDocument.uri; |
| 1002 | auto Code = DraftMgr.getDraft(File.file()); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1003 | if (!Code) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 1004 | return Reply(llvm::make_error<LSPError>( |
| 1005 | "onCodeAction called for non-added file", ErrorCode::InvalidParams)); |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 1006 | // We provide a code action for Fixes on the specified diagnostics. |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 1007 | std::vector<CodeAction> FixIts; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1008 | for (const Diagnostic &D : Params.context.diagnostics) { |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 1009 | for (auto &F : getFixes(File.file(), D)) { |
| 1010 | FixIts.push_back(toCodeAction(F, Params.textDocument.uri)); |
| 1011 | FixIts.back().diagnostics = {D}; |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 1012 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 1013 | } |
Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 1014 | |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 1015 | // Now enumerate the semantic code actions. |
| 1016 | auto ConsumeActions = |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1017 | [Reply = std::move(Reply), File, Code = std::move(*Code), |
| 1018 | Selection = Params.range, FixIts = std::move(FixIts), this]( |
| 1019 | llvm::Expected<std::vector<ClangdServer::TweakRef>> Tweaks) mutable { |
Ilya Biryukov | c6ed778 | 2019-01-30 14:24:17 +0000 | [diff] [blame] | 1020 | if (!Tweaks) |
| 1021 | return Reply(Tweaks.takeError()); |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 1022 | |
| 1023 | std::vector<CodeAction> Actions = std::move(FixIts); |
| 1024 | Actions.reserve(Actions.size() + Tweaks->size()); |
| 1025 | for (const auto &T : *Tweaks) |
| 1026 | Actions.push_back(toCodeAction(T, File, Selection)); |
| 1027 | |
| 1028 | if (SupportsCodeAction) |
| 1029 | return Reply(llvm::json::Array(Actions)); |
| 1030 | std::vector<Command> Commands; |
| 1031 | for (const auto &Action : Actions) { |
| 1032 | if (auto Command = asCommand(Action)) |
| 1033 | Commands.push_back(std::move(*Command)); |
| 1034 | } |
| 1035 | return Reply(llvm::json::Array(Commands)); |
| 1036 | }; |
| 1037 | |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1038 | Server->enumerateTweaks(File.file(), Params.range, std::move(ConsumeActions)); |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 1041 | void ClangdLSPServer::onCompletion(const CompletionParams &Params, |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1042 | Callback<CompletionList> Reply) { |
Ilya Biryukov | a7a1147 | 2019-06-07 16:24:38 +0000 | [diff] [blame] | 1043 | if (!shouldRunCompletion(Params)) { |
| 1044 | // Clients sometimes auto-trigger completions in undesired places (e.g. |
| 1045 | // 'a >^ '), we return empty results in those cases. |
| 1046 | vlog("ignored auto-triggered completion, preceding char did not match"); |
| 1047 | return Reply(CompletionList()); |
| 1048 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 1049 | Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1050 | [Reply = std::move(Reply), |
| 1051 | this](llvm::Expected<CodeCompleteResult> List) mutable { |
| 1052 | if (!List) |
| 1053 | return Reply(List.takeError()); |
| 1054 | CompletionList LSPList; |
| 1055 | LSPList.isIncomplete = List->HasMore; |
| 1056 | for (const auto &R : List->Completions) { |
| 1057 | CompletionItem C = R.render(CCOpts); |
| 1058 | C.kind = adjustKindToCapability( |
| 1059 | C.kind, SupportedCompletionItemKinds); |
| 1060 | LSPList.items.push_back(std::move(C)); |
| 1061 | } |
| 1062 | return Reply(std::move(LSPList)); |
| 1063 | }); |
Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1066 | void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params, |
| 1067 | Callback<SignatureHelp> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 1068 | Server->signatureHelp(Params.textDocument.uri.file(), Params.position, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1069 | [Reply = std::move(Reply), this]( |
| 1070 | llvm::Expected<SignatureHelp> Signature) mutable { |
| 1071 | if (!Signature) |
| 1072 | return Reply(Signature.takeError()); |
| 1073 | if (SupportsOffsetsInSignatureHelp) |
| 1074 | return Reply(std::move(*Signature)); |
| 1075 | // Strip out the offsets from signature help for |
| 1076 | // clients that only support string labels. |
| 1077 | for (auto &SigInfo : Signature->signatures) { |
| 1078 | for (auto &Param : SigInfo.parameters) |
| 1079 | Param.labelOffsets.reset(); |
| 1080 | } |
| 1081 | return Reply(std::move(*Signature)); |
| 1082 | }); |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Sam McCall | 0dbab7f | 2019-02-02 05:56:00 +0000 | [diff] [blame] | 1085 | // Go to definition has a toggle function: if def and decl are distinct, then |
| 1086 | // the first press gives you the def, the second gives you the matching def. |
| 1087 | // getToggle() returns the counterpart location that under the cursor. |
| 1088 | // |
| 1089 | // We return the toggled location alone (ignoring other symbols) to encourage |
| 1090 | // editors to "bounce" quickly between locations, without showing a menu. |
| 1091 | static Location *getToggle(const TextDocumentPositionParams &Point, |
| 1092 | LocatedSymbol &Sym) { |
| 1093 | // Toggle only makes sense with two distinct locations. |
| 1094 | if (!Sym.Definition || *Sym.Definition == Sym.PreferredDeclaration) |
| 1095 | return nullptr; |
| 1096 | if (Sym.Definition->uri.file() == Point.textDocument.uri.file() && |
| 1097 | Sym.Definition->range.contains(Point.position)) |
| 1098 | return &Sym.PreferredDeclaration; |
| 1099 | if (Sym.PreferredDeclaration.uri.file() == Point.textDocument.uri.file() && |
| 1100 | Sym.PreferredDeclaration.range.contains(Point.position)) |
| 1101 | return &*Sym.Definition; |
| 1102 | return nullptr; |
| 1103 | } |
| 1104 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1105 | void ClangdLSPServer::onGoToDefinition(const TextDocumentPositionParams &Params, |
| 1106 | Callback<std::vector<Location>> Reply) { |
Sam McCall | 866ba2c | 2019-02-01 11:26:13 +0000 | [diff] [blame] | 1107 | Server->locateSymbolAt( |
| 1108 | Params.textDocument.uri.file(), Params.position, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1109 | [Params, Reply = std::move(Reply)]( |
| 1110 | llvm::Expected<std::vector<LocatedSymbol>> Symbols) mutable { |
| 1111 | if (!Symbols) |
| 1112 | return Reply(Symbols.takeError()); |
| 1113 | std::vector<Location> Defs; |
| 1114 | for (auto &S : *Symbols) { |
| 1115 | if (Location *Toggle = getToggle(Params, S)) |
| 1116 | return Reply(std::vector<Location>{std::move(*Toggle)}); |
| 1117 | Defs.push_back(S.Definition.getValueOr(S.PreferredDeclaration)); |
| 1118 | } |
| 1119 | Reply(std::move(Defs)); |
| 1120 | }); |
Sam McCall | 866ba2c | 2019-02-01 11:26:13 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void ClangdLSPServer::onGoToDeclaration( |
| 1124 | const TextDocumentPositionParams &Params, |
| 1125 | Callback<std::vector<Location>> Reply) { |
| 1126 | Server->locateSymbolAt( |
| 1127 | Params.textDocument.uri.file(), Params.position, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1128 | [Params, Reply = std::move(Reply)]( |
| 1129 | llvm::Expected<std::vector<LocatedSymbol>> Symbols) mutable { |
| 1130 | if (!Symbols) |
| 1131 | return Reply(Symbols.takeError()); |
| 1132 | std::vector<Location> Decls; |
| 1133 | for (auto &S : *Symbols) { |
| 1134 | if (Location *Toggle = getToggle(Params, S)) |
| 1135 | return Reply(std::vector<Location>{std::move(*Toggle)}); |
| 1136 | Decls.push_back(std::move(S.PreferredDeclaration)); |
| 1137 | } |
| 1138 | Reply(std::move(Decls)); |
| 1139 | }); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Sam McCall | 111fe84 | 2019-05-07 07:55:35 +0000 | [diff] [blame] | 1142 | void ClangdLSPServer::onSwitchSourceHeader( |
| 1143 | const TextDocumentIdentifier &Params, |
Sam McCall | b9ec3e9 | 2019-05-07 08:30:32 +0000 | [diff] [blame] | 1144 | Callback<llvm::Optional<URIForFile>> Reply) { |
Haojian Wu | d6d5edd | 2019-10-01 10:21:15 +0000 | [diff] [blame] | 1145 | Server->switchSourceHeader( |
| 1146 | Params.uri.file(), |
| 1147 | [Reply = std::move(Reply), |
| 1148 | Params](llvm::Expected<llvm::Optional<clangd::Path>> Path) mutable { |
| 1149 | if (!Path) |
| 1150 | return Reply(Path.takeError()); |
| 1151 | if (*Path) |
Haojian Wu | 77c9700 | 2019-10-07 11:37:25 +0000 | [diff] [blame] | 1152 | return Reply(URIForFile::canonicalize(**Path, Params.uri.file())); |
Haojian Wu | d6d5edd | 2019-10-01 10:21:15 +0000 | [diff] [blame] | 1153 | return Reply(llvm::None); |
| 1154 | }); |
Marc-Andre Laperle | 6571b3e | 2017-09-28 03:14:40 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1157 | void ClangdLSPServer::onDocumentHighlight( |
| 1158 | const TextDocumentPositionParams &Params, |
| 1159 | Callback<std::vector<DocumentHighlight>> Reply) { |
| 1160 | Server->findDocumentHighlights(Params.textDocument.uri.file(), |
| 1161 | Params.position, std::move(Reply)); |
Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1164 | void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 1165 | Callback<llvm::Optional<Hover>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 1166 | Server->findHover(Params.textDocument.uri.file(), Params.position, |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1167 | [Reply = std::move(Reply), this]( |
| 1168 | llvm::Expected<llvm::Optional<HoverInfo>> H) mutable { |
| 1169 | if (!H) |
| 1170 | return Reply(H.takeError()); |
| 1171 | if (!*H) |
| 1172 | return Reply(llvm::None); |
Ilya Biryukov | f9169d0 | 2019-05-29 10:01:00 +0000 | [diff] [blame] | 1173 | |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1174 | Hover R; |
| 1175 | R.contents.kind = HoverContentFormat; |
| 1176 | R.range = (*H)->SymRange; |
| 1177 | switch (HoverContentFormat) { |
| 1178 | case MarkupKind::PlainText: |
Kadir Cetinkaya | 597c6b6 | 2019-12-10 10:28:37 +0100 | [diff] [blame] | 1179 | R.contents.value = (*H)->present().asPlainText(); |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1180 | return Reply(std::move(R)); |
| 1181 | case MarkupKind::Markdown: |
Kadir Cetinkaya | 597c6b6 | 2019-12-10 10:28:37 +0100 | [diff] [blame] | 1182 | R.contents.value = (*H)->present().asMarkdown(); |
Benjamin Kramer | 9880b5d | 2019-08-15 14:16:06 +0000 | [diff] [blame] | 1183 | return Reply(std::move(R)); |
| 1184 | }; |
| 1185 | llvm_unreachable("unhandled MarkupKind"); |
| 1186 | }); |
Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Kadir Cetinkaya | 8665802 | 2019-03-19 09:27:04 +0000 | [diff] [blame] | 1189 | void ClangdLSPServer::onTypeHierarchy( |
| 1190 | const TypeHierarchyParams &Params, |
| 1191 | Callback<Optional<TypeHierarchyItem>> Reply) { |
| 1192 | Server->typeHierarchy(Params.textDocument.uri.file(), Params.position, |
| 1193 | Params.resolve, Params.direction, std::move(Reply)); |
| 1194 | } |
| 1195 | |
Nathan Ridge | 087b044 | 2019-07-13 03:24:48 +0000 | [diff] [blame] | 1196 | void ClangdLSPServer::onResolveTypeHierarchy( |
| 1197 | const ResolveTypeHierarchyItemParams &Params, |
| 1198 | Callback<Optional<TypeHierarchyItem>> Reply) { |
| 1199 | Server->resolveTypeHierarchy(Params.item, Params.resolve, Params.direction, |
| 1200 | std::move(Reply)); |
| 1201 | } |
| 1202 | |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 1203 | void ClangdLSPServer::applyConfiguration( |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 1204 | const ConfigurationSettings &Settings) { |
Simon Marchi | abeed66 | 2018-10-16 15:55:03 +0000 | [diff] [blame] | 1205 | // Per-file update to the compilation database. |
David Goldman | 60249c2 | 2020-01-13 17:01:10 -0500 | [diff] [blame] | 1206 | llvm::StringSet<> ModifiedFiles; |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 1207 | for (auto &Entry : Settings.compilationDatabaseChanges) { |
Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 1208 | PathRef File = Entry.first; |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 1209 | auto Old = CDB->getCompileCommand(File); |
| 1210 | auto New = |
| 1211 | tooling::CompileCommand(std::move(Entry.second.workingDirectory), File, |
| 1212 | std::move(Entry.second.compilationCommand), |
| 1213 | /*Output=*/""); |
Sam McCall | 6980edb | 2018-11-02 14:07:51 +0000 | [diff] [blame] | 1214 | if (Old != New) { |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 1215 | CDB->setCompileCommand(File, std::move(New)); |
David Goldman | 60249c2 | 2020-01-13 17:01:10 -0500 | [diff] [blame] | 1216 | ModifiedFiles.insert(File); |
Sam McCall | 6980edb | 2018-11-02 14:07:51 +0000 | [diff] [blame] | 1217 | } |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 1218 | } |
David Goldman | 60249c2 | 2020-01-13 17:01:10 -0500 | [diff] [blame] | 1219 | |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 1220 | reparseOpenFilesIfNeeded( |
| 1221 | [&](llvm::StringRef File) { return ModifiedFiles.count(File) != 0; }); |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Sam McCall | edf6a19 | 2020-03-24 00:31:14 +0100 | [diff] [blame] | 1224 | void ClangdLSPServer::publishTheiaSemanticHighlighting( |
| 1225 | const TheiaSemanticHighlightingParams &Params) { |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 1226 | notify("textDocument/semanticHighlighting", Params); |
| 1227 | } |
| 1228 | |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 1229 | void ClangdLSPServer::publishDiagnostics( |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 1230 | const PublishDiagnosticsParams &Params) { |
| 1231 | notify("textDocument/publishDiagnostics", Params); |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 1234 | // FIXME: This function needs to be properly tested. |
| 1235 | void ClangdLSPServer::onChangeConfiguration( |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1236 | const DidChangeConfigurationParams &Params) { |
Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 1237 | applyConfiguration(Params.settings); |
| 1238 | } |
| 1239 | |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1240 | void ClangdLSPServer::onReference(const ReferenceParams &Params, |
| 1241 | Callback<std::vector<Location>> Reply) { |
Ilya Biryukov | 652364b | 2018-09-26 05:48:29 +0000 | [diff] [blame] | 1242 | Server->findReferences(Params.textDocument.uri.file(), Params.position, |
Haojian Wu | 5181ada | 2019-11-18 11:35:00 +0100 | [diff] [blame] | 1243 | CCOpts.Limit, |
| 1244 | [Reply = std::move(Reply)]( |
| 1245 | llvm::Expected<ReferencesResult> Refs) mutable { |
| 1246 | if (!Refs) |
| 1247 | return Reply(Refs.takeError()); |
| 1248 | return Reply(std::move(Refs->References)); |
| 1249 | }); |
Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 1252 | void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params, |
| 1253 | Callback<std::vector<SymbolDetails>> Reply) { |
| 1254 | Server->symbolInfo(Params.textDocument.uri.file(), Params.position, |
| 1255 | std::move(Reply)); |
| 1256 | } |
| 1257 | |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 1258 | void ClangdLSPServer::onSelectionRange( |
| 1259 | const SelectionRangeParams &Params, |
| 1260 | Callback<std::vector<SelectionRange>> Reply) { |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 1261 | Server->semanticRanges( |
Sam McCall | 8f237f9 | 2020-03-25 00:51:50 +0100 | [diff] [blame] | 1262 | Params.textDocument.uri.file(), Params.positions, |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 1263 | [Reply = std::move(Reply)]( |
Sam McCall | 8f237f9 | 2020-03-25 00:51:50 +0100 | [diff] [blame] | 1264 | llvm::Expected<std::vector<SelectionRange>> Ranges) mutable { |
| 1265 | if (!Ranges) |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 1266 | return Reply(Ranges.takeError()); |
Sam McCall | 8f237f9 | 2020-03-25 00:51:50 +0100 | [diff] [blame] | 1267 | return Reply(std::move(*Ranges)); |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 1268 | }); |
| 1269 | } |
| 1270 | |
Sam McCall | 8d7ecc1 | 2019-12-16 19:08:51 +0100 | [diff] [blame] | 1271 | void ClangdLSPServer::onDocumentLink( |
| 1272 | const DocumentLinkParams &Params, |
| 1273 | Callback<std::vector<DocumentLink>> Reply) { |
| 1274 | |
| 1275 | // TODO(forster): This currently resolves all targets eagerly. This is slow, |
| 1276 | // because it blocks on the preamble/AST being built. We could respond to the |
| 1277 | // request faster by using string matching or the lexer to find the includes |
| 1278 | // and resolving the targets lazily. |
| 1279 | Server->documentLinks( |
| 1280 | Params.textDocument.uri.file(), |
| 1281 | [Reply = std::move(Reply)]( |
| 1282 | llvm::Expected<std::vector<DocumentLink>> Links) mutable { |
| 1283 | if (!Links) { |
| 1284 | return Reply(Links.takeError()); |
| 1285 | } |
| 1286 | return Reply(std::move(Links)); |
| 1287 | }); |
| 1288 | } |
| 1289 | |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1290 | // Increment a numeric string: "" -> 1 -> 2 -> ... -> 9 -> 10 -> 11 ... |
| 1291 | static void increment(std::string &S) { |
| 1292 | for (char &C : llvm::reverse(S)) { |
| 1293 | if (C != '9') { |
| 1294 | ++C; |
| 1295 | return; |
| 1296 | } |
| 1297 | C = '0'; |
| 1298 | } |
| 1299 | S.insert(S.begin(), '1'); |
| 1300 | } |
| 1301 | |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 1302 | void ClangdLSPServer::onSemanticTokens(const SemanticTokensParams &Params, |
| 1303 | Callback<SemanticTokens> CB) { |
| 1304 | Server->semanticHighlights( |
| 1305 | Params.textDocument.uri.file(), |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1306 | [this, File(Params.textDocument.uri.file().str()), CB(std::move(CB))]( |
| 1307 | llvm::Expected<std::vector<HighlightingToken>> HT) mutable { |
| 1308 | if (!HT) |
| 1309 | return CB(HT.takeError()); |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 1310 | SemanticTokens Result; |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1311 | Result.tokens = toSemanticTokens(*HT); |
| 1312 | { |
| 1313 | std::lock_guard<std::mutex> Lock(SemanticTokensMutex); |
Kadir Cetinkaya | e64f99c | 2020-04-16 23:12:09 +0200 | [diff] [blame] | 1314 | auto &Last = LastSemanticTokens[File]; |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1315 | |
| 1316 | Last.tokens = Result.tokens; |
| 1317 | increment(Last.resultId); |
| 1318 | Result.resultId = Last.resultId; |
| 1319 | } |
| 1320 | CB(std::move(Result)); |
| 1321 | }); |
| 1322 | } |
| 1323 | |
Sam McCall | 5fea54b | 2020-07-10 16:08:14 +0200 | [diff] [blame] | 1324 | void ClangdLSPServer::onSemanticTokensDelta( |
| 1325 | const SemanticTokensDeltaParams &Params, |
| 1326 | Callback<SemanticTokensOrDelta> CB) { |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1327 | Server->semanticHighlights( |
| 1328 | Params.textDocument.uri.file(), |
| 1329 | [this, PrevResultID(Params.previousResultId), |
| 1330 | File(Params.textDocument.uri.file().str()), CB(std::move(CB))]( |
| 1331 | llvm::Expected<std::vector<HighlightingToken>> HT) mutable { |
| 1332 | if (!HT) |
| 1333 | return CB(HT.takeError()); |
| 1334 | std::vector<SemanticToken> Toks = toSemanticTokens(*HT); |
| 1335 | |
Sam McCall | 5fea54b | 2020-07-10 16:08:14 +0200 | [diff] [blame] | 1336 | SemanticTokensOrDelta Result; |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1337 | { |
| 1338 | std::lock_guard<std::mutex> Lock(SemanticTokensMutex); |
Kadir Cetinkaya | e64f99c | 2020-04-16 23:12:09 +0200 | [diff] [blame] | 1339 | auto &Last = LastSemanticTokens[File]; |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1340 | |
| 1341 | if (PrevResultID == Last.resultId) { |
| 1342 | Result.edits = diffTokens(Last.tokens, Toks); |
| 1343 | } else { |
Sam McCall | 5fea54b | 2020-07-10 16:08:14 +0200 | [diff] [blame] | 1344 | vlog("semanticTokens/full/delta: wanted edits vs {0} but last " |
| 1345 | "result had ID {1}. Returning full token list.", |
Sam McCall | 9e3063e | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 1346 | PrevResultID, Last.resultId); |
| 1347 | Result.tokens = Toks; |
| 1348 | } |
| 1349 | |
| 1350 | Last.tokens = std::move(Toks); |
| 1351 | increment(Last.resultId); |
| 1352 | Result.resultId = Last.resultId; |
| 1353 | } |
| 1354 | |
Sam McCall | 71177ac | 2020-03-24 02:24:47 +0100 | [diff] [blame] | 1355 | CB(std::move(Result)); |
| 1356 | }); |
| 1357 | } |
| 1358 | |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 1359 | ClangdLSPServer::ClangdLSPServer( |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 1360 | class Transport &Transp, const ThreadsafeFS &TFS, |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 1361 | const clangd::CodeCompleteOptions &CCOpts, |
Haojian Wu | 34d0e1b | 2020-02-19 15:37:36 +0100 | [diff] [blame] | 1362 | const clangd::RenameOptions &RenameOpts, |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 1363 | llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, |
| 1364 | llvm::Optional<OffsetEncoding> ForcedOffsetEncoding, |
| 1365 | const ClangdServer::Options &Opts) |
Kadir Cetinkaya | 9d66247 | 2019-10-15 14:20:52 +0000 | [diff] [blame] | 1366 | : BackgroundContext(Context::current().clone()), Transp(Transp), |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 1367 | MsgHandler(new MessageHandler(*this)), TFS(TFS), CCOpts(CCOpts), |
| 1368 | RenameOpts(RenameOpts), SupportedSymbolKinds(defaultSymbolKinds()), |
Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 1369 | SupportedCompletionItemKinds(defaultCompletionItemKinds()), |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 1370 | UseDirBasedCDB(UseDirBasedCDB), |
Sam McCall | a69698f | 2019-03-27 17:47:49 +0000 | [diff] [blame] | 1371 | CompileCommandsDir(std::move(CompileCommandsDir)), ClangdServerOpts(Opts), |
| 1372 | NegotiatedOffsetEncoding(ForcedOffsetEncoding) { |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1373 | // clang-format off |
| 1374 | MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize); |
Sam McCall | 8a2d294 | 2020-03-03 12:12:14 +0100 | [diff] [blame] | 1375 | MsgHandler->bind("initialized", &ClangdLSPServer::onInitialized); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1376 | MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown); |
Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame] | 1377 | MsgHandler->bind("sync", &ClangdLSPServer::onSync); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1378 | MsgHandler->bind("textDocument/rangeFormatting", &ClangdLSPServer::onDocumentRangeFormatting); |
| 1379 | MsgHandler->bind("textDocument/onTypeFormatting", &ClangdLSPServer::onDocumentOnTypeFormatting); |
| 1380 | MsgHandler->bind("textDocument/formatting", &ClangdLSPServer::onDocumentFormatting); |
| 1381 | MsgHandler->bind("textDocument/codeAction", &ClangdLSPServer::onCodeAction); |
| 1382 | MsgHandler->bind("textDocument/completion", &ClangdLSPServer::onCompletion); |
| 1383 | MsgHandler->bind("textDocument/signatureHelp", &ClangdLSPServer::onSignatureHelp); |
| 1384 | MsgHandler->bind("textDocument/definition", &ClangdLSPServer::onGoToDefinition); |
Sam McCall | 866ba2c | 2019-02-01 11:26:13 +0000 | [diff] [blame] | 1385 | MsgHandler->bind("textDocument/declaration", &ClangdLSPServer::onGoToDeclaration); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1386 | MsgHandler->bind("textDocument/references", &ClangdLSPServer::onReference); |
| 1387 | MsgHandler->bind("textDocument/switchSourceHeader", &ClangdLSPServer::onSwitchSourceHeader); |
Haojian Wu | f429ab6 | 2019-07-24 07:49:23 +0000 | [diff] [blame] | 1388 | MsgHandler->bind("textDocument/prepareRename", &ClangdLSPServer::onPrepareRename); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1389 | MsgHandler->bind("textDocument/rename", &ClangdLSPServer::onRename); |
| 1390 | MsgHandler->bind("textDocument/hover", &ClangdLSPServer::onHover); |
| 1391 | MsgHandler->bind("textDocument/documentSymbol", &ClangdLSPServer::onDocumentSymbol); |
| 1392 | MsgHandler->bind("workspace/executeCommand", &ClangdLSPServer::onCommand); |
| 1393 | MsgHandler->bind("textDocument/documentHighlight", &ClangdLSPServer::onDocumentHighlight); |
| 1394 | MsgHandler->bind("workspace/symbol", &ClangdLSPServer::onWorkspaceSymbol); |
| 1395 | MsgHandler->bind("textDocument/didOpen", &ClangdLSPServer::onDocumentDidOpen); |
| 1396 | MsgHandler->bind("textDocument/didClose", &ClangdLSPServer::onDocumentDidClose); |
| 1397 | MsgHandler->bind("textDocument/didChange", &ClangdLSPServer::onDocumentDidChange); |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 1398 | MsgHandler->bind("textDocument/didSave", &ClangdLSPServer::onDocumentDidSave); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1399 | MsgHandler->bind("workspace/didChangeWatchedFiles", &ClangdLSPServer::onFileEvent); |
| 1400 | MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration); |
Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 1401 | MsgHandler->bind("textDocument/symbolInfo", &ClangdLSPServer::onSymbolInfo); |
Kadir Cetinkaya | 8665802 | 2019-03-19 09:27:04 +0000 | [diff] [blame] | 1402 | MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy); |
Nathan Ridge | 087b044 | 2019-07-13 03:24:48 +0000 | [diff] [blame] | 1403 | MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy); |
Utkarsh Saxena | 55925da | 2019-09-24 13:38:33 +0000 | [diff] [blame] | 1404 | MsgHandler->bind("textDocument/selectionRange", &ClangdLSPServer::onSelectionRange); |
Sam McCall | 8d7ecc1 | 2019-12-16 19:08:51 +0100 | [diff] [blame] | 1405 | MsgHandler->bind("textDocument/documentLink", &ClangdLSPServer::onDocumentLink); |
Sam McCall | 5fea54b | 2020-07-10 16:08:14 +0200 | [diff] [blame] | 1406 | MsgHandler->bind("textDocument/semanticTokens/full", &ClangdLSPServer::onSemanticTokens); |
| 1407 | MsgHandler->bind("textDocument/semanticTokens/full/delta", &ClangdLSPServer::onSemanticTokensDelta); |
Kirill Bobyrev | 7a514c9 | 2020-07-14 09:28:38 +0200 | [diff] [blame] | 1408 | if (Opts.FoldingRanges) |
| 1409 | MsgHandler->bind("textDocument/foldingRange", &ClangdLSPServer::onFoldingRange); |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1410 | // clang-format on |
| 1411 | } |
| 1412 | |
Kadir Cetinkaya | 6b85032 | 2020-03-17 19:08:23 +0100 | [diff] [blame] | 1413 | ClangdLSPServer::~ClangdLSPServer() { |
| 1414 | IsBeingDestroyed = true; |
Sam McCall | 8bda5f2 | 2019-10-23 11:11:18 +0200 | [diff] [blame] | 1415 | // Explicitly destroy ClangdServer first, blocking on threads it owns. |
| 1416 | // This ensures they don't access any other members. |
| 1417 | Server.reset(); |
| 1418 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1419 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 1420 | bool ClangdLSPServer::run() { |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 1421 | // Run the Language Server loop. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 1422 | bool CleanExit = true; |
Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 1423 | if (auto Err = Transp.loop(*MsgHandler)) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 1424 | elog("Transport error: {0}", std::move(Err)); |
| 1425 | CleanExit = false; |
| 1426 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 1427 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 1428 | return CleanExit && ShutdownRequestReceived; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 1431 | std::vector<Fix> ClangdLSPServer::getFixes(llvm::StringRef File, |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 1432 | const clangd::Diagnostic &D) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1433 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 1434 | auto DiagToFixItsIter = FixItsMap.find(File); |
| 1435 | if (DiagToFixItsIter == FixItsMap.end()) |
| 1436 | return {}; |
| 1437 | |
| 1438 | const auto &DiagToFixItsMap = DiagToFixItsIter->second; |
| 1439 | auto FixItsIter = DiagToFixItsMap.find(D); |
| 1440 | if (FixItsIter == DiagToFixItsMap.end()) |
| 1441 | return {}; |
| 1442 | |
| 1443 | return FixItsIter->second; |
| 1444 | } |
| 1445 | |
Sam McCall | 032727f | 2020-05-06 01:39:59 +0200 | [diff] [blame] | 1446 | // A completion request is sent when the user types '>' or ':', but we only |
| 1447 | // want to trigger on '->' and '::'. We check the preceeding text to make |
| 1448 | // sure it matches what we expected. |
| 1449 | // Running the lexer here would be more robust (e.g. we can detect comments |
| 1450 | // and avoid triggering completion there), but we choose to err on the side |
| 1451 | // of simplicity here. |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 1452 | bool ClangdLSPServer::shouldRunCompletion( |
| 1453 | const CompletionParams &Params) const { |
Sam McCall | 032727f | 2020-05-06 01:39:59 +0200 | [diff] [blame] | 1454 | if (Params.context.triggerKind != CompletionTriggerKind::TriggerCharacter) |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 1455 | return true; |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 1456 | auto Code = DraftMgr.getDraft(Params.textDocument.uri.file()); |
| 1457 | if (!Code) |
| 1458 | return true; // completion code will log the error for untracked doc. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 1459 | auto Offset = positionToOffset(Code->Contents, Params.position, |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 1460 | /*AllowColumnsBeyondLineLength=*/false); |
| 1461 | if (!Offset) { |
| 1462 | vlog("could not convert position '{0}' to offset for file '{1}'", |
| 1463 | Params.position, Params.textDocument.uri.file()); |
| 1464 | return true; |
| 1465 | } |
Sam McCall | 032727f | 2020-05-06 01:39:59 +0200 | [diff] [blame] | 1466 | return allowImplicitCompletion(Code->Contents, *Offset); |
Ilya Biryukov | b0826bd | 2019-01-03 13:37:12 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 1469 | void ClangdLSPServer::onHighlightingsReady( |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 1470 | PathRef File, llvm::StringRef Version, |
| 1471 | std::vector<HighlightingToken> Highlightings) { |
Johan Vikstrom | c2653ef2 | 2019-08-01 08:08:44 +0000 | [diff] [blame] | 1472 | std::vector<HighlightingToken> Old; |
| 1473 | std::vector<HighlightingToken> HighlightingsCopy = Highlightings; |
| 1474 | { |
| 1475 | std::lock_guard<std::mutex> Lock(HighlightingsMutex); |
| 1476 | Old = std::move(FileToHighlightings[File]); |
| 1477 | FileToHighlightings[File] = std::move(HighlightingsCopy); |
| 1478 | } |
| 1479 | // LSP allows us to send incremental edits of highlightings. Also need to diff |
| 1480 | // to remove highlightings from tokens that should no longer have them. |
Haojian Wu | 0a6000f | 2019-08-26 08:38:45 +0000 | [diff] [blame] | 1481 | std::vector<LineHighlightings> Diffed = diffHighlightings(Highlightings, Old); |
Sam McCall | edf6a19 | 2020-03-24 00:31:14 +0100 | [diff] [blame] | 1482 | TheiaSemanticHighlightingParams Notification; |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 1483 | Notification.TextDocument.uri = |
| 1484 | URIForFile::canonicalize(File, /*TUPath=*/File); |
| 1485 | Notification.TextDocument.version = decodeVersion(Version); |
Sam McCall | edf6a19 | 2020-03-24 00:31:14 +0100 | [diff] [blame] | 1486 | Notification.Lines = toTheiaSemanticHighlightingInformation(Diffed); |
| 1487 | publishTheiaSemanticHighlighting(Notification); |
Johan Vikstrom | a848dab | 2019-07-04 07:53:12 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 1490 | void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version, |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 1491 | std::vector<Diag> Diagnostics) { |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 1492 | PublishDiagnosticsParams Notification; |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 1493 | Notification.version = decodeVersion(Version); |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 1494 | Notification.uri = URIForFile::canonicalize(File, /*TUPath=*/File); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1495 | DiagnosticToReplacementMap LocalFixIts; // Temporary storage |
Sam McCall | a7bb0cc | 2018-03-12 23:22:35 +0000 | [diff] [blame] | 1496 | for (auto &Diag : Diagnostics) { |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 1497 | toLSPDiags(Diag, Notification.uri, DiagOpts, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 1498 | [&](clangd::Diagnostic Diag, llvm::ArrayRef<Fix> Fixes) { |
Sam McCall | 16e7070 | 2018-10-24 07:59:38 +0000 | [diff] [blame] | 1499 | auto &FixItsForDiagnostic = LocalFixIts[Diag]; |
| 1500 | llvm::copy(Fixes, std::back_inserter(FixItsForDiagnostic)); |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 1501 | Notification.diagnostics.push_back(std::move(Diag)); |
Sam McCall | 16e7070 | 2018-10-24 07:59:38 +0000 | [diff] [blame] | 1502 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // Cache FixIts |
| 1506 | { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1507 | std::lock_guard<std::mutex> Lock(FixItsMutex); |
| 1508 | FixItsMap[File] = LocalFixIts; |
| 1509 | } |
| 1510 | |
Ilya Biryukov | 49c1071 | 2019-03-25 10:15:11 +0000 | [diff] [blame] | 1511 | // Send a notification to the LSP client. |
Sam McCall | 6525a6b | 2020-03-03 12:44:40 +0100 | [diff] [blame] | 1512 | publishDiagnostics(Notification); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1513 | } |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 1514 | |
Sam McCall | 7d20e80 | 2020-01-22 19:41:45 +0100 | [diff] [blame] | 1515 | void ClangdLSPServer::onBackgroundIndexProgress( |
| 1516 | const BackgroundQueue::Stats &Stats) { |
| 1517 | static const char ProgressToken[] = "backgroundIndexProgress"; |
| 1518 | std::lock_guard<std::mutex> Lock(BackgroundIndexProgressMutex); |
| 1519 | |
| 1520 | auto NotifyProgress = [this](const BackgroundQueue::Stats &Stats) { |
| 1521 | if (BackgroundIndexProgressState != BackgroundIndexProgress::Live) { |
| 1522 | WorkDoneProgressBegin Begin; |
| 1523 | Begin.percentage = true; |
| 1524 | Begin.title = "indexing"; |
| 1525 | progress(ProgressToken, std::move(Begin)); |
| 1526 | BackgroundIndexProgressState = BackgroundIndexProgress::Live; |
| 1527 | } |
| 1528 | |
| 1529 | if (Stats.Completed < Stats.Enqueued) { |
| 1530 | assert(Stats.Enqueued > Stats.LastIdle); |
| 1531 | WorkDoneProgressReport Report; |
| 1532 | Report.percentage = 100.0 * (Stats.Completed - Stats.LastIdle) / |
| 1533 | (Stats.Enqueued - Stats.LastIdle); |
| 1534 | Report.message = |
| 1535 | llvm::formatv("{0}/{1}", Stats.Completed - Stats.LastIdle, |
| 1536 | Stats.Enqueued - Stats.LastIdle); |
| 1537 | progress(ProgressToken, std::move(Report)); |
| 1538 | } else { |
| 1539 | assert(Stats.Completed == Stats.Enqueued); |
| 1540 | progress(ProgressToken, WorkDoneProgressEnd()); |
| 1541 | BackgroundIndexProgressState = BackgroundIndexProgress::Empty; |
| 1542 | } |
| 1543 | }; |
| 1544 | |
| 1545 | switch (BackgroundIndexProgressState) { |
| 1546 | case BackgroundIndexProgress::Unsupported: |
| 1547 | return; |
| 1548 | case BackgroundIndexProgress::Creating: |
| 1549 | // Cache this update for when the progress bar is available. |
| 1550 | PendingBackgroundIndexProgress = Stats; |
| 1551 | return; |
| 1552 | case BackgroundIndexProgress::Empty: { |
| 1553 | if (BackgroundIndexSkipCreate) { |
| 1554 | NotifyProgress(Stats); |
| 1555 | break; |
| 1556 | } |
| 1557 | // Cache this update for when the progress bar is available. |
| 1558 | PendingBackgroundIndexProgress = Stats; |
| 1559 | BackgroundIndexProgressState = BackgroundIndexProgress::Creating; |
| 1560 | WorkDoneProgressCreateParams CreateRequest; |
| 1561 | CreateRequest.token = ProgressToken; |
| 1562 | call<std::nullptr_t>( |
| 1563 | "window/workDoneProgress/create", CreateRequest, |
| 1564 | [this, NotifyProgress](llvm::Expected<std::nullptr_t> E) { |
| 1565 | std::lock_guard<std::mutex> Lock(BackgroundIndexProgressMutex); |
| 1566 | if (E) { |
| 1567 | NotifyProgress(this->PendingBackgroundIndexProgress); |
| 1568 | } else { |
| 1569 | elog("Failed to create background index progress bar: {0}", |
| 1570 | E.takeError()); |
| 1571 | // give up forever rather than thrashing about |
| 1572 | BackgroundIndexProgressState = BackgroundIndexProgress::Unsupported; |
| 1573 | } |
| 1574 | }); |
| 1575 | break; |
| 1576 | } |
| 1577 | case BackgroundIndexProgress::Live: |
| 1578 | NotifyProgress(Stats); |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | |
Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame] | 1583 | void ClangdLSPServer::onFileUpdated(PathRef File, const TUStatus &Status) { |
| 1584 | if (!SupportFileStatus) |
| 1585 | return; |
| 1586 | // FIXME: we don't emit "BuildingFile" and `RunningAction`, as these |
| 1587 | // two statuses are running faster in practice, which leads the UI constantly |
| 1588 | // changing, and doesn't provide much value. We may want to emit status at a |
| 1589 | // reasonable time interval (e.g. 0.5s). |
Kadir Cetinkaya | 6b85032 | 2020-03-17 19:08:23 +0100 | [diff] [blame] | 1590 | if (Status.PreambleActivity == PreambleAction::Idle && |
| 1591 | (Status.ASTActivity.K == ASTAction::Building || |
| 1592 | Status.ASTActivity.K == ASTAction::RunningAction)) |
Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | notify("textDocument/clangd.fileStatus", Status.render(File)); |
| 1595 | } |
| 1596 | |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 1597 | void ClangdLSPServer::reparseOpenFilesIfNeeded( |
| 1598 | llvm::function_ref<bool(llvm::StringRef File)> Filter) { |
David Goldman | 60249c2 | 2020-01-13 17:01:10 -0500 | [diff] [blame] | 1599 | // Reparse only opened files that were modified. |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 1600 | for (const Path &FilePath : DraftMgr.getActiveFiles()) |
Sam McCall | 596b63a | 2020-04-10 03:27:37 +0200 | [diff] [blame] | 1601 | if (Filter(FilePath)) |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 1602 | if (auto Draft = DraftMgr.getDraft(FilePath)) // else disappeared in race? |
| 1603 | Server->addDocument(FilePath, std::move(Draft->Contents), |
| 1604 | encodeVersion(Draft->Version), |
| 1605 | WantDiagnostics::Auto); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 1606 | } |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 1607 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 1608 | } // namespace clangd |
| 1609 | } // namespace clang |