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