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