1 //===--- ClangdLSPServer.cpp - LSP server ------------------------*- C++-*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ClangdLSPServer.h"
10 #include "ClangdServer.h"
11 #include "CodeComplete.h"
12 #include "CompileCommands.h"
13 #include "Diagnostics.h"
14 #include "Feature.h"
15 #include "GlobalCompilationDatabase.h"
16 #include "LSPBinder.h"
17 #include "Protocol.h"
18 #include "SemanticHighlighting.h"
19 #include "SourceCode.h"
20 #include "TUScheduler.h"
21 #include "URI.h"
22 #include "refactor/Tweak.h"
23 #include "support/Cancellation.h"
24 #include "support/Context.h"
25 #include "support/MemoryTree.h"
26 #include "support/Trace.h"
27 #include "clang/Tooling/Core/Replacement.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/FunctionExtras.h"
30 #include "llvm/ADT/None.h"
31 #include "llvm/ADT/Optional.h"
32 #include "llvm/ADT/ScopeExit.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/ADT/Twine.h"
35 #include "llvm/Support/Allocator.h"
36 #include "llvm/Support/Error.h"
37 #include "llvm/Support/FormatVariadic.h"
38 #include "llvm/Support/JSON.h"
39 #include "llvm/Support/SHA1.h"
40 #include "llvm/Support/ScopedPrinter.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <chrono>
43 #include <cstddef>
44 #include <cstdint>
45 #include <functional>
46 #include <memory>
47 #include <mutex>
48 #include <string>
49 #include <vector>
50 
51 namespace clang {
52 namespace clangd {
53 namespace {
54 // Tracks end-to-end latency of high level lsp calls. Measurements are in
55 // seconds.
56 constexpr trace::Metric LSPLatency("lsp_latency", trace::Metric::Distribution,
57                                    "method_name");
58 
59 // LSP defines file versions as numbers that increase.
60 // ClangdServer treats them as opaque and therefore uses strings instead.
encodeVersion(llvm::Optional<int64_t> LSPVersion)61 std::string encodeVersion(llvm::Optional<int64_t> LSPVersion) {
62   return LSPVersion ? llvm::to_string(*LSPVersion) : "";
63 }
decodeVersion(llvm::StringRef Encoded)64 llvm::Optional<int64_t> decodeVersion(llvm::StringRef Encoded) {
65   int64_t Result;
66   if (llvm::to_integer(Encoded, Result, 10))
67     return Result;
68   if (!Encoded.empty()) // Empty can be e.g. diagnostics on close.
69     elog("unexpected non-numeric version {0}", Encoded);
70   return llvm::None;
71 }
72 
73 const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
74 const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
75 
76 /// Transforms a tweak into a code action that would apply it if executed.
77 /// EXPECTS: T.prepare() was called and returned true.
toCodeAction(const ClangdServer::TweakRef & T,const URIForFile & File,Range Selection)78 CodeAction toCodeAction(const ClangdServer::TweakRef &T, const URIForFile &File,
79                         Range Selection) {
80   CodeAction CA;
81   CA.title = T.Title;
82   CA.kind = T.Kind.str();
83   // This tweak may have an expensive second stage, we only run it if the user
84   // actually chooses it in the UI. We reply with a command that would run the
85   // corresponding tweak.
86   // FIXME: for some tweaks, computing the edits is cheap and we could send them
87   //        directly.
88   CA.command.emplace();
89   CA.command->title = T.Title;
90   CA.command->command = std::string(ApplyTweakCommand);
91   TweakArgs Args;
92   Args.file = File;
93   Args.tweakID = T.ID;
94   Args.selection = Selection;
95   CA.command->argument = std::move(Args);
96   return CA;
97 }
98 
adjustSymbolKinds(llvm::MutableArrayRef<DocumentSymbol> Syms,SymbolKindBitset Kinds)99 void adjustSymbolKinds(llvm::MutableArrayRef<DocumentSymbol> Syms,
100                        SymbolKindBitset Kinds) {
101   for (auto &S : Syms) {
102     S.kind = adjustKindToCapability(S.kind, Kinds);
103     adjustSymbolKinds(S.children, Kinds);
104   }
105 }
106 
defaultSymbolKinds()107 SymbolKindBitset defaultSymbolKinds() {
108   SymbolKindBitset Defaults;
109   for (size_t I = SymbolKindMin; I <= static_cast<size_t>(SymbolKind::Array);
110        ++I)
111     Defaults.set(I);
112   return Defaults;
113 }
114 
defaultCompletionItemKinds()115 CompletionItemKindBitset defaultCompletionItemKinds() {
116   CompletionItemKindBitset Defaults;
117   for (size_t I = CompletionItemKindMin;
118        I <= static_cast<size_t>(CompletionItemKind::Reference); ++I)
119     Defaults.set(I);
120   return Defaults;
121 }
122 
123 // Makes sure edits in \p FE are applicable to latest file contents reported by
124 // editor. If not generates an error message containing information about files
125 // that needs to be saved.
validateEdits(const ClangdServer & Server,const FileEdits & FE)126 llvm::Error validateEdits(const ClangdServer &Server, const FileEdits &FE) {
127   size_t InvalidFileCount = 0;
128   llvm::StringRef LastInvalidFile;
129   for (const auto &It : FE) {
130     if (auto Draft = Server.getDraft(It.first())) {
131       // If the file is open in user's editor, make sure the version we
132       // saw and current version are compatible as this is the text that
133       // will be replaced by editors.
134       if (!It.second.canApplyTo(*Draft)) {
135         ++InvalidFileCount;
136         LastInvalidFile = It.first();
137       }
138     }
139   }
140   if (!InvalidFileCount)
141     return llvm::Error::success();
142   if (InvalidFileCount == 1)
143     return error("File must be saved first: {0}", LastInvalidFile);
144   return error("Files must be saved first: {0} (and {1} others)",
145                LastInvalidFile, InvalidFileCount - 1);
146 }
147 } // namespace
148 
149 // MessageHandler dispatches incoming LSP messages.
150 // It handles cross-cutting concerns:
151 //  - serializes/deserializes protocol objects to JSON
152 //  - logging of inbound messages
153 //  - cancellation handling
154 //  - basic call tracing
155 // MessageHandler ensures that initialize() is called before any other handler.
156 class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
157 public:
MessageHandler(ClangdLSPServer & Server)158   MessageHandler(ClangdLSPServer &Server) : Server(Server) {}
159 
onNotify(llvm::StringRef Method,llvm::json::Value Params)160   bool onNotify(llvm::StringRef Method, llvm::json::Value Params) override {
161     trace::Span Tracer(Method, LSPLatency);
162     SPAN_ATTACH(Tracer, "Params", Params);
163     WithContext HandlerContext(handlerContext());
164     log("<-- {0}", Method);
165     if (Method == "exit")
166       return false;
167     auto Handler = Server.Handlers.NotificationHandlers.find(Method);
168     if (Handler != Server.Handlers.NotificationHandlers.end()) {
169       Handler->second(std::move(Params));
170       Server.maybeExportMemoryProfile();
171       Server.maybeCleanupMemory();
172     } else if (!Server.Server) {
173       elog("Notification {0} before initialization", Method);
174     } else if (Method == "$/cancelRequest") {
175       onCancel(std::move(Params));
176     } else {
177       log("unhandled notification {0}", Method);
178     }
179     return true;
180   }
181 
onCall(llvm::StringRef Method,llvm::json::Value Params,llvm::json::Value ID)182   bool onCall(llvm::StringRef Method, llvm::json::Value Params,
183               llvm::json::Value ID) override {
184     WithContext HandlerContext(handlerContext());
185     // Calls can be canceled by the client. Add cancellation context.
186     WithContext WithCancel(cancelableRequestContext(ID));
187     trace::Span Tracer(Method, LSPLatency);
188     SPAN_ATTACH(Tracer, "Params", Params);
189     ReplyOnce Reply(ID, Method, &Server, Tracer.Args);
190     log("<-- {0}({1})", Method, ID);
191     auto Handler = Server.Handlers.MethodHandlers.find(Method);
192     if (Handler != Server.Handlers.MethodHandlers.end()) {
193       Handler->second(std::move(Params), std::move(Reply));
194     } else if (!Server.Server) {
195       elog("Call {0} before initialization.", Method);
196       Reply(llvm::make_error<LSPError>("server not initialized",
197                                        ErrorCode::ServerNotInitialized));
198     } else {
199       Reply(llvm::make_error<LSPError>("method not found",
200                                        ErrorCode::MethodNotFound));
201     }
202     return true;
203   }
204 
onReply(llvm::json::Value ID,llvm::Expected<llvm::json::Value> Result)205   bool onReply(llvm::json::Value ID,
206                llvm::Expected<llvm::json::Value> Result) override {
207     WithContext HandlerContext(handlerContext());
208 
209     Callback<llvm::json::Value> ReplyHandler = nullptr;
210     if (auto IntID = ID.getAsInteger()) {
211       std::lock_guard<std::mutex> Mutex(CallMutex);
212       // Find a corresponding callback for the request ID;
213       for (size_t Index = 0; Index < ReplyCallbacks.size(); ++Index) {
214         if (ReplyCallbacks[Index].first == *IntID) {
215           ReplyHandler = std::move(ReplyCallbacks[Index].second);
216           ReplyCallbacks.erase(ReplyCallbacks.begin() +
217                                Index); // remove the entry
218           break;
219         }
220       }
221     }
222 
223     if (!ReplyHandler) {
224       // No callback being found, use a default log callback.
225       ReplyHandler = [&ID](llvm::Expected<llvm::json::Value> Result) {
226         elog("received a reply with ID {0}, but there was no such call", ID);
227         if (!Result)
228           llvm::consumeError(Result.takeError());
229       };
230     }
231 
232     // Log and run the reply handler.
233     if (Result) {
234       log("<-- reply({0})", ID);
235       ReplyHandler(std::move(Result));
236     } else {
237       auto Err = Result.takeError();
238       log("<-- reply({0}) error: {1}", ID, Err);
239       ReplyHandler(std::move(Err));
240     }
241     return true;
242   }
243 
244   // Bind a reply callback to a request. The callback will be invoked when
245   // clangd receives the reply from the LSP client.
246   // Return a call id of the request.
bindReply(Callback<llvm::json::Value> Reply)247   llvm::json::Value bindReply(Callback<llvm::json::Value> Reply) {
248     llvm::Optional<std::pair<int, Callback<llvm::json::Value>>> OldestCB;
249     int ID;
250     {
251       std::lock_guard<std::mutex> Mutex(CallMutex);
252       ID = NextCallID++;
253       ReplyCallbacks.emplace_back(ID, std::move(Reply));
254 
255       // If the queue overflows, we assume that the client didn't reply the
256       // oldest request, and run the corresponding callback which replies an
257       // error to the client.
258       if (ReplyCallbacks.size() > MaxReplayCallbacks) {
259         elog("more than {0} outstanding LSP calls, forgetting about {1}",
260              MaxReplayCallbacks, ReplyCallbacks.front().first);
261         OldestCB = std::move(ReplyCallbacks.front());
262         ReplyCallbacks.pop_front();
263       }
264     }
265     if (OldestCB)
266       OldestCB->second(
267           error("failed to receive a client reply for request ({0})",
268                 OldestCB->first));
269     return ID;
270   }
271 
272 private:
273   // Function object to reply to an LSP call.
274   // Each instance must be called exactly once, otherwise:
275   //  - the bug is logged, and (in debug mode) an assert will fire
276   //  - if there was no reply, an error reply is sent
277   //  - if there were multiple replies, only the first is sent
278   class ReplyOnce {
279     std::atomic<bool> Replied = {false};
280     std::chrono::steady_clock::time_point Start;
281     llvm::json::Value ID;
282     std::string Method;
283     ClangdLSPServer *Server; // Null when moved-from.
284     llvm::json::Object *TraceArgs;
285 
286   public:
ReplyOnce(const llvm::json::Value & ID,llvm::StringRef Method,ClangdLSPServer * Server,llvm::json::Object * TraceArgs)287     ReplyOnce(const llvm::json::Value &ID, llvm::StringRef Method,
288               ClangdLSPServer *Server, llvm::json::Object *TraceArgs)
289         : Start(std::chrono::steady_clock::now()), ID(ID), Method(Method),
290           Server(Server), TraceArgs(TraceArgs) {
291       assert(Server);
292     }
ReplyOnce(ReplyOnce && Other)293     ReplyOnce(ReplyOnce &&Other)
294         : Replied(Other.Replied.load()), Start(Other.Start),
295           ID(std::move(Other.ID)), Method(std::move(Other.Method)),
296           Server(Other.Server), TraceArgs(Other.TraceArgs) {
297       Other.Server = nullptr;
298     }
299     ReplyOnce &operator=(ReplyOnce &&) = delete;
300     ReplyOnce(const ReplyOnce &) = delete;
301     ReplyOnce &operator=(const ReplyOnce &) = delete;
302 
~ReplyOnce()303     ~ReplyOnce() {
304       // There's one legitimate reason to never reply to a request: clangd's
305       // request handler send a call to the client (e.g. applyEdit) and the
306       // client never replied. In this case, the ReplyOnce is owned by
307       // ClangdLSPServer's reply callback table and is destroyed along with the
308       // server. We don't attempt to send a reply in this case, there's little
309       // to be gained from doing so.
310       if (Server && !Server->IsBeingDestroyed && !Replied) {
311         elog("No reply to message {0}({1})", Method, ID);
312         assert(false && "must reply to all calls!");
313         (*this)(llvm::make_error<LSPError>("server failed to reply",
314                                            ErrorCode::InternalError));
315       }
316     }
317 
operator ()(llvm::Expected<llvm::json::Value> Reply)318     void operator()(llvm::Expected<llvm::json::Value> Reply) {
319       assert(Server && "moved-from!");
320       if (Replied.exchange(true)) {
321         elog("Replied twice to message {0}({1})", Method, ID);
322         assert(false && "must reply to each call only once!");
323         return;
324       }
325       auto Duration = std::chrono::steady_clock::now() - Start;
326       if (Reply) {
327         log("--> reply:{0}({1}) {2:ms}", Method, ID, Duration);
328         if (TraceArgs)
329           (*TraceArgs)["Reply"] = *Reply;
330         std::lock_guard<std::mutex> Lock(Server->TranspWriter);
331         Server->Transp.reply(std::move(ID), std::move(Reply));
332       } else {
333         llvm::Error Err = Reply.takeError();
334         log("--> reply:{0}({1}) {2:ms}, error: {3}", Method, ID, Duration, Err);
335         if (TraceArgs)
336           (*TraceArgs)["Error"] = llvm::to_string(Err);
337         std::lock_guard<std::mutex> Lock(Server->TranspWriter);
338         Server->Transp.reply(std::move(ID), std::move(Err));
339       }
340     }
341   };
342 
343   // Method calls may be cancelled by ID, so keep track of their state.
344   // This needs a mutex: handlers may finish on a different thread, and that's
345   // when we clean up entries in the map.
346   mutable std::mutex RequestCancelersMutex;
347   llvm::StringMap<std::pair<Canceler, /*Cookie*/ unsigned>> RequestCancelers;
348   unsigned NextRequestCookie = 0; // To disambiguate reused IDs, see below.
onCancel(const llvm::json::Value & Params)349   void onCancel(const llvm::json::Value &Params) {
350     const llvm::json::Value *ID = nullptr;
351     if (auto *O = Params.getAsObject())
352       ID = O->get("id");
353     if (!ID) {
354       elog("Bad cancellation request: {0}", Params);
355       return;
356     }
357     auto StrID = llvm::to_string(*ID);
358     std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
359     auto It = RequestCancelers.find(StrID);
360     if (It != RequestCancelers.end())
361       It->second.first(); // Invoke the canceler.
362   }
363 
handlerContext() const364   Context handlerContext() const {
365     return Context::current().derive(
366         kCurrentOffsetEncoding,
367         Server.Opts.Encoding.value_or(OffsetEncoding::UTF16));
368   }
369 
370   // We run cancelable requests in a context that does two things:
371   //  - allows cancellation using RequestCancelers[ID]
372   //  - cleans up the entry in RequestCancelers when it's no longer needed
373   // If a client reuses an ID, the last wins and the first cannot be canceled.
cancelableRequestContext(const llvm::json::Value & ID)374   Context cancelableRequestContext(const llvm::json::Value &ID) {
375     auto Task = cancelableTask(
376         /*Reason=*/static_cast<int>(ErrorCode::RequestCancelled));
377     auto StrID = llvm::to_string(ID);  // JSON-serialize ID for map key.
378     auto Cookie = NextRequestCookie++; // No lock, only called on main thread.
379     {
380       std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
381       RequestCancelers[StrID] = {std::move(Task.second), Cookie};
382     }
383     // When the request ends, we can clean up the entry we just added.
384     // The cookie lets us check that it hasn't been overwritten due to ID
385     // reuse.
386     return Task.first.derive(llvm::make_scope_exit([this, StrID, Cookie] {
387       std::lock_guard<std::mutex> Lock(RequestCancelersMutex);
388       auto It = RequestCancelers.find(StrID);
389       if (It != RequestCancelers.end() && It->second.second == Cookie)
390         RequestCancelers.erase(It);
391     }));
392   }
393 
394   // The maximum number of callbacks held in clangd.
395   //
396   // We bound the maximum size to the pending map to prevent memory leakage
397   // for cases where LSP clients don't reply for the request.
398   // This has to go after RequestCancellers and RequestCancellersMutex since it
399   // can contain a callback that has a cancelable context.
400   static constexpr int MaxReplayCallbacks = 100;
401   mutable std::mutex CallMutex;
402   int NextCallID = 0; /* GUARDED_BY(CallMutex) */
403   std::deque<std::pair</*RequestID*/ int,
404                        /*ReplyHandler*/ Callback<llvm::json::Value>>>
405       ReplyCallbacks; /* GUARDED_BY(CallMutex) */
406 
407   ClangdLSPServer &Server;
408 };
409 constexpr int ClangdLSPServer::MessageHandler::MaxReplayCallbacks;
410 
411 // call(), notify(), and reply() wrap the Transport, adding logging and locking.
callMethod(StringRef Method,llvm::json::Value Params,Callback<llvm::json::Value> CB)412 void ClangdLSPServer::callMethod(StringRef Method, llvm::json::Value Params,
413                                  Callback<llvm::json::Value> CB) {
414   auto ID = MsgHandler->bindReply(std::move(CB));
415   log("--> {0}({1})", Method, ID);
416   std::lock_guard<std::mutex> Lock(TranspWriter);
417   Transp.call(Method, std::move(Params), ID);
418 }
419 
notify(llvm::StringRef Method,llvm::json::Value Params)420 void ClangdLSPServer::notify(llvm::StringRef Method, llvm::json::Value Params) {
421   log("--> {0}", Method);
422   maybeCleanupMemory();
423   std::lock_guard<std::mutex> Lock(TranspWriter);
424   Transp.notify(Method, std::move(Params));
425 }
426 
semanticTokenTypes()427 static std::vector<llvm::StringRef> semanticTokenTypes() {
428   std::vector<llvm::StringRef> Types;
429   for (unsigned I = 0; I <= static_cast<unsigned>(HighlightingKind::LastKind);
430        ++I)
431     Types.push_back(toSemanticTokenType(static_cast<HighlightingKind>(I)));
432   return Types;
433 }
434 
semanticTokenModifiers()435 static std::vector<llvm::StringRef> semanticTokenModifiers() {
436   std::vector<llvm::StringRef> Modifiers;
437   for (unsigned I = 0;
438        I <= static_cast<unsigned>(HighlightingModifier::LastModifier); ++I)
439     Modifiers.push_back(
440         toSemanticTokenModifier(static_cast<HighlightingModifier>(I)));
441   return Modifiers;
442 }
443 
onInitialize(const InitializeParams & Params,Callback<llvm::json::Value> Reply)444 void ClangdLSPServer::onInitialize(const InitializeParams &Params,
445                                    Callback<llvm::json::Value> Reply) {
446   // Determine character encoding first as it affects constructed ClangdServer.
447   if (Params.capabilities.offsetEncoding && !Opts.Encoding) {
448     Opts.Encoding = OffsetEncoding::UTF16; // fallback
449     for (OffsetEncoding Supported : *Params.capabilities.offsetEncoding)
450       if (Supported != OffsetEncoding::UnsupportedEncoding) {
451         Opts.Encoding = Supported;
452         break;
453       }
454   }
455 
456   if (Params.capabilities.TheiaSemanticHighlighting &&
457       !Params.capabilities.SemanticTokens) {
458     elog("Client requested legacy semanticHighlights notification, which is "
459          "no longer supported. Migrate to standard semanticTokens request");
460   }
461 
462   if (Params.rootUri && *Params.rootUri)
463     Opts.WorkspaceRoot = std::string(Params.rootUri->file());
464   else if (Params.rootPath && !Params.rootPath->empty())
465     Opts.WorkspaceRoot = *Params.rootPath;
466   if (Server)
467     return Reply(llvm::make_error<LSPError>("server already initialized",
468                                             ErrorCode::InvalidRequest));
469   if (Opts.UseDirBasedCDB) {
470     DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS);
471     if (const auto &Dir = Params.initializationOptions.compilationDatabasePath)
472       CDBOpts.CompileCommandsDir = Dir;
473     CDBOpts.ContextProvider = Opts.ContextProvider;
474     BaseCDB =
475         std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts);
476     BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs),
477                                      std::move(BaseCDB));
478   }
479   auto Mangler = CommandMangler::detect();
480   if (Opts.ResourceDir)
481     Mangler.ResourceDir = *Opts.ResourceDir;
482   CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags,
483               tooling::ArgumentsAdjuster(std::move(Mangler)));
484   {
485     // Switch caller's context with LSPServer's background context. Since we
486     // rather want to propagate information from LSPServer's context into the
487     // Server, CDB, etc.
488     WithContext MainContext(BackgroundContext.clone());
489     llvm::Optional<WithContextValue> WithOffsetEncoding;
490     if (Opts.Encoding)
491       WithOffsetEncoding.emplace(kCurrentOffsetEncoding, *Opts.Encoding);
492     Server.emplace(*CDB, TFS, Opts,
493                    static_cast<ClangdServer::Callbacks *>(this));
494   }
495 
496   Opts.CodeComplete.EnableSnippets = Params.capabilities.CompletionSnippets;
497   Opts.CodeComplete.IncludeFixIts = Params.capabilities.CompletionFixes;
498   if (!Opts.CodeComplete.BundleOverloads)
499     Opts.CodeComplete.BundleOverloads = Params.capabilities.HasSignatureHelp;
500   Opts.CodeComplete.DocumentationFormat =
501       Params.capabilities.CompletionDocumentationFormat;
502   Opts.SignatureHelpDocumentationFormat =
503       Params.capabilities.SignatureHelpDocumentationFormat;
504   DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes;
505   DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory;
506   DiagOpts.EmitRelatedLocations =
507       Params.capabilities.DiagnosticRelatedInformation;
508   if (Params.capabilities.WorkspaceSymbolKinds)
509     SupportedSymbolKinds |= *Params.capabilities.WorkspaceSymbolKinds;
510   if (Params.capabilities.CompletionItemKinds)
511     SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds;
512   SupportsCodeAction = Params.capabilities.CodeActionStructure;
513   SupportsHierarchicalDocumentSymbol =
514       Params.capabilities.HierarchicalDocumentSymbol;
515   SupportFileStatus = Params.initializationOptions.FileStatus;
516   HoverContentFormat = Params.capabilities.HoverContentFormat;
517   SupportsOffsetsInSignatureHelp = Params.capabilities.OffsetsInSignatureHelp;
518   if (Params.capabilities.WorkDoneProgress)
519     BackgroundIndexProgressState = BackgroundIndexProgress::Empty;
520   BackgroundIndexSkipCreate = Params.capabilities.ImplicitProgressCreation;
521   Opts.ImplicitCancellation = !Params.capabilities.CancelsStaleRequests;
522 
523   llvm::json::Object ServerCaps{
524       {"textDocumentSync",
525        llvm::json::Object{
526            {"openClose", true},
527            {"change", (int)TextDocumentSyncKind::Incremental},
528            {"save", true},
529        }},
530       {"documentFormattingProvider", true},
531       {"documentRangeFormattingProvider", true},
532       {"documentOnTypeFormattingProvider",
533        llvm::json::Object{
534            {"firstTriggerCharacter", "\n"},
535            {"moreTriggerCharacter", {}},
536        }},
537       {"completionProvider",
538        llvm::json::Object{
539            // We don't set `(` etc as allCommitCharacters as they interact
540            // poorly with snippet results.
541            // See https://github.com/clangd/vscode-clangd/issues/357
542            // Hopefully we can use them one day without this side-effect:
543            //     https://github.com/microsoft/vscode/issues/42544
544            {"resolveProvider", false},
545            // We do extra checks, e.g. that > is part of ->.
546            {"triggerCharacters", {".", "<", ">", ":", "\"", "/", "*"}},
547        }},
548       {"semanticTokensProvider",
549        llvm::json::Object{
550            {"full", llvm::json::Object{{"delta", true}}},
551            {"range", false},
552            {"legend",
553             llvm::json::Object{{"tokenTypes", semanticTokenTypes()},
554                                {"tokenModifiers", semanticTokenModifiers()}}},
555        }},
556       {"signatureHelpProvider",
557        llvm::json::Object{
558            {"triggerCharacters", {"(", ")", "{", "}", "<", ">", ","}},
559        }},
560       {"declarationProvider", true},
561       {"definitionProvider", true},
562       {"implementationProvider", true},
563       {"typeDefinitionProvider", true},
564       {"documentHighlightProvider", true},
565       {"documentLinkProvider",
566        llvm::json::Object{
567            {"resolveProvider", false},
568        }},
569       {"hoverProvider", true},
570       {"selectionRangeProvider", true},
571       {"documentSymbolProvider", true},
572       {"workspaceSymbolProvider", true},
573       {"referencesProvider", true},
574       {"astProvider", true}, // clangd extension
575       {"typeHierarchyProvider", true},
576       // Unfortunately our extension made use of the same capability name as the
577       // standard. Advertise this capability to tell clients that implement our
578       // extension we really have support for the standardized one as well.
579       {"standardTypeHierarchyProvider", true}, // clangd extension
580       {"memoryUsageProvider", true},           // clangd extension
581       {"compilationDatabase",                  // clangd extension
582        llvm::json::Object{{"automaticReload", true}}},
583       {"callHierarchyProvider", true},
584       {"clangdInlayHintsProvider", true},
585       {"inlayHintProvider", true},
586   };
587 
588   {
589     LSPBinder Binder(Handlers, *this);
590     bindMethods(Binder, Params.capabilities);
591     if (Opts.FeatureModules)
592       for (auto &Mod : *Opts.FeatureModules)
593         Mod.initializeLSP(Binder, Params.rawCapabilities, ServerCaps);
594   }
595 
596   // Per LSP, renameProvider can be either boolean or RenameOptions.
597   // RenameOptions will be specified if the client states it supports prepare.
598   ServerCaps["renameProvider"] =
599       Params.capabilities.RenamePrepareSupport
600           ? llvm::json::Object{{"prepareProvider", true}}
601           : llvm::json::Value(true);
602 
603   // Per LSP, codeActionProvider can be either boolean or CodeActionOptions.
604   // CodeActionOptions is only valid if the client supports action literal
605   // via textDocument.codeAction.codeActionLiteralSupport.
606   llvm::json::Value CodeActionProvider = true;
607   ServerCaps["codeActionProvider"] =
608       Params.capabilities.CodeActionStructure
609           ? llvm::json::Object{{"codeActionKinds",
610                                 {CodeAction::QUICKFIX_KIND,
611                                  CodeAction::REFACTOR_KIND,
612                                  CodeAction::INFO_KIND}}}
613           : llvm::json::Value(true);
614 
615   if (Opts.FoldingRanges)
616     ServerCaps["foldingRangeProvider"] = true;
617 
618   std::vector<llvm::StringRef> Commands;
619   for (llvm::StringRef Command : Handlers.CommandHandlers.keys())
620     Commands.push_back(Command);
621   llvm::sort(Commands);
622   ServerCaps["executeCommandProvider"] =
623       llvm::json::Object{{"commands", Commands}};
624 
625   llvm::json::Object Result{
626       {{"serverInfo",
627         llvm::json::Object{
628             {"name", "clangd"},
629             {"version", llvm::formatv("{0} {1} {2}", versionString(),
630                                       featureString(), platformString())}}},
631        {"capabilities", std::move(ServerCaps)}}};
632   if (Opts.Encoding)
633     Result["offsetEncoding"] = *Opts.Encoding;
634   Reply(std::move(Result));
635 
636   // Apply settings after we're fully initialized.
637   // This can start background indexing and in turn trigger LSP notifications.
638   applyConfiguration(Params.initializationOptions.ConfigSettings);
639 }
640 
onInitialized(const InitializedParams & Params)641 void ClangdLSPServer::onInitialized(const InitializedParams &Params) {}
642 
onShutdown(const NoParams &,Callback<std::nullptr_t> Reply)643 void ClangdLSPServer::onShutdown(const NoParams &,
644                                  Callback<std::nullptr_t> Reply) {
645   // Do essentially nothing, just say we're ready to exit.
646   ShutdownRequestReceived = true;
647   Reply(nullptr);
648 }
649 
650 // sync is a clangd extension: it blocks until all background work completes.
651 // It blocks the calling thread, so no messages are processed until it returns!
onSync(const NoParams &,Callback<std::nullptr_t> Reply)652 void ClangdLSPServer::onSync(const NoParams &, Callback<std::nullptr_t> Reply) {
653   if (Server->blockUntilIdleForTest(/*TimeoutSeconds=*/60))
654     Reply(nullptr);
655   else
656     Reply(error("Not idle after a minute"));
657 }
658 
onDocumentDidOpen(const DidOpenTextDocumentParams & Params)659 void ClangdLSPServer::onDocumentDidOpen(
660     const DidOpenTextDocumentParams &Params) {
661   PathRef File = Params.textDocument.uri.file();
662 
663   const std::string &Contents = Params.textDocument.text;
664 
665   Server->addDocument(File, Contents,
666                       encodeVersion(Params.textDocument.version),
667                       WantDiagnostics::Yes);
668 }
669 
onDocumentDidChange(const DidChangeTextDocumentParams & Params)670 void ClangdLSPServer::onDocumentDidChange(
671     const DidChangeTextDocumentParams &Params) {
672   auto WantDiags = WantDiagnostics::Auto;
673   if (Params.wantDiagnostics)
674     WantDiags = Params.wantDiagnostics.value() ? WantDiagnostics::Yes
675                                                : WantDiagnostics::No;
676 
677   PathRef File = Params.textDocument.uri.file();
678   auto Code = Server->getDraft(File);
679   if (!Code) {
680     log("Trying to incrementally change non-added document: {0}", File);
681     return;
682   }
683   std::string NewCode(*Code);
684   for (const auto &Change : Params.contentChanges) {
685     if (auto Err = applyChange(NewCode, Change)) {
686       // If this fails, we are most likely going to be not in sync anymore with
687       // the client.  It is better to remove the draft and let further
688       // operations fail rather than giving wrong results.
689       Server->removeDocument(File);
690       elog("Failed to update {0}: {1}", File, std::move(Err));
691       return;
692     }
693   }
694   Server->addDocument(File, NewCode, encodeVersion(Params.textDocument.version),
695                       WantDiags, Params.forceRebuild);
696 }
697 
onDocumentDidSave(const DidSaveTextDocumentParams & Params)698 void ClangdLSPServer::onDocumentDidSave(
699     const DidSaveTextDocumentParams &Params) {
700   Server->reparseOpenFilesIfNeeded([](llvm::StringRef) { return true; });
701 }
702 
onFileEvent(const DidChangeWatchedFilesParams & Params)703 void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
704   // We could also reparse all open files here. However:
705   //  - this could be frequent, and revalidating all the preambles isn't free
706   //  - this is useful e.g. when switching git branches, but we're likely to see
707   //    fresh headers but still have the old-branch main-file content
708   Server->onFileEvent(Params);
709   // FIXME: observe config files, immediately expire time-based caches, reparse:
710   //  - compile_commands.json and compile_flags.txt
711   //  - .clang_format and .clang-tidy
712   //  - .clangd and clangd/config.yaml
713 }
714 
onCommand(const ExecuteCommandParams & Params,Callback<llvm::json::Value> Reply)715 void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params,
716                                 Callback<llvm::json::Value> Reply) {
717   auto It = Handlers.CommandHandlers.find(Params.command);
718   if (It == Handlers.CommandHandlers.end()) {
719     return Reply(llvm::make_error<LSPError>(
720         llvm::formatv("Unsupported command \"{0}\".", Params.command).str(),
721         ErrorCode::InvalidParams));
722   }
723   It->second(Params.argument, std::move(Reply));
724 }
725 
onCommandApplyEdit(const WorkspaceEdit & WE,Callback<llvm::json::Value> Reply)726 void ClangdLSPServer::onCommandApplyEdit(const WorkspaceEdit &WE,
727                                          Callback<llvm::json::Value> Reply) {
728   // The flow for "apply-fix" :
729   // 1. We publish a diagnostic, including fixits
730   // 2. The user clicks on the diagnostic, the editor asks us for code actions
731   // 3. We send code actions, with the fixit embedded as context
732   // 4. The user selects the fixit, the editor asks us to apply it
733   // 5. We unwrap the changes and send them back to the editor
734   // 6. The editor applies the changes (applyEdit), and sends us a reply
735   // 7. We unwrap the reply and send a reply to the editor.
736   applyEdit(WE, "Fix applied.", std::move(Reply));
737 }
738 
onCommandApplyTweak(const TweakArgs & Args,Callback<llvm::json::Value> Reply)739 void ClangdLSPServer::onCommandApplyTweak(const TweakArgs &Args,
740                                           Callback<llvm::json::Value> Reply) {
741   auto Action = [this, Reply = std::move(Reply)](
742                     llvm::Expected<Tweak::Effect> R) mutable {
743     if (!R)
744       return Reply(R.takeError());
745 
746     assert(R->ShowMessage || (!R->ApplyEdits.empty() && "tweak has no effect"));
747 
748     if (R->ShowMessage) {
749       ShowMessageParams Msg;
750       Msg.message = *R->ShowMessage;
751       Msg.type = MessageType::Info;
752       ShowMessage(Msg);
753     }
754     // When no edit is specified, make sure we Reply().
755     if (R->ApplyEdits.empty())
756       return Reply("Tweak applied.");
757 
758     if (auto Err = validateEdits(*Server, R->ApplyEdits))
759       return Reply(std::move(Err));
760 
761     WorkspaceEdit WE;
762     for (const auto &It : R->ApplyEdits) {
763       WE.changes[URI::createFile(It.first()).toString()] =
764           It.second.asTextEdits();
765     }
766     // ApplyEdit will take care of calling Reply().
767     return applyEdit(std::move(WE), "Tweak applied.", std::move(Reply));
768   };
769   Server->applyTweak(Args.file.file(), Args.selection, Args.tweakID,
770                      std::move(Action));
771 }
772 
applyEdit(WorkspaceEdit WE,llvm::json::Value Success,Callback<llvm::json::Value> Reply)773 void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
774                                 Callback<llvm::json::Value> Reply) {
775   ApplyWorkspaceEditParams Edit;
776   Edit.edit = std::move(WE);
777   ApplyWorkspaceEdit(
778       Edit, [Reply = std::move(Reply), SuccessMessage = std::move(Success)](
779                 llvm::Expected<ApplyWorkspaceEditResponse> Response) mutable {
780         if (!Response)
781           return Reply(Response.takeError());
782         if (!Response->applied) {
783           std::string Reason = Response->failureReason
784                                    ? *Response->failureReason
785                                    : "unknown reason";
786           return Reply(error("edits were not applied: {0}", Reason));
787         }
788         return Reply(SuccessMessage);
789       });
790 }
791 
onWorkspaceSymbol(const WorkspaceSymbolParams & Params,Callback<std::vector<SymbolInformation>> Reply)792 void ClangdLSPServer::onWorkspaceSymbol(
793     const WorkspaceSymbolParams &Params,
794     Callback<std::vector<SymbolInformation>> Reply) {
795   Server->workspaceSymbols(
796       Params.query, Params.limit.value_or(Opts.CodeComplete.Limit),
797       [Reply = std::move(Reply),
798        this](llvm::Expected<std::vector<SymbolInformation>> Items) mutable {
799         if (!Items)
800           return Reply(Items.takeError());
801         for (auto &Sym : *Items)
802           Sym.kind = adjustKindToCapability(Sym.kind, SupportedSymbolKinds);
803 
804         Reply(std::move(*Items));
805       });
806 }
807 
onPrepareRename(const TextDocumentPositionParams & Params,Callback<llvm::Optional<Range>> Reply)808 void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
809                                       Callback<llvm::Optional<Range>> Reply) {
810   Server->prepareRename(
811       Params.textDocument.uri.file(), Params.position, /*NewName*/ llvm::None,
812       Opts.Rename,
813       [Reply = std::move(Reply)](llvm::Expected<RenameResult> Result) mutable {
814         if (!Result)
815           return Reply(Result.takeError());
816         return Reply(std::move(Result->Target));
817       });
818 }
819 
onRename(const RenameParams & Params,Callback<WorkspaceEdit> Reply)820 void ClangdLSPServer::onRename(const RenameParams &Params,
821                                Callback<WorkspaceEdit> Reply) {
822   Path File = std::string(Params.textDocument.uri.file());
823   if (!Server->getDraft(File))
824     return Reply(llvm::make_error<LSPError>(
825         "onRename called for non-added file", ErrorCode::InvalidParams));
826   Server->rename(File, Params.position, Params.newName, Opts.Rename,
827                  [File, Params, Reply = std::move(Reply),
828                   this](llvm::Expected<RenameResult> R) mutable {
829                    if (!R)
830                      return Reply(R.takeError());
831                    if (auto Err = validateEdits(*Server, R->GlobalChanges))
832                      return Reply(std::move(Err));
833                    WorkspaceEdit Result;
834                    for (const auto &Rep : R->GlobalChanges) {
835                      Result.changes[URI::createFile(Rep.first()).toString()] =
836                          Rep.second.asTextEdits();
837                    }
838                    Reply(Result);
839                  });
840 }
841 
onDocumentDidClose(const DidCloseTextDocumentParams & Params)842 void ClangdLSPServer::onDocumentDidClose(
843     const DidCloseTextDocumentParams &Params) {
844   PathRef File = Params.textDocument.uri.file();
845   Server->removeDocument(File);
846 
847   {
848     std::lock_guard<std::mutex> Lock(FixItsMutex);
849     FixItsMap.erase(File);
850   }
851   {
852     std::lock_guard<std::mutex> HLock(SemanticTokensMutex);
853     LastSemanticTokens.erase(File);
854   }
855   // clangd will not send updates for this file anymore, so we empty out the
856   // list of diagnostics shown on the client (e.g. in the "Problems" pane of
857   // VSCode). Note that this cannot race with actual diagnostics responses
858   // because removeDocument() guarantees no diagnostic callbacks will be
859   // executed after it returns.
860   PublishDiagnosticsParams Notification;
861   Notification.uri = URIForFile::canonicalize(File, /*TUPath=*/File);
862   PublishDiagnostics(Notification);
863 }
864 
onDocumentOnTypeFormatting(const DocumentOnTypeFormattingParams & Params,Callback<std::vector<TextEdit>> Reply)865 void ClangdLSPServer::onDocumentOnTypeFormatting(
866     const DocumentOnTypeFormattingParams &Params,
867     Callback<std::vector<TextEdit>> Reply) {
868   auto File = Params.textDocument.uri.file();
869   Server->formatOnType(File, Params.position, Params.ch, std::move(Reply));
870 }
871 
onDocumentRangeFormatting(const DocumentRangeFormattingParams & Params,Callback<std::vector<TextEdit>> Reply)872 void ClangdLSPServer::onDocumentRangeFormatting(
873     const DocumentRangeFormattingParams &Params,
874     Callback<std::vector<TextEdit>> Reply) {
875   auto File = Params.textDocument.uri.file();
876   auto Code = Server->getDraft(File);
877   Server->formatFile(File, Params.range,
878                      [Code = std::move(Code), Reply = std::move(Reply)](
879                          llvm::Expected<tooling::Replacements> Result) mutable {
880                        if (Result)
881                          Reply(replacementsToEdits(*Code, Result.get()));
882                        else
883                          Reply(Result.takeError());
884                      });
885 }
886 
onDocumentFormatting(const DocumentFormattingParams & Params,Callback<std::vector<TextEdit>> Reply)887 void ClangdLSPServer::onDocumentFormatting(
888     const DocumentFormattingParams &Params,
889     Callback<std::vector<TextEdit>> Reply) {
890   auto File = Params.textDocument.uri.file();
891   auto Code = Server->getDraft(File);
892   Server->formatFile(File,
893                      /*Rng=*/llvm::None,
894                      [Code = std::move(Code), Reply = std::move(Reply)](
895                          llvm::Expected<tooling::Replacements> Result) mutable {
896                        if (Result)
897                          Reply(replacementsToEdits(*Code, Result.get()));
898                        else
899                          Reply(Result.takeError());
900                      });
901 }
902 
903 /// The functions constructs a flattened view of the DocumentSymbol hierarchy.
904 /// Used by the clients that do not support the hierarchical view.
905 static std::vector<SymbolInformation>
flattenSymbolHierarchy(llvm::ArrayRef<DocumentSymbol> Symbols,const URIForFile & FileURI)906 flattenSymbolHierarchy(llvm::ArrayRef<DocumentSymbol> Symbols,
907                        const URIForFile &FileURI) {
908   std::vector<SymbolInformation> Results;
909   std::function<void(const DocumentSymbol &, llvm::StringRef)> Process =
910       [&](const DocumentSymbol &S, llvm::Optional<llvm::StringRef> ParentName) {
911         SymbolInformation SI;
912         SI.containerName = std::string(ParentName ? "" : *ParentName);
913         SI.name = S.name;
914         SI.kind = S.kind;
915         SI.location.range = S.range;
916         SI.location.uri = FileURI;
917 
918         Results.push_back(std::move(SI));
919         std::string FullName =
920             !ParentName ? S.name : (ParentName->str() + "::" + S.name);
921         for (auto &C : S.children)
922           Process(C, /*ParentName=*/FullName);
923       };
924   for (auto &S : Symbols)
925     Process(S, /*ParentName=*/"");
926   return Results;
927 }
928 
onDocumentSymbol(const DocumentSymbolParams & Params,Callback<llvm::json::Value> Reply)929 void ClangdLSPServer::onDocumentSymbol(const DocumentSymbolParams &Params,
930                                        Callback<llvm::json::Value> Reply) {
931   URIForFile FileURI = Params.textDocument.uri;
932   Server->documentSymbols(
933       Params.textDocument.uri.file(),
934       [this, FileURI, Reply = std::move(Reply)](
935           llvm::Expected<std::vector<DocumentSymbol>> Items) mutable {
936         if (!Items)
937           return Reply(Items.takeError());
938         adjustSymbolKinds(*Items, SupportedSymbolKinds);
939         if (SupportsHierarchicalDocumentSymbol)
940           return Reply(std::move(*Items));
941         return Reply(flattenSymbolHierarchy(*Items, FileURI));
942       });
943 }
944 
onFoldingRange(const FoldingRangeParams & Params,Callback<std::vector<FoldingRange>> Reply)945 void ClangdLSPServer::onFoldingRange(
946     const FoldingRangeParams &Params,
947     Callback<std::vector<FoldingRange>> Reply) {
948   Server->foldingRanges(Params.textDocument.uri.file(), std::move(Reply));
949 }
950 
asCommand(const CodeAction & Action)951 static llvm::Optional<Command> asCommand(const CodeAction &Action) {
952   Command Cmd;
953   if (Action.command && Action.edit)
954     return None; // Not representable. (We never emit these anyway).
955   if (Action.command) {
956     Cmd = *Action.command;
957   } else if (Action.edit) {
958     Cmd.command = std::string(ApplyFixCommand);
959     Cmd.argument = *Action.edit;
960   } else {
961     return None;
962   }
963   Cmd.title = Action.title;
964   if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND)
965     Cmd.title = "Apply fix: " + Cmd.title;
966   return Cmd;
967 }
968 
onCodeAction(const CodeActionParams & Params,Callback<llvm::json::Value> Reply)969 void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
970                                    Callback<llvm::json::Value> Reply) {
971   URIForFile File = Params.textDocument.uri;
972   // Checks whether a particular CodeActionKind is included in the response.
973   auto KindAllowed = [Only(Params.context.only)](llvm::StringRef Kind) {
974     if (Only.empty())
975       return true;
976     return llvm::any_of(Only, [&](llvm::StringRef Base) {
977       return Kind.consume_front(Base) && (Kind.empty() || Kind.startswith("."));
978     });
979   };
980 
981   // We provide a code action for Fixes on the specified diagnostics.
982   std::vector<CodeAction> FixIts;
983   if (KindAllowed(CodeAction::QUICKFIX_KIND)) {
984     for (const Diagnostic &D : Params.context.diagnostics) {
985       for (auto &F : getFixes(File.file(), D)) {
986         FixIts.push_back(toCodeAction(F, Params.textDocument.uri));
987         FixIts.back().diagnostics = {D};
988       }
989     }
990   }
991 
992   // Now enumerate the semantic code actions.
993   auto ConsumeActions =
994       [Diags = Params.context.diagnostics, Reply = std::move(Reply), File,
995        Selection = Params.range, FixIts = std::move(FixIts), this](
996           llvm::Expected<std::vector<ClangdServer::TweakRef>> Tweaks) mutable {
997         if (!Tweaks)
998           return Reply(Tweaks.takeError());
999 
1000         std::vector<CodeAction> Actions = std::move(FixIts);
1001         Actions.reserve(Actions.size() + Tweaks->size());
1002         for (const auto &T : *Tweaks)
1003           Actions.push_back(toCodeAction(T, File, Selection));
1004 
1005         // If there's exactly one quick-fix, call it "preferred".
1006         // We never consider refactorings etc as preferred.
1007         CodeAction *OnlyFix = nullptr;
1008         for (auto &Action : Actions) {
1009           if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) {
1010             if (OnlyFix) {
1011               OnlyFix = nullptr;
1012               break;
1013             }
1014             OnlyFix = &Action;
1015           }
1016         }
1017         if (OnlyFix) {
1018           OnlyFix->isPreferred = true;
1019           if (Diags.size() == 1 && Diags.front().range == Selection)
1020             OnlyFix->diagnostics = {Diags.front()};
1021         }
1022 
1023         if (SupportsCodeAction)
1024           return Reply(llvm::json::Array(Actions));
1025         std::vector<Command> Commands;
1026         for (const auto &Action : Actions) {
1027           if (auto Command = asCommand(Action))
1028             Commands.push_back(std::move(*Command));
1029         }
1030         return Reply(llvm::json::Array(Commands));
1031       };
1032   Server->enumerateTweaks(
1033       File.file(), Params.range,
1034       [this, KindAllowed(std::move(KindAllowed))](const Tweak &T) {
1035         return Opts.TweakFilter(T) && KindAllowed(T.kind());
1036       },
1037       std::move(ConsumeActions));
1038 }
1039 
onCompletion(const CompletionParams & Params,Callback<CompletionList> Reply)1040 void ClangdLSPServer::onCompletion(const CompletionParams &Params,
1041                                    Callback<CompletionList> Reply) {
1042   if (!shouldRunCompletion(Params)) {
1043     // Clients sometimes auto-trigger completions in undesired places (e.g.
1044     // 'a >^ '), we return empty results in those cases.
1045     vlog("ignored auto-triggered completion, preceding char did not match");
1046     return Reply(CompletionList());
1047   }
1048   auto Opts = this->Opts.CodeComplete;
1049   if (Params.limit && *Params.limit >= 0)
1050     Opts.Limit = *Params.limit;
1051   Server->codeComplete(Params.textDocument.uri.file(), Params.position, Opts,
1052                        [Reply = std::move(Reply), Opts,
1053                         this](llvm::Expected<CodeCompleteResult> List) mutable {
1054                          if (!List)
1055                            return Reply(List.takeError());
1056                          CompletionList LSPList;
1057                          LSPList.isIncomplete = List->HasMore;
1058                          for (const auto &R : List->Completions) {
1059                            CompletionItem C = R.render(Opts);
1060                            C.kind = adjustKindToCapability(
1061                                C.kind, SupportedCompletionItemKinds);
1062                            LSPList.items.push_back(std::move(C));
1063                          }
1064                          return Reply(std::move(LSPList));
1065                        });
1066 }
1067 
onSignatureHelp(const TextDocumentPositionParams & Params,Callback<SignatureHelp> Reply)1068 void ClangdLSPServer::onSignatureHelp(const TextDocumentPositionParams &Params,
1069                                       Callback<SignatureHelp> Reply) {
1070   Server->signatureHelp(Params.textDocument.uri.file(), Params.position,
1071                         Opts.SignatureHelpDocumentationFormat,
1072                         [Reply = std::move(Reply), this](
1073                             llvm::Expected<SignatureHelp> Signature) mutable {
1074                           if (!Signature)
1075                             return Reply(Signature.takeError());
1076                           if (SupportsOffsetsInSignatureHelp)
1077                             return Reply(std::move(*Signature));
1078                           // Strip out the offsets from signature help for
1079                           // clients that only support string labels.
1080                           for (auto &SigInfo : Signature->signatures) {
1081                             for (auto &Param : SigInfo.parameters)
1082                               Param.labelOffsets.reset();
1083                           }
1084                           return Reply(std::move(*Signature));
1085                         });
1086 }
1087 
1088 // Go to definition has a toggle function: if def and decl are distinct, then
1089 // the first press gives you the def, the second gives you the matching def.
1090 // getToggle() returns the counterpart location that under the cursor.
1091 //
1092 // We return the toggled location alone (ignoring other symbols) to encourage
1093 // editors to "bounce" quickly between locations, without showing a menu.
getToggle(const TextDocumentPositionParams & Point,LocatedSymbol & Sym)1094 static Location *getToggle(const TextDocumentPositionParams &Point,
1095                            LocatedSymbol &Sym) {
1096   // Toggle only makes sense with two distinct locations.
1097   if (!Sym.Definition || *Sym.Definition == Sym.PreferredDeclaration)
1098     return nullptr;
1099   if (Sym.Definition->uri.file() == Point.textDocument.uri.file() &&
1100       Sym.Definition->range.contains(Point.position))
1101     return &Sym.PreferredDeclaration;
1102   if (Sym.PreferredDeclaration.uri.file() == Point.textDocument.uri.file() &&
1103       Sym.PreferredDeclaration.range.contains(Point.position))
1104     return &*Sym.Definition;
1105   return nullptr;
1106 }
1107 
onGoToDefinition(const TextDocumentPositionParams & Params,Callback<std::vector<Location>> Reply)1108 void ClangdLSPServer::onGoToDefinition(const TextDocumentPositionParams &Params,
1109                                        Callback<std::vector<Location>> Reply) {
1110   Server->locateSymbolAt(
1111       Params.textDocument.uri.file(), Params.position,
1112       [Params, Reply = std::move(Reply)](
1113           llvm::Expected<std::vector<LocatedSymbol>> Symbols) mutable {
1114         if (!Symbols)
1115           return Reply(Symbols.takeError());
1116         std::vector<Location> Defs;
1117         for (auto &S : *Symbols) {
1118           if (Location *Toggle = getToggle(Params, S))
1119             return Reply(std::vector<Location>{std::move(*Toggle)});
1120           Defs.push_back(S.Definition.value_or(S.PreferredDeclaration));
1121         }
1122         Reply(std::move(Defs));
1123       });
1124 }
1125 
onGoToDeclaration(const TextDocumentPositionParams & Params,Callback<std::vector<Location>> Reply)1126 void ClangdLSPServer::onGoToDeclaration(
1127     const TextDocumentPositionParams &Params,
1128     Callback<std::vector<Location>> Reply) {
1129   Server->locateSymbolAt(
1130       Params.textDocument.uri.file(), Params.position,
1131       [Params, Reply = std::move(Reply)](
1132           llvm::Expected<std::vector<LocatedSymbol>> Symbols) mutable {
1133         if (!Symbols)
1134           return Reply(Symbols.takeError());
1135         std::vector<Location> Decls;
1136         for (auto &S : *Symbols) {
1137           if (Location *Toggle = getToggle(Params, S))
1138             return Reply(std::vector<Location>{std::move(*Toggle)});
1139           Decls.push_back(std::move(S.PreferredDeclaration));
1140         }
1141         Reply(std::move(Decls));
1142       });
1143 }
1144 
onSwitchSourceHeader(const TextDocumentIdentifier & Params,Callback<llvm::Optional<URIForFile>> Reply)1145 void ClangdLSPServer::onSwitchSourceHeader(
1146     const TextDocumentIdentifier &Params,
1147     Callback<llvm::Optional<URIForFile>> Reply) {
1148   Server->switchSourceHeader(
1149       Params.uri.file(),
1150       [Reply = std::move(Reply),
1151        Params](llvm::Expected<llvm::Optional<clangd::Path>> Path) mutable {
1152         if (!Path)
1153           return Reply(Path.takeError());
1154         if (*Path)
1155           return Reply(URIForFile::canonicalize(**Path, Params.uri.file()));
1156         return Reply(llvm::None);
1157       });
1158 }
1159 
onDocumentHighlight(const TextDocumentPositionParams & Params,Callback<std::vector<DocumentHighlight>> Reply)1160 void ClangdLSPServer::onDocumentHighlight(
1161     const TextDocumentPositionParams &Params,
1162     Callback<std::vector<DocumentHighlight>> Reply) {
1163   Server->findDocumentHighlights(Params.textDocument.uri.file(),
1164                                  Params.position, std::move(Reply));
1165 }
1166 
onHover(const TextDocumentPositionParams & Params,Callback<llvm::Optional<Hover>> Reply)1167 void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
1168                               Callback<llvm::Optional<Hover>> Reply) {
1169   Server->findHover(Params.textDocument.uri.file(), Params.position,
1170                     [Reply = std::move(Reply), this](
1171                         llvm::Expected<llvm::Optional<HoverInfo>> H) mutable {
1172                       if (!H)
1173                         return Reply(H.takeError());
1174                       if (!*H)
1175                         return Reply(llvm::None);
1176 
1177                       Hover R;
1178                       R.contents.kind = HoverContentFormat;
1179                       R.range = (*H)->SymRange;
1180                       switch (HoverContentFormat) {
1181                       case MarkupKind::PlainText:
1182                         R.contents.value = (*H)->present().asPlainText();
1183                         return Reply(std::move(R));
1184                       case MarkupKind::Markdown:
1185                         R.contents.value = (*H)->present().asMarkdown();
1186                         return Reply(std::move(R));
1187                       };
1188                       llvm_unreachable("unhandled MarkupKind");
1189                     });
1190 }
1191 
1192 // Our extension has a different representation on the wire than the standard.
1193 // https://clangd.llvm.org/extensions#type-hierarchy
serializeTHIForExtension(TypeHierarchyItem THI)1194 llvm::json::Value serializeTHIForExtension(TypeHierarchyItem THI) {
1195   llvm::json::Object Result{{
1196       {"name", std::move(THI.name)},
1197       {"kind", static_cast<int>(THI.kind)},
1198       {"uri", std::move(THI.uri)},
1199       {"range", THI.range},
1200       {"selectionRange", THI.selectionRange},
1201       {"data", std::move(THI.data)},
1202   }};
1203   if (THI.deprecated)
1204     Result["deprecated"] = THI.deprecated;
1205   if (THI.detail)
1206     Result["detail"] = std::move(*THI.detail);
1207 
1208   if (THI.parents) {
1209     llvm::json::Array Parents;
1210     for (auto &Parent : *THI.parents)
1211       Parents.emplace_back(serializeTHIForExtension(std::move(Parent)));
1212     Result["parents"] = std::move(Parents);
1213   }
1214 
1215   if (THI.children) {
1216     llvm::json::Array Children;
1217     for (auto &child : *THI.children)
1218       Children.emplace_back(serializeTHIForExtension(std::move(child)));
1219     Result["children"] = std::move(Children);
1220   }
1221   return Result;
1222 }
1223 
onTypeHierarchy(const TypeHierarchyPrepareParams & Params,Callback<llvm::json::Value> Reply)1224 void ClangdLSPServer::onTypeHierarchy(const TypeHierarchyPrepareParams &Params,
1225                                       Callback<llvm::json::Value> Reply) {
1226   auto Serialize =
1227       [Reply = std::move(Reply)](
1228           llvm::Expected<std::vector<TypeHierarchyItem>> Resp) mutable {
1229         if (!Resp) {
1230           Reply(Resp.takeError());
1231           return;
1232         }
1233         if (Resp->empty()) {
1234           Reply(nullptr);
1235           return;
1236         }
1237         Reply(serializeTHIForExtension(std::move(Resp->front())));
1238       };
1239   Server->typeHierarchy(Params.textDocument.uri.file(), Params.position,
1240                         Params.resolve, Params.direction, std::move(Serialize));
1241 }
1242 
onResolveTypeHierarchy(const ResolveTypeHierarchyItemParams & Params,Callback<llvm::json::Value> Reply)1243 void ClangdLSPServer::onResolveTypeHierarchy(
1244     const ResolveTypeHierarchyItemParams &Params,
1245     Callback<llvm::json::Value> Reply) {
1246   auto Serialize =
1247       [Reply = std::move(Reply)](
1248           llvm::Expected<llvm::Optional<TypeHierarchyItem>> Resp) mutable {
1249         if (!Resp) {
1250           Reply(Resp.takeError());
1251           return;
1252         }
1253         if (!*Resp) {
1254           Reply(std::move(*Resp));
1255           return;
1256         }
1257         Reply(serializeTHIForExtension(std::move(**Resp)));
1258       };
1259   Server->resolveTypeHierarchy(Params.item, Params.resolve, Params.direction,
1260                                std::move(Serialize));
1261 }
1262 
onPrepareTypeHierarchy(const TypeHierarchyPrepareParams & Params,Callback<std::vector<TypeHierarchyItem>> Reply)1263 void ClangdLSPServer::onPrepareTypeHierarchy(
1264     const TypeHierarchyPrepareParams &Params,
1265     Callback<std::vector<TypeHierarchyItem>> Reply) {
1266   Server->typeHierarchy(Params.textDocument.uri.file(), Params.position,
1267                         Params.resolve, Params.direction, std::move(Reply));
1268 }
1269 
onSuperTypes(const ResolveTypeHierarchyItemParams & Params,Callback<llvm::Optional<std::vector<TypeHierarchyItem>>> Reply)1270 void ClangdLSPServer::onSuperTypes(
1271     const ResolveTypeHierarchyItemParams &Params,
1272     Callback<llvm::Optional<std::vector<TypeHierarchyItem>>> Reply) {
1273   Server->superTypes(Params.item, std::move(Reply));
1274 }
1275 
onSubTypes(const ResolveTypeHierarchyItemParams & Params,Callback<std::vector<TypeHierarchyItem>> Reply)1276 void ClangdLSPServer::onSubTypes(
1277     const ResolveTypeHierarchyItemParams &Params,
1278     Callback<std::vector<TypeHierarchyItem>> Reply) {
1279   Server->subTypes(Params.item, std::move(Reply));
1280 }
1281 
onPrepareCallHierarchy(const CallHierarchyPrepareParams & Params,Callback<std::vector<CallHierarchyItem>> Reply)1282 void ClangdLSPServer::onPrepareCallHierarchy(
1283     const CallHierarchyPrepareParams &Params,
1284     Callback<std::vector<CallHierarchyItem>> Reply) {
1285   Server->prepareCallHierarchy(Params.textDocument.uri.file(), Params.position,
1286                                std::move(Reply));
1287 }
1288 
onCallHierarchyIncomingCalls(const CallHierarchyIncomingCallsParams & Params,Callback<std::vector<CallHierarchyIncomingCall>> Reply)1289 void ClangdLSPServer::onCallHierarchyIncomingCalls(
1290     const CallHierarchyIncomingCallsParams &Params,
1291     Callback<std::vector<CallHierarchyIncomingCall>> Reply) {
1292   Server->incomingCalls(Params.item, std::move(Reply));
1293 }
1294 
onClangdInlayHints(const InlayHintsParams & Params,Callback<llvm::json::Value> Reply)1295 void ClangdLSPServer::onClangdInlayHints(const InlayHintsParams &Params,
1296                                          Callback<llvm::json::Value> Reply) {
1297   // Our extension has a different representation on the wire than the standard.
1298   // We have a "range" property and "kind" is represented as a string, not as an
1299   // enum value.
1300   // https://clangd.llvm.org/extensions#inlay-hints
1301   auto Serialize = [Reply = std::move(Reply)](
1302                        llvm::Expected<std::vector<InlayHint>> Hints) mutable {
1303     if (!Hints) {
1304       Reply(Hints.takeError());
1305       return;
1306     }
1307     llvm::json::Array Result;
1308     Result.reserve(Hints->size());
1309     for (auto &Hint : *Hints) {
1310       Result.emplace_back(llvm::json::Object{
1311           {"kind", llvm::to_string(Hint.kind)},
1312           {"range", Hint.range},
1313           {"position", Hint.position},
1314           // Extension doesn't have paddingLeft/Right so adjust the label
1315           // accordingly.
1316           {"label",
1317            ((Hint.paddingLeft ? " " : "") + llvm::StringRef(Hint.label) +
1318             (Hint.paddingRight ? " " : ""))
1319                .str()},
1320       });
1321     }
1322     Reply(std::move(Result));
1323   };
1324   Server->inlayHints(Params.textDocument.uri.file(), Params.range,
1325                      std::move(Serialize));
1326 }
1327 
onInlayHint(const InlayHintsParams & Params,Callback<std::vector<InlayHint>> Reply)1328 void ClangdLSPServer::onInlayHint(const InlayHintsParams &Params,
1329                                   Callback<std::vector<InlayHint>> Reply) {
1330   Server->inlayHints(Params.textDocument.uri.file(), Params.range,
1331                      std::move(Reply));
1332 }
1333 
applyConfiguration(const ConfigurationSettings & Settings)1334 void ClangdLSPServer::applyConfiguration(
1335     const ConfigurationSettings &Settings) {
1336   // Per-file update to the compilation database.
1337   llvm::StringSet<> ModifiedFiles;
1338   for (auto &Entry : Settings.compilationDatabaseChanges) {
1339     PathRef File = Entry.first;
1340     auto Old = CDB->getCompileCommand(File);
1341     auto New =
1342         tooling::CompileCommand(std::move(Entry.second.workingDirectory), File,
1343                                 std::move(Entry.second.compilationCommand),
1344                                 /*Output=*/"");
1345     if (Old != New) {
1346       CDB->setCompileCommand(File, std::move(New));
1347       ModifiedFiles.insert(File);
1348     }
1349   }
1350 
1351   Server->reparseOpenFilesIfNeeded(
1352       [&](llvm::StringRef File) { return ModifiedFiles.count(File) != 0; });
1353 }
1354 
maybeExportMemoryProfile()1355 void ClangdLSPServer::maybeExportMemoryProfile() {
1356   if (!trace::enabled() || !ShouldProfile())
1357     return;
1358 
1359   static constexpr trace::Metric MemoryUsage(
1360       "memory_usage", trace::Metric::Value, "component_name");
1361   trace::Span Tracer("ProfileBrief");
1362   MemoryTree MT;
1363   profile(MT);
1364   record(MT, "clangd_lsp_server", MemoryUsage);
1365 }
1366 
maybeCleanupMemory()1367 void ClangdLSPServer::maybeCleanupMemory() {
1368   if (!Opts.MemoryCleanup || !ShouldCleanupMemory())
1369     return;
1370   Opts.MemoryCleanup();
1371 }
1372 
1373 // FIXME: This function needs to be properly tested.
onChangeConfiguration(const DidChangeConfigurationParams & Params)1374 void ClangdLSPServer::onChangeConfiguration(
1375     const DidChangeConfigurationParams &Params) {
1376   applyConfiguration(Params.settings);
1377 }
1378 
onReference(const ReferenceParams & Params,Callback<std::vector<Location>> Reply)1379 void ClangdLSPServer::onReference(const ReferenceParams &Params,
1380                                   Callback<std::vector<Location>> Reply) {
1381   Server->findReferences(
1382       Params.textDocument.uri.file(), Params.position, Opts.ReferencesLimit,
1383       [Reply = std::move(Reply),
1384        IncludeDecl(Params.context.includeDeclaration)](
1385           llvm::Expected<ReferencesResult> Refs) mutable {
1386         if (!Refs)
1387           return Reply(Refs.takeError());
1388         // Filter out declarations if the client asked.
1389         std::vector<Location> Result;
1390         Result.reserve(Refs->References.size());
1391         for (auto &Ref : Refs->References) {
1392           bool IsDecl = Ref.Attributes & ReferencesResult::Declaration;
1393           if (IncludeDecl || !IsDecl)
1394             Result.push_back(std::move(Ref.Loc));
1395         }
1396         return Reply(std::move(Result));
1397       });
1398 }
1399 
onGoToType(const TextDocumentPositionParams & Params,Callback<std::vector<Location>> Reply)1400 void ClangdLSPServer::onGoToType(const TextDocumentPositionParams &Params,
1401                                  Callback<std::vector<Location>> Reply) {
1402   Server->findType(
1403       Params.textDocument.uri.file(), Params.position,
1404       [Reply = std::move(Reply)](
1405           llvm::Expected<std::vector<LocatedSymbol>> Types) mutable {
1406         if (!Types)
1407           return Reply(Types.takeError());
1408         std::vector<Location> Response;
1409         for (const LocatedSymbol &Sym : *Types)
1410           Response.push_back(Sym.PreferredDeclaration);
1411         return Reply(std::move(Response));
1412       });
1413 }
1414 
onGoToImplementation(const TextDocumentPositionParams & Params,Callback<std::vector<Location>> Reply)1415 void ClangdLSPServer::onGoToImplementation(
1416     const TextDocumentPositionParams &Params,
1417     Callback<std::vector<Location>> Reply) {
1418   Server->findImplementations(
1419       Params.textDocument.uri.file(), Params.position,
1420       [Reply = std::move(Reply)](
1421           llvm::Expected<std::vector<LocatedSymbol>> Overrides) mutable {
1422         if (!Overrides)
1423           return Reply(Overrides.takeError());
1424         std::vector<Location> Impls;
1425         for (const LocatedSymbol &Sym : *Overrides)
1426           Impls.push_back(Sym.PreferredDeclaration);
1427         return Reply(std::move(Impls));
1428       });
1429 }
1430 
onSymbolInfo(const TextDocumentPositionParams & Params,Callback<std::vector<SymbolDetails>> Reply)1431 void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params,
1432                                    Callback<std::vector<SymbolDetails>> Reply) {
1433   Server->symbolInfo(Params.textDocument.uri.file(), Params.position,
1434                      std::move(Reply));
1435 }
1436 
onSelectionRange(const SelectionRangeParams & Params,Callback<std::vector<SelectionRange>> Reply)1437 void ClangdLSPServer::onSelectionRange(
1438     const SelectionRangeParams &Params,
1439     Callback<std::vector<SelectionRange>> Reply) {
1440   Server->semanticRanges(
1441       Params.textDocument.uri.file(), Params.positions,
1442       [Reply = std::move(Reply)](
1443           llvm::Expected<std::vector<SelectionRange>> Ranges) mutable {
1444         if (!Ranges)
1445           return Reply(Ranges.takeError());
1446         return Reply(std::move(*Ranges));
1447       });
1448 }
1449 
onDocumentLink(const DocumentLinkParams & Params,Callback<std::vector<DocumentLink>> Reply)1450 void ClangdLSPServer::onDocumentLink(
1451     const DocumentLinkParams &Params,
1452     Callback<std::vector<DocumentLink>> Reply) {
1453 
1454   // TODO(forster): This currently resolves all targets eagerly. This is slow,
1455   // because it blocks on the preamble/AST being built. We could respond to the
1456   // request faster by using string matching or the lexer to find the includes
1457   // and resolving the targets lazily.
1458   Server->documentLinks(
1459       Params.textDocument.uri.file(),
1460       [Reply = std::move(Reply)](
1461           llvm::Expected<std::vector<DocumentLink>> Links) mutable {
1462         if (!Links) {
1463           return Reply(Links.takeError());
1464         }
1465         return Reply(std::move(Links));
1466       });
1467 }
1468 
1469 // Increment a numeric string: "" -> 1 -> 2 -> ... -> 9 -> 10 -> 11 ...
increment(std::string & S)1470 static void increment(std::string &S) {
1471   for (char &C : llvm::reverse(S)) {
1472     if (C != '9') {
1473       ++C;
1474       return;
1475     }
1476     C = '0';
1477   }
1478   S.insert(S.begin(), '1');
1479 }
1480 
onSemanticTokens(const SemanticTokensParams & Params,Callback<SemanticTokens> CB)1481 void ClangdLSPServer::onSemanticTokens(const SemanticTokensParams &Params,
1482                                        Callback<SemanticTokens> CB) {
1483   auto File = Params.textDocument.uri.file();
1484   Server->semanticHighlights(
1485       Params.textDocument.uri.file(),
1486       [this, File(File.str()), CB(std::move(CB)), Code(Server->getDraft(File))](
1487           llvm::Expected<std::vector<HighlightingToken>> HT) mutable {
1488         if (!HT)
1489           return CB(HT.takeError());
1490         SemanticTokens Result;
1491         Result.tokens = toSemanticTokens(*HT, *Code);
1492         {
1493           std::lock_guard<std::mutex> Lock(SemanticTokensMutex);
1494           auto &Last = LastSemanticTokens[File];
1495 
1496           Last.tokens = Result.tokens;
1497           increment(Last.resultId);
1498           Result.resultId = Last.resultId;
1499         }
1500         CB(std::move(Result));
1501       });
1502 }
1503 
onSemanticTokensDelta(const SemanticTokensDeltaParams & Params,Callback<SemanticTokensOrDelta> CB)1504 void ClangdLSPServer::onSemanticTokensDelta(
1505     const SemanticTokensDeltaParams &Params,
1506     Callback<SemanticTokensOrDelta> CB) {
1507   auto File = Params.textDocument.uri.file();
1508   Server->semanticHighlights(
1509       Params.textDocument.uri.file(),
1510       [this, PrevResultID(Params.previousResultId), File(File.str()),
1511        CB(std::move(CB)), Code(Server->getDraft(File))](
1512           llvm::Expected<std::vector<HighlightingToken>> HT) mutable {
1513         if (!HT)
1514           return CB(HT.takeError());
1515         std::vector<SemanticToken> Toks = toSemanticTokens(*HT, *Code);
1516 
1517         SemanticTokensOrDelta Result;
1518         {
1519           std::lock_guard<std::mutex> Lock(SemanticTokensMutex);
1520           auto &Last = LastSemanticTokens[File];
1521 
1522           if (PrevResultID == Last.resultId) {
1523             Result.edits = diffTokens(Last.tokens, Toks);
1524           } else {
1525             vlog("semanticTokens/full/delta: wanted edits vs {0} but last "
1526                  "result had ID {1}. Returning full token list.",
1527                  PrevResultID, Last.resultId);
1528             Result.tokens = Toks;
1529           }
1530 
1531           Last.tokens = std::move(Toks);
1532           increment(Last.resultId);
1533           Result.resultId = Last.resultId;
1534         }
1535 
1536         CB(std::move(Result));
1537       });
1538 }
1539 
onMemoryUsage(const NoParams &,Callback<MemoryTree> Reply)1540 void ClangdLSPServer::onMemoryUsage(const NoParams &,
1541                                     Callback<MemoryTree> Reply) {
1542   llvm::BumpPtrAllocator DetailAlloc;
1543   MemoryTree MT(&DetailAlloc);
1544   profile(MT);
1545   Reply(std::move(MT));
1546 }
1547 
onAST(const ASTParams & Params,Callback<llvm::Optional<ASTNode>> CB)1548 void ClangdLSPServer::onAST(const ASTParams &Params,
1549                             Callback<llvm::Optional<ASTNode>> CB) {
1550   Server->getAST(Params.textDocument.uri.file(), Params.range, std::move(CB));
1551 }
1552 
ClangdLSPServer(Transport & Transp,const ThreadsafeFS & TFS,const ClangdLSPServer::Options & Opts)1553 ClangdLSPServer::ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS,
1554                                  const ClangdLSPServer::Options &Opts)
1555     : ShouldProfile(/*Period=*/std::chrono::minutes(5),
1556                     /*Delay=*/std::chrono::minutes(1)),
1557       ShouldCleanupMemory(/*Period=*/std::chrono::minutes(1),
1558                           /*Delay=*/std::chrono::minutes(1)),
1559       BackgroundContext(Context::current().clone()), Transp(Transp),
1560       MsgHandler(new MessageHandler(*this)), TFS(TFS),
1561       SupportedSymbolKinds(defaultSymbolKinds()),
1562       SupportedCompletionItemKinds(defaultCompletionItemKinds()), Opts(Opts) {
1563   if (Opts.ConfigProvider) {
1564     assert(!Opts.ContextProvider &&
1565            "Only one of ConfigProvider and ContextProvider allowed!");
1566     this->Opts.ContextProvider = ClangdServer::createConfiguredContextProvider(
1567         Opts.ConfigProvider, this);
1568   }
1569   LSPBinder Bind(this->Handlers, *this);
1570   Bind.method("initialize", this, &ClangdLSPServer::onInitialize);
1571 }
1572 
bindMethods(LSPBinder & Bind,const ClientCapabilities & Caps)1573 void ClangdLSPServer::bindMethods(LSPBinder &Bind,
1574                                   const ClientCapabilities &Caps) {
1575   // clang-format off
1576   Bind.notification("initialized", this, &ClangdLSPServer::onInitialized);
1577   Bind.method("shutdown", this, &ClangdLSPServer::onShutdown);
1578   Bind.method("sync", this, &ClangdLSPServer::onSync);
1579   Bind.method("textDocument/rangeFormatting", this, &ClangdLSPServer::onDocumentRangeFormatting);
1580   Bind.method("textDocument/onTypeFormatting", this, &ClangdLSPServer::onDocumentOnTypeFormatting);
1581   Bind.method("textDocument/formatting", this, &ClangdLSPServer::onDocumentFormatting);
1582   Bind.method("textDocument/codeAction", this, &ClangdLSPServer::onCodeAction);
1583   Bind.method("textDocument/completion", this, &ClangdLSPServer::onCompletion);
1584   Bind.method("textDocument/signatureHelp", this, &ClangdLSPServer::onSignatureHelp);
1585   Bind.method("textDocument/definition", this, &ClangdLSPServer::onGoToDefinition);
1586   Bind.method("textDocument/declaration", this, &ClangdLSPServer::onGoToDeclaration);
1587   Bind.method("textDocument/typeDefinition", this, &ClangdLSPServer::onGoToType);
1588   Bind.method("textDocument/implementation", this, &ClangdLSPServer::onGoToImplementation);
1589   Bind.method("textDocument/references", this, &ClangdLSPServer::onReference);
1590   Bind.method("textDocument/switchSourceHeader", this, &ClangdLSPServer::onSwitchSourceHeader);
1591   Bind.method("textDocument/prepareRename", this, &ClangdLSPServer::onPrepareRename);
1592   Bind.method("textDocument/rename", this, &ClangdLSPServer::onRename);
1593   Bind.method("textDocument/hover", this, &ClangdLSPServer::onHover);
1594   Bind.method("textDocument/documentSymbol", this, &ClangdLSPServer::onDocumentSymbol);
1595   Bind.method("workspace/executeCommand", this, &ClangdLSPServer::onCommand);
1596   Bind.method("textDocument/documentHighlight", this, &ClangdLSPServer::onDocumentHighlight);
1597   Bind.method("workspace/symbol", this, &ClangdLSPServer::onWorkspaceSymbol);
1598   Bind.method("textDocument/ast", this, &ClangdLSPServer::onAST);
1599   Bind.notification("textDocument/didOpen", this, &ClangdLSPServer::onDocumentDidOpen);
1600   Bind.notification("textDocument/didClose", this, &ClangdLSPServer::onDocumentDidClose);
1601   Bind.notification("textDocument/didChange", this, &ClangdLSPServer::onDocumentDidChange);
1602   Bind.notification("textDocument/didSave", this, &ClangdLSPServer::onDocumentDidSave);
1603   Bind.notification("workspace/didChangeWatchedFiles", this, &ClangdLSPServer::onFileEvent);
1604   Bind.notification("workspace/didChangeConfiguration", this, &ClangdLSPServer::onChangeConfiguration);
1605   Bind.method("textDocument/symbolInfo", this, &ClangdLSPServer::onSymbolInfo);
1606   Bind.method("textDocument/typeHierarchy", this, &ClangdLSPServer::onTypeHierarchy);
1607   Bind.method("typeHierarchy/resolve", this, &ClangdLSPServer::onResolveTypeHierarchy);
1608   Bind.method("textDocument/prepareTypeHierarchy", this, &ClangdLSPServer::onPrepareTypeHierarchy);
1609   Bind.method("typeHierarchy/supertypes", this, &ClangdLSPServer::onSuperTypes);
1610   Bind.method("typeHierarchy/subtypes", this, &ClangdLSPServer::onSubTypes);
1611   Bind.method("textDocument/prepareCallHierarchy", this, &ClangdLSPServer::onPrepareCallHierarchy);
1612   Bind.method("callHierarchy/incomingCalls", this, &ClangdLSPServer::onCallHierarchyIncomingCalls);
1613   Bind.method("textDocument/selectionRange", this, &ClangdLSPServer::onSelectionRange);
1614   Bind.method("textDocument/documentLink", this, &ClangdLSPServer::onDocumentLink);
1615   Bind.method("textDocument/semanticTokens/full", this, &ClangdLSPServer::onSemanticTokens);
1616   Bind.method("textDocument/semanticTokens/full/delta", this, &ClangdLSPServer::onSemanticTokensDelta);
1617   Bind.method("clangd/inlayHints", this, &ClangdLSPServer::onClangdInlayHints);
1618   Bind.method("textDocument/inlayHint", this, &ClangdLSPServer::onInlayHint);
1619   Bind.method("$/memoryUsage", this, &ClangdLSPServer::onMemoryUsage);
1620   if (Opts.FoldingRanges)
1621     Bind.method("textDocument/foldingRange", this, &ClangdLSPServer::onFoldingRange);
1622   Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
1623   Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
1624 
1625   ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
1626   PublishDiagnostics = Bind.outgoingNotification("textDocument/publishDiagnostics");
1627   ShowMessage = Bind.outgoingNotification("window/showMessage");
1628   NotifyFileStatus = Bind.outgoingNotification("textDocument/clangd.fileStatus");
1629   CreateWorkDoneProgress = Bind.outgoingMethod("window/workDoneProgress/create");
1630   BeginWorkDoneProgress = Bind.outgoingNotification("$/progress");
1631   ReportWorkDoneProgress = Bind.outgoingNotification("$/progress");
1632   EndWorkDoneProgress = Bind.outgoingNotification("$/progress");
1633   if(Caps.SemanticTokenRefreshSupport)
1634     SemanticTokensRefresh = Bind.outgoingMethod("workspace/semanticTokens/refresh");
1635   // clang-format on
1636 }
1637 
~ClangdLSPServer()1638 ClangdLSPServer::~ClangdLSPServer() {
1639   IsBeingDestroyed = true;
1640   // Explicitly destroy ClangdServer first, blocking on threads it owns.
1641   // This ensures they don't access any other members.
1642   Server.reset();
1643 }
1644 
run()1645 bool ClangdLSPServer::run() {
1646   // Run the Language Server loop.
1647   bool CleanExit = true;
1648   if (auto Err = Transp.loop(*MsgHandler)) {
1649     elog("Transport error: {0}", std::move(Err));
1650     CleanExit = false;
1651   }
1652 
1653   return CleanExit && ShutdownRequestReceived;
1654 }
1655 
profile(MemoryTree & MT) const1656 void ClangdLSPServer::profile(MemoryTree &MT) const {
1657   if (Server)
1658     Server->profile(MT.child("clangd_server"));
1659 }
1660 
getFixes(llvm::StringRef File,const clangd::Diagnostic & D)1661 std::vector<Fix> ClangdLSPServer::getFixes(llvm::StringRef File,
1662                                            const clangd::Diagnostic &D) {
1663   std::lock_guard<std::mutex> Lock(FixItsMutex);
1664   auto DiagToFixItsIter = FixItsMap.find(File);
1665   if (DiagToFixItsIter == FixItsMap.end())
1666     return {};
1667 
1668   const auto &DiagToFixItsMap = DiagToFixItsIter->second;
1669   auto FixItsIter = DiagToFixItsMap.find(D);
1670   if (FixItsIter == DiagToFixItsMap.end())
1671     return {};
1672 
1673   return FixItsIter->second;
1674 }
1675 
1676 // A completion request is sent when the user types '>' or ':', but we only
1677 // want to trigger on '->' and '::'. We check the preceeding text to make
1678 // sure it matches what we expected.
1679 // Running the lexer here would be more robust (e.g. we can detect comments
1680 // and avoid triggering completion there), but we choose to err on the side
1681 // of simplicity here.
shouldRunCompletion(const CompletionParams & Params) const1682 bool ClangdLSPServer::shouldRunCompletion(
1683     const CompletionParams &Params) const {
1684   if (Params.context.triggerKind != CompletionTriggerKind::TriggerCharacter)
1685     return true;
1686   auto Code = Server->getDraft(Params.textDocument.uri.file());
1687   if (!Code)
1688     return true; // completion code will log the error for untracked doc.
1689   auto Offset = positionToOffset(*Code, Params.position,
1690                                  /*AllowColumnsBeyondLineLength=*/false);
1691   if (!Offset) {
1692     vlog("could not convert position '{0}' to offset for file '{1}'",
1693          Params.position, Params.textDocument.uri.file());
1694     return true;
1695   }
1696   return allowImplicitCompletion(*Code, *Offset);
1697 }
1698 
onDiagnosticsReady(PathRef File,llvm::StringRef Version,std::vector<Diag> Diagnostics)1699 void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version,
1700                                          std::vector<Diag> Diagnostics) {
1701   PublishDiagnosticsParams Notification;
1702   Notification.version = decodeVersion(Version);
1703   Notification.uri = URIForFile::canonicalize(File, /*TUPath=*/File);
1704   DiagnosticToReplacementMap LocalFixIts; // Temporary storage
1705   for (auto &Diag : Diagnostics) {
1706     toLSPDiags(Diag, Notification.uri, DiagOpts,
1707                [&](clangd::Diagnostic Diag, llvm::ArrayRef<Fix> Fixes) {
1708                  auto &FixItsForDiagnostic = LocalFixIts[Diag];
1709                  llvm::copy(Fixes, std::back_inserter(FixItsForDiagnostic));
1710                  Notification.diagnostics.push_back(std::move(Diag));
1711                });
1712   }
1713 
1714   // Cache FixIts
1715   {
1716     std::lock_guard<std::mutex> Lock(FixItsMutex);
1717     FixItsMap[File] = LocalFixIts;
1718   }
1719 
1720   // Send a notification to the LSP client.
1721   PublishDiagnostics(Notification);
1722 }
1723 
onBackgroundIndexProgress(const BackgroundQueue::Stats & Stats)1724 void ClangdLSPServer::onBackgroundIndexProgress(
1725     const BackgroundQueue::Stats &Stats) {
1726   static const char ProgressToken[] = "backgroundIndexProgress";
1727 
1728   // The background index did some work, maybe we need to cleanup
1729   maybeCleanupMemory();
1730 
1731   std::lock_guard<std::mutex> Lock(BackgroundIndexProgressMutex);
1732 
1733   auto NotifyProgress = [this](const BackgroundQueue::Stats &Stats) {
1734     if (BackgroundIndexProgressState != BackgroundIndexProgress::Live) {
1735       WorkDoneProgressBegin Begin;
1736       Begin.percentage = true;
1737       Begin.title = "indexing";
1738       BeginWorkDoneProgress({ProgressToken, std::move(Begin)});
1739       BackgroundIndexProgressState = BackgroundIndexProgress::Live;
1740     }
1741 
1742     if (Stats.Completed < Stats.Enqueued) {
1743       assert(Stats.Enqueued > Stats.LastIdle);
1744       WorkDoneProgressReport Report;
1745       Report.percentage = 100 * (Stats.Completed - Stats.LastIdle) /
1746                           (Stats.Enqueued - Stats.LastIdle);
1747       Report.message =
1748           llvm::formatv("{0}/{1}", Stats.Completed - Stats.LastIdle,
1749                         Stats.Enqueued - Stats.LastIdle);
1750       ReportWorkDoneProgress({ProgressToken, std::move(Report)});
1751     } else {
1752       assert(Stats.Completed == Stats.Enqueued);
1753       EndWorkDoneProgress({ProgressToken, WorkDoneProgressEnd()});
1754       BackgroundIndexProgressState = BackgroundIndexProgress::Empty;
1755     }
1756   };
1757 
1758   switch (BackgroundIndexProgressState) {
1759   case BackgroundIndexProgress::Unsupported:
1760     return;
1761   case BackgroundIndexProgress::Creating:
1762     // Cache this update for when the progress bar is available.
1763     PendingBackgroundIndexProgress = Stats;
1764     return;
1765   case BackgroundIndexProgress::Empty: {
1766     if (BackgroundIndexSkipCreate) {
1767       NotifyProgress(Stats);
1768       break;
1769     }
1770     // Cache this update for when the progress bar is available.
1771     PendingBackgroundIndexProgress = Stats;
1772     BackgroundIndexProgressState = BackgroundIndexProgress::Creating;
1773     WorkDoneProgressCreateParams CreateRequest;
1774     CreateRequest.token = ProgressToken;
1775     CreateWorkDoneProgress(
1776         CreateRequest,
1777         [this, NotifyProgress](llvm::Expected<std::nullptr_t> E) {
1778           std::lock_guard<std::mutex> Lock(BackgroundIndexProgressMutex);
1779           if (E) {
1780             NotifyProgress(this->PendingBackgroundIndexProgress);
1781           } else {
1782             elog("Failed to create background index progress bar: {0}",
1783                  E.takeError());
1784             // give up forever rather than thrashing about
1785             BackgroundIndexProgressState = BackgroundIndexProgress::Unsupported;
1786           }
1787         });
1788     break;
1789   }
1790   case BackgroundIndexProgress::Live:
1791     NotifyProgress(Stats);
1792     break;
1793   }
1794 }
1795 
onFileUpdated(PathRef File,const TUStatus & Status)1796 void ClangdLSPServer::onFileUpdated(PathRef File, const TUStatus &Status) {
1797   if (!SupportFileStatus)
1798     return;
1799   // FIXME: we don't emit "BuildingFile" and `RunningAction`, as these
1800   // two statuses are running faster in practice, which leads the UI constantly
1801   // changing, and doesn't provide much value. We may want to emit status at a
1802   // reasonable time interval (e.g. 0.5s).
1803   if (Status.PreambleActivity == PreambleAction::Idle &&
1804       (Status.ASTActivity.K == ASTAction::Building ||
1805        Status.ASTActivity.K == ASTAction::RunningAction))
1806     return;
1807   NotifyFileStatus(Status.render(File));
1808 }
1809 
onSemanticsMaybeChanged(PathRef File)1810 void ClangdLSPServer::onSemanticsMaybeChanged(PathRef File) {
1811   if (SemanticTokensRefresh) {
1812     SemanticTokensRefresh(NoParams{}, [](llvm::Expected<std::nullptr_t> E) {
1813       if (E)
1814         return;
1815       elog("Failed to refresh semantic tokens: {0}", E.takeError());
1816     });
1817   }
1818 }
1819 } // namespace clangd
1820 } // namespace clang
1821