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