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