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