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