Mercurial > hg > CbC > CbC_llvm
diff clang-tools-extra/clangd/ClangdLSPServer.cpp @ 221:79ff65ed7e25
LLVM12 Original
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 15 Jun 2021 19:15:29 +0900 |
parents | 0572611fdcc8 |
children | 5f17cb93ff66 |
line wrap: on
line diff
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp Tue Jun 15 19:13:43 2021 +0900 +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp Tue Jun 15 19:15:29 2021 +0900 @@ -7,10 +7,13 @@ //===----------------------------------------------------------------------===// #include "ClangdLSPServer.h" +#include "ClangdServer.h" #include "CodeComplete.h" #include "Diagnostics.h" #include "DraftStore.h" +#include "DumpAST.h" #include "GlobalCompilationDatabase.h" +#include "LSPBinder.h" #include "Protocol.h" #include "SemanticHighlighting.h" #include "SourceCode.h" @@ -18,7 +21,9 @@ #include "URI.h" #include "refactor/Tweak.h" #include "support/Context.h" +#include "support/MemoryTree.h" #include "support/Trace.h" +#include "clang/AST/ASTContext.h" #include "clang/Basic/Version.h" #include "clang/Tooling/Core/Replacement.h" #include "llvm/ADT/ArrayRef.h" @@ -26,6 +31,7 @@ #include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" @@ -33,7 +39,11 @@ #include "llvm/Support/Path.h" #include "llvm/Support/SHA1.h" #include "llvm/Support/ScopedPrinter.h" +#include "llvm/Support/raw_ostream.h" +#include <chrono> #include <cstddef> +#include <cstdint> +#include <functional> #include <memory> #include <mutex> #include <string> @@ -42,7 +52,6 @@ namespace clang { namespace clangd { namespace { - // Tracks end-to-end latency of high level lsp calls. Measurements are in // seconds. constexpr trace::Metric LSPLatency("lsp_latency", trace::Metric::Distribution, @@ -50,32 +59,28 @@ // LSP defines file versions as numbers that increase. // ClangdServer treats them as opaque and therefore uses strings instead. -std::string encodeVersion(int64_t LSPVersion) { - return llvm::to_string(LSPVersion); +std::string encodeVersion(llvm::Optional<int64_t> LSPVersion) { + return LSPVersion ? llvm::to_string(*LSPVersion) : ""; } llvm::Optional<int64_t> decodeVersion(llvm::StringRef Encoded) { int64_t Result; if (llvm::to_integer(Encoded, Result, 10)) return Result; - else if (!Encoded.empty()) // Empty can be e.g. diagnostics on close. + if (!Encoded.empty()) // Empty can be e.g. diagnostics on close. elog("unexpected non-numeric version {0}", Encoded); return llvm::None; } +const llvm::StringLiteral APPLY_FIX_COMMAND = "clangd.applyFix"; +const llvm::StringLiteral APPLY_TWEAK_COMMAND = "clangd.applyTweak"; + /// Transforms a tweak into a code action that would apply it if executed. /// EXPECTS: T.prepare() was called and returned true. CodeAction toCodeAction(const ClangdServer::TweakRef &T, const URIForFile &File, Range Selection) { CodeAction CA; CA.title = T.Title; - switch (T.Intent) { - case Tweak::Refactor: - CA.kind = std::string(CodeAction::REFACTOR_KIND); - break; - case Tweak::Info: - CA.kind = std::string(CodeAction::INFO_KIND); - break; - } + CA.kind = T.Kind.str(); // This tweak may have an expensive second stage, we only run it if the user // actually chooses it in the UI. We reply with a command that would run the // corresponding tweak. @@ -83,11 +88,12 @@ // directly. CA.command.emplace(); CA.command->title = T.Title; - CA.command->command = std::string(Command::CLANGD_APPLY_TWEAK); - CA.command->tweakArgs.emplace(); - CA.command->tweakArgs->file = File; - CA.command->tweakArgs->tweakID = T.ID; - CA.command->tweakArgs->selection = Selection; + CA.command->command = std::string(APPLY_TWEAK_COMMAND); + TweakArgs Args; + Args.file = File; + Args.tweakID = T.ID; + Args.selection = Selection; + CA.command->argument = std::move(Args); return CA; } @@ -115,30 +121,18 @@ return Defaults; } -// Build a lookup table (HighlightingKind => {TextMate Scopes}), which is sent -// to the LSP client. -std::vector<std::vector<std::string>> buildHighlightScopeLookupTable() { - std::vector<std::vector<std::string>> LookupTable; - // HighlightingKind is using as the index. - for (int KindValue = 0; KindValue <= (int)HighlightingKind::LastKind; - ++KindValue) - LookupTable.push_back( - {std::string(toTextMateScope((HighlightingKind)(KindValue)))}); - return LookupTable; -} - // Makes sure edits in \p FE are applicable to latest file contents reported by // editor. If not generates an error message containing information about files // that needs to be saved. -llvm::Error validateEdits(const DraftStore &DraftMgr, const FileEdits &FE) { +llvm::Error validateEdits(const ClangdServer &Server, const FileEdits &FE) { size_t InvalidFileCount = 0; llvm::StringRef LastInvalidFile; for (const auto &It : FE) { - if (auto Draft = DraftMgr.getDraft(It.first())) { + if (auto Draft = Server.getDraft(It.first())) { // If the file is open in user's editor, make sure the version we // saw and current version are compatible as this is the text that // will be replaced by editors. - if (!It.second.canApplyTo(Draft->Contents)) { + if (!It.second.canApplyTo(*Draft)) { ++InvalidFileCount; LastInvalidFile = It.first(); } @@ -147,15 +141,10 @@ if (!InvalidFileCount) return llvm::Error::success(); if (InvalidFileCount == 1) - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "File must be saved first: " + - LastInvalidFile); - return llvm::createStringError( - llvm::inconvertibleErrorCode(), - "Files must be saved first: " + LastInvalidFile + " (and " + - llvm::to_string(InvalidFileCount - 1) + " others)"); + return error("File must be saved first: {0}", LastInvalidFile); + return error("Files must be saved first: {0} (and {1} others)", + LastInvalidFile, InvalidFileCount - 1); } - } // namespace // MessageHandler dispatches incoming LSP messages. @@ -170,18 +159,24 @@ MessageHandler(ClangdLSPServer &Server) : Server(Server) {} bool onNotify(llvm::StringRef Method, llvm::json::Value Params) override { + trace::Span Tracer(Method, LSPLatency); + SPAN_ATTACH(Tracer, "Params", Params); WithContext HandlerContext(handlerContext()); log("<-- {0}", Method); if (Method == "exit") return false; - if (!Server.Server) + auto Handler = Server.Handlers.NotificationHandlers.find(Method); + if (Handler != Server.Handlers.NotificationHandlers.end()) { + Handler->second(std::move(Params)); + Server.maybeExportMemoryProfile(); + Server.maybeCleanupMemory(); + } else if (!Server.Server) { elog("Notification {0} before initialization", Method); - else if (Method == "$/cancelRequest") + } else if (Method == "$/cancelRequest") { onCancel(std::move(Params)); - else if (auto Handler = Notifications.lookup(Method)) - Handler(std::move(Params)); - else + } else { log("unhandled notification {0}", Method); + } return true; } @@ -194,15 +189,17 @@ SPAN_ATTACH(Tracer, "Params", Params); ReplyOnce Reply(ID, Method, &Server, Tracer.Args); log("<-- {0}({1})", Method, ID); - if (!Server.Server && Method != "initialize") { + auto Handler = Server.Handlers.MethodHandlers.find(Method); + if (Handler != Server.Handlers.MethodHandlers.end()) { + Handler->second(std::move(Params), std::move(Reply)); + } else if (!Server.Server) { elog("Call {0} before initialization.", Method); Reply(llvm::make_error<LSPError>("server not initialized", ErrorCode::ServerNotInitialized)); - } else if (auto Handler = Calls.lookup(Method)) - Handler(std::move(Params), std::move(Reply)); - else + } else { Reply(llvm::make_error<LSPError>("method not found", ErrorCode::MethodNotFound)); + } return true; } @@ -245,23 +242,6 @@ return true; } - // Bind an LSP method name to a call. - template <typename Param, typename Result> - void bind(const char *Method, - void (ClangdLSPServer::*Handler)(const Param &, Callback<Result>)) { - Calls[Method] = [Method, Handler, this](llvm::json::Value RawParams, - ReplyOnce Reply) { - Param P; - if (fromJSON(RawParams, P)) { - (Server.*Handler)(P, std::move(Reply)); - } else { - elog("Failed to decode {0} request.", Method); - Reply(llvm::make_error<LSPError>("failed to decode request", - ErrorCode::InvalidRequest)); - } - }; - } - // Bind a reply callback to a request. The callback will be invoked when // clangd receives the reply from the LSP client. // Return a call id of the request. @@ -284,30 +264,12 @@ } } if (OldestCB) - OldestCB->second(llvm::createStringError( - llvm::inconvertibleErrorCode(), - llvm::formatv("failed to receive a client reply for request ({0})", - OldestCB->first))); + OldestCB->second( + error("failed to receive a client reply for request ({0})", + OldestCB->first)); return ID; } - // Bind an LSP method name to a notification. - template <typename Param> - void bind(const char *Method, - void (ClangdLSPServer::*Handler)(const Param &)) { - Notifications[Method] = [Method, Handler, - this](llvm::json::Value RawParams) { - Param P; - if (!fromJSON(RawParams, P)) { - elog("Failed to decode {0} request.", Method); - return; - } - trace::Span Tracer(Method, LSPLatency); - SPAN_ATTACH(Tracer, "Params", RawParams); - (Server.*Handler)(P); - }; - } - private: // Function object to reply to an LSP call. // Each instance must be called exactly once, otherwise: @@ -379,9 +341,6 @@ } }; - llvm::StringMap<std::function<void(llvm::json::Value)>> Notifications; - llvm::StringMap<std::function<void(llvm::json::Value, ReplyOnce)>> Calls; - // Method calls may be cancelled by ID, so keep track of their state. // This needs a mutex: handlers may finish on a different thread, and that's // when we clean up entries in the map. @@ -406,7 +365,7 @@ Context handlerContext() const { return Context::current().derive( kCurrentOffsetEncoding, - Server.NegotiatedOffsetEncoding.getValueOr(OffsetEncoding::UTF16)); + Server.Opts.Encoding.getValueOr(OffsetEncoding::UTF16)); } // We run cancelable requests in a context that does two things: @@ -451,8 +410,8 @@ constexpr int ClangdLSPServer::MessageHandler::MaxReplayCallbacks; // call(), notify(), and reply() wrap the Transport, adding logging and locking. -void ClangdLSPServer::callRaw(StringRef Method, llvm::json::Value Params, - Callback<llvm::json::Value> CB) { +void ClangdLSPServer::callMethod(StringRef Method, llvm::json::Value Params, + Callback<llvm::json::Value> CB) { auto ID = MsgHandler->bindReply(std::move(CB)); log("--> {0}({1})", Method, ID); std::lock_guard<std::mutex> Lock(TranspWriter); @@ -461,6 +420,7 @@ void ClangdLSPServer::notify(llvm::StringRef Method, llvm::json::Value Params) { log("--> {0}", Method); + maybeCleanupMemory(); std::lock_guard<std::mutex> Lock(TranspWriter); Transp.notify(Method, std::move(Params)); } @@ -473,67 +433,73 @@ return Types; } +static std::vector<llvm::StringRef> semanticTokenModifiers() { + std::vector<llvm::StringRef> Modifiers; + for (unsigned I = 0; + I <= static_cast<unsigned>(HighlightingModifier::LastModifier); ++I) + Modifiers.push_back( + toSemanticTokenModifier(static_cast<HighlightingModifier>(I))); + return Modifiers; +} + void ClangdLSPServer::onInitialize(const InitializeParams &Params, Callback<llvm::json::Value> Reply) { // Determine character encoding first as it affects constructed ClangdServer. - if (Params.capabilities.offsetEncoding && !NegotiatedOffsetEncoding) { - NegotiatedOffsetEncoding = OffsetEncoding::UTF16; // fallback + if (Params.capabilities.offsetEncoding && !Opts.Encoding) { + Opts.Encoding = OffsetEncoding::UTF16; // fallback for (OffsetEncoding Supported : *Params.capabilities.offsetEncoding) if (Supported != OffsetEncoding::UnsupportedEncoding) { - NegotiatedOffsetEncoding = Supported; + Opts.Encoding = Supported; break; } } - ClangdServerOpts.TheiaSemanticHighlighting = - Params.capabilities.TheiaSemanticHighlighting; if (Params.capabilities.TheiaSemanticHighlighting && - Params.capabilities.SemanticTokens) { - log("Client supports legacy semanticHighlights notification and standard " - "semanticTokens request, choosing the latter (no notifications)."); - ClangdServerOpts.TheiaSemanticHighlighting = false; + !Params.capabilities.SemanticTokens) { + elog("Client requested legacy semanticHighlights notification, which is " + "no longer supported. Migrate to standard semanticTokens request"); } if (Params.rootUri && *Params.rootUri) - ClangdServerOpts.WorkspaceRoot = std::string(Params.rootUri->file()); + Opts.WorkspaceRoot = std::string(Params.rootUri->file()); else if (Params.rootPath && !Params.rootPath->empty()) - ClangdServerOpts.WorkspaceRoot = *Params.rootPath; + Opts.WorkspaceRoot = *Params.rootPath; if (Server) return Reply(llvm::make_error<LSPError>("server already initialized", ErrorCode::InvalidRequest)); - if (const auto &Dir = Params.initializationOptions.compilationDatabasePath) - CompileCommandsDir = Dir; - if (UseDirBasedCDB) { - BaseCDB = std::make_unique<DirectoryBasedGlobalCompilationDatabase>( - CompileCommandsDir); - BaseCDB = getQueryDriverDatabase( - llvm::makeArrayRef(ClangdServerOpts.QueryDriverGlobs), - std::move(BaseCDB)); + if (Opts.UseDirBasedCDB) { + DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS); + if (const auto &Dir = Params.initializationOptions.compilationDatabasePath) + CDBOpts.CompileCommandsDir = Dir; + CDBOpts.ContextProvider = Opts.ContextProvider; + BaseCDB = + std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts); + BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs), + std::move(BaseCDB)); } auto Mangler = CommandMangler::detect(); - if (ClangdServerOpts.ResourceDir) - Mangler.ResourceDir = *ClangdServerOpts.ResourceDir; + if (Opts.ResourceDir) + Mangler.ResourceDir = *Opts.ResourceDir; CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags, - tooling::ArgumentsAdjuster(Mangler)); + tooling::ArgumentsAdjuster(std::move(Mangler))); { // Switch caller's context with LSPServer's background context. Since we // rather want to propagate information from LSPServer's context into the // Server, CDB, etc. WithContext MainContext(BackgroundContext.clone()); llvm::Optional<WithContextValue> WithOffsetEncoding; - if (NegotiatedOffsetEncoding) - WithOffsetEncoding.emplace(kCurrentOffsetEncoding, - *NegotiatedOffsetEncoding); - Server.emplace(*CDB, FSProvider, ClangdServerOpts, + if (Opts.Encoding) + WithOffsetEncoding.emplace(kCurrentOffsetEncoding, *Opts.Encoding); + Server.emplace(*CDB, TFS, Opts, static_cast<ClangdServer::Callbacks *>(this)); } applyConfiguration(Params.initializationOptions.ConfigSettings); - CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets; - CCOpts.IncludeFixIts = Params.capabilities.CompletionFixes; - if (!CCOpts.BundleOverloads.hasValue()) - CCOpts.BundleOverloads = Params.capabilities.HasSignatureHelp; - CCOpts.DocumentationFormat = + Opts.CodeComplete.EnableSnippets = Params.capabilities.CompletionSnippets; + Opts.CodeComplete.IncludeFixIts = Params.capabilities.CompletionFixes; + if (!Opts.CodeComplete.BundleOverloads.hasValue()) + Opts.CodeComplete.BundleOverloads = Params.capabilities.HasSignatureHelp; + Opts.CodeComplete.DocumentationFormat = Params.capabilities.CompletionDocumentationFormat; DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes; DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory; @@ -552,97 +518,118 @@ if (Params.capabilities.WorkDoneProgress) BackgroundIndexProgressState = BackgroundIndexProgress::Empty; BackgroundIndexSkipCreate = Params.capabilities.ImplicitProgressCreation; + Opts.ImplicitCancellation = !Params.capabilities.CancelsStaleRequests; + + llvm::json::Object ServerCaps{ + {"textDocumentSync", + llvm::json::Object{ + {"openClose", true}, + {"change", (int)TextDocumentSyncKind::Incremental}, + {"save", true}, + }}, + {"documentFormattingProvider", true}, + {"documentRangeFormattingProvider", true}, + {"documentOnTypeFormattingProvider", + llvm::json::Object{ + {"firstTriggerCharacter", "\n"}, + {"moreTriggerCharacter", {}}, + }}, + {"completionProvider", + llvm::json::Object{ + {"allCommitCharacters", + {" ", "\t", "(", ")", "[", "]", "{", "}", "<", + ">", ":", ";", ",", "+", "-", "/", "*", "%", + "^", "&", "#", "?", ".", "=", "\"", "'", "|"}}, + {"resolveProvider", false}, + // We do extra checks, e.g. that > is part of ->. + {"triggerCharacters", {".", "<", ">", ":", "\"", "/"}}, + }}, + {"semanticTokensProvider", + llvm::json::Object{ + {"full", llvm::json::Object{{"delta", true}}}, + {"range", false}, + {"legend", + llvm::json::Object{{"tokenTypes", semanticTokenTypes()}, + {"tokenModifiers", semanticTokenModifiers()}}}, + }}, + {"signatureHelpProvider", + llvm::json::Object{ + {"triggerCharacters", {"(", ","}}, + }}, + {"declarationProvider", true}, + {"definitionProvider", true}, + {"implementationProvider", true}, + {"documentHighlightProvider", true}, + {"documentLinkProvider", + llvm::json::Object{ + {"resolveProvider", false}, + }}, + {"hoverProvider", true}, + {"selectionRangeProvider", true}, + {"documentSymbolProvider", true}, + {"workspaceSymbolProvider", true}, + {"referencesProvider", true}, + {"astProvider", true}, // clangd extension + {"typeHierarchyProvider", true}, + {"memoryUsageProvider", true}, // clangd extension + {"compilationDatabase", // clangd extension + llvm::json::Object{{"automaticReload", true}}}, + {"callHierarchyProvider", true}, + }; + + { + LSPBinder Binder(Handlers, *this); + bindMethods(Binder, Params.capabilities); + if (Opts.FeatureModules) + for (auto &Mod : *Opts.FeatureModules) + Mod.initializeLSP(Binder, Params.rawCapabilities, ServerCaps); + } // Per LSP, renameProvider can be either boolean or RenameOptions. // RenameOptions will be specified if the client states it supports prepare. - llvm::json::Value RenameProvider = - llvm::json::Object{{"prepareProvider", true}}; - if (!Params.capabilities.RenamePrepareSupport) // Only boolean allowed per LSP - RenameProvider = true; + ServerCaps["renameProvider"] = + Params.capabilities.RenamePrepareSupport + ? llvm::json::Object{{"prepareProvider", true}} + : llvm::json::Value(true); - // Per LSP, codeActionProvide can be either boolean or CodeActionOptions. + // Per LSP, codeActionProvider can be either boolean or CodeActionOptions. // CodeActionOptions is only valid if the client supports action literal // via textDocument.codeAction.codeActionLiteralSupport. llvm::json::Value CodeActionProvider = true; - if (Params.capabilities.CodeActionStructure) - CodeActionProvider = llvm::json::Object{ - {"codeActionKinds", - {CodeAction::QUICKFIX_KIND, CodeAction::REFACTOR_KIND, - CodeAction::INFO_KIND}}}; + ServerCaps["codeActionProvider"] = + Params.capabilities.CodeActionStructure + ? llvm::json::Object{{"codeActionKinds", + {CodeAction::QUICKFIX_KIND, + CodeAction::REFACTOR_KIND, + CodeAction::INFO_KIND}}} + : llvm::json::Value(true); + + if (Opts.FoldingRanges) + ServerCaps["foldingRangeProvider"] = true; + + if (Opts.InlayHints) + ServerCaps["clangdInlayHintsProvider"] = true; + + std::vector<llvm::StringRef> Commands; + for (llvm::StringRef Command : Handlers.CommandHandlers.keys()) + Commands.push_back(Command); + llvm::sort(Commands); + ServerCaps["executeCommandProvider"] = + llvm::json::Object{{"commands", Commands}}; llvm::json::Object Result{ {{"serverInfo", llvm::json::Object{{"name", "clangd"}, {"version", getClangToolFullVersion("clangd")}}}, - {"capabilities", - llvm::json::Object{ - {"textDocumentSync", - llvm::json::Object{ - {"openClose", true}, - {"change", (int)TextDocumentSyncKind::Incremental}, - {"save", true}, - }}, - {"documentFormattingProvider", true}, - {"documentRangeFormattingProvider", true}, - {"documentOnTypeFormattingProvider", - llvm::json::Object{ - {"firstTriggerCharacter", "\n"}, - {"moreTriggerCharacter", {}}, - }}, - {"codeActionProvider", std::move(CodeActionProvider)}, - {"completionProvider", - llvm::json::Object{ - {"allCommitCharacters", " \t()[]{}<>:;,+-/*%^&#?.=\"'|"}, - {"resolveProvider", false}, - // We do extra checks, e.g. that > is part of ->. - {"triggerCharacters", {".", "<", ">", ":", "\"", "/"}}, - }}, - {"semanticTokensProvider", - llvm::json::Object{ - {"documentProvider", llvm::json::Object{{"edits", true}}}, - {"rangeProvider", false}, - {"legend", - llvm::json::Object{{"tokenTypes", semanticTokenTypes()}, - {"tokenModifiers", llvm::json::Array()}}}, - }}, - {"signatureHelpProvider", - llvm::json::Object{ - {"triggerCharacters", {"(", ","}}, - }}, - {"declarationProvider", true}, - {"definitionProvider", true}, - {"documentHighlightProvider", true}, - {"documentLinkProvider", - llvm::json::Object{ - {"resolveProvider", false}, - }}, - {"hoverProvider", true}, - {"renameProvider", std::move(RenameProvider)}, - {"selectionRangeProvider", true}, - {"documentSymbolProvider", true}, - {"workspaceSymbolProvider", true}, - {"referencesProvider", true}, - {"executeCommandProvider", - llvm::json::Object{ - {"commands", - {ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND, - ExecuteCommandParams::CLANGD_APPLY_TWEAK}}, - }}, - {"typeHierarchyProvider", true}, - }}}}; - if (NegotiatedOffsetEncoding) - Result["offsetEncoding"] = *NegotiatedOffsetEncoding; - if (ClangdServerOpts.TheiaSemanticHighlighting) - Result.getObject("capabilities") - ->insert( - {"semanticHighlighting", - llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}}); + {"capabilities", std::move(ServerCaps)}}}; + if (Opts.Encoding) + Result["offsetEncoding"] = *Opts.Encoding; Reply(std::move(Result)); } void ClangdLSPServer::onInitialized(const InitializedParams &Params) {} -void ClangdLSPServer::onShutdown(const ShutdownParams &Params, +void ClangdLSPServer::onShutdown(const NoParams &, Callback<std::nullptr_t> Reply) { // Do essentially nothing, just say we're ready to exit. ShutdownRequestReceived = true; @@ -651,13 +638,11 @@ // sync is a clangd extension: it blocks until all background work completes. // It blocks the calling thread, so no messages are processed until it returns! -void ClangdLSPServer::onSync(const NoParams &Params, - Callback<std::nullptr_t> Reply) { +void ClangdLSPServer::onSync(const NoParams &, Callback<std::nullptr_t> Reply) { if (Server->blockUntilIdleForTest(/*TimeoutSeconds=*/60)) Reply(nullptr); else - Reply(llvm::createStringError(llvm::inconvertibleErrorCode(), - "Not idle after a minute")); + Reply(error("Not idle after a minute")); } void ClangdLSPServer::onDocumentDidOpen( @@ -666,8 +651,8 @@ const std::string &Contents = Params.textDocument.text; - auto Version = DraftMgr.addDraft(File, Params.textDocument.version, Contents); - Server->addDocument(File, Contents, encodeVersion(Version), + Server->addDocument(File, Contents, + encodeVersion(Params.textDocument.version), WantDiagnostics::Yes); } @@ -679,25 +664,29 @@ : WantDiagnostics::No; PathRef File = Params.textDocument.uri.file(); - llvm::Expected<DraftStore::Draft> Draft = DraftMgr.updateDraft( - File, Params.textDocument.version, Params.contentChanges); - if (!Draft) { - // If this fails, we are most likely going to be not in sync anymore with - // the client. It is better to remove the draft and let further operations - // fail rather than giving wrong results. - DraftMgr.removeDraft(File); - Server->removeDocument(File); - elog("Failed to update {0}: {1}", File, Draft.takeError()); + auto Code = Server->getDraft(File); + if (!Code) { + log("Trying to incrementally change non-added document: {0}", File); return; } - - Server->addDocument(File, Draft->Contents, encodeVersion(Draft->Version), + std::string NewCode(*Code); + for (const auto &Change : Params.contentChanges) { + if (auto Err = applyChange(NewCode, Change)) { + // If this fails, we are most likely going to be not in sync anymore with + // the client. It is better to remove the draft and let further + // operations fail rather than giving wrong results. + Server->removeDocument(File); + elog("Failed to update {0}: {1}", File, std::move(Err)); + return; + } + } + Server->addDocument(File, NewCode, encodeVersion(Params.textDocument.version), WantDiags, Params.forceRebuild); } void ClangdLSPServer::onDocumentDidSave( const DidSaveTextDocumentParams &Params) { - reparseOpenFilesIfNeeded([](llvm::StringRef) { return true; }); + Server->reparseOpenFilesIfNeeded([](llvm::StringRef) { return true; }); } void ClangdLSPServer::onFileEvent(const DidChangeWatchedFilesParams &Params) { @@ -706,100 +695,94 @@ // - this is useful e.g. when switching git branches, but we're likely to see // fresh headers but still have the old-branch main-file content Server->onFileEvent(Params); + // FIXME: observe config files, immediately expire time-based caches, reparse: + // - compile_commands.json and compile_flags.txt + // - .clang_format and .clang-tidy + // - .clangd and clangd/config.yaml } void ClangdLSPServer::onCommand(const ExecuteCommandParams &Params, Callback<llvm::json::Value> Reply) { - auto ApplyEdit = [this](WorkspaceEdit WE, std::string SuccessMessage, - decltype(Reply) Reply) { - ApplyWorkspaceEditParams Edit; - Edit.edit = std::move(WE); - call<ApplyWorkspaceEditResponse>( - "workspace/applyEdit", std::move(Edit), - [Reply = std::move(Reply), SuccessMessage = std::move(SuccessMessage)]( - llvm::Expected<ApplyWorkspaceEditResponse> Response) mutable { - if (!Response) - return Reply(Response.takeError()); - if (!Response->applied) { - std::string Reason = Response->failureReason - ? *Response->failureReason - : "unknown reason"; - return Reply(llvm::createStringError( - llvm::inconvertibleErrorCode(), - ("edits were not applied: " + Reason).c_str())); - } - return Reply(SuccessMessage); - }); - }; - - if (Params.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND && - Params.workspaceEdit) { - // The flow for "apply-fix" : - // 1. We publish a diagnostic, including fixits - // 2. The user clicks on the diagnostic, the editor asks us for code actions - // 3. We send code actions, with the fixit embedded as context - // 4. The user selects the fixit, the editor asks us to apply it - // 5. We unwrap the changes and send them back to the editor - // 6. The editor applies the changes (applyEdit), and sends us a reply - // 7. We unwrap the reply and send a reply to the editor. - ApplyEdit(*Params.workspaceEdit, "Fix applied.", std::move(Reply)); - } else if (Params.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK && - Params.tweakArgs) { - auto Code = DraftMgr.getDraft(Params.tweakArgs->file.file()); - if (!Code) - return Reply(llvm::createStringError( - llvm::inconvertibleErrorCode(), - "trying to apply a code action for a non-added file")); - - auto Action = [this, ApplyEdit, Reply = std::move(Reply), - File = Params.tweakArgs->file, Code = std::move(*Code)]( - llvm::Expected<Tweak::Effect> R) mutable { - if (!R) - return Reply(R.takeError()); - - assert(R->ShowMessage || - (!R->ApplyEdits.empty() && "tweak has no effect")); - - if (R->ShowMessage) { - ShowMessageParams Msg; - Msg.message = *R->ShowMessage; - Msg.type = MessageType::Info; - notify("window/showMessage", Msg); - } - // When no edit is specified, make sure we Reply(). - if (R->ApplyEdits.empty()) - return Reply("Tweak applied."); - - if (auto Err = validateEdits(DraftMgr, R->ApplyEdits)) - return Reply(std::move(Err)); - - WorkspaceEdit WE; - WE.changes.emplace(); - for (const auto &It : R->ApplyEdits) { - (*WE.changes)[URI::createFile(It.first()).toString()] = - It.second.asTextEdits(); - } - // ApplyEdit will take care of calling Reply(). - return ApplyEdit(std::move(WE), "Tweak applied.", std::move(Reply)); - }; - Server->applyTweak(Params.tweakArgs->file.file(), - Params.tweakArgs->selection, Params.tweakArgs->tweakID, - std::move(Action)); - } else { - // We should not get here because ExecuteCommandParams would not have - // parsed in the first place and this handler should not be called. But if - // more commands are added, this will be here has a safe guard. - Reply(llvm::make_error<LSPError>( + auto It = Handlers.CommandHandlers.find(Params.command); + if (It == Handlers.CommandHandlers.end()) { + return Reply(llvm::make_error<LSPError>( llvm::formatv("Unsupported command \"{0}\".", Params.command).str(), ErrorCode::InvalidParams)); } + It->second(Params.argument, std::move(Reply)); +} + +void ClangdLSPServer::onCommandApplyEdit(const WorkspaceEdit &WE, + Callback<llvm::json::Value> Reply) { + // The flow for "apply-fix" : + // 1. We publish a diagnostic, including fixits + // 2. The user clicks on the diagnostic, the editor asks us for code actions + // 3. We send code actions, with the fixit embedded as context + // 4. The user selects the fixit, the editor asks us to apply it + // 5. We unwrap the changes and send them back to the editor + // 6. The editor applies the changes (applyEdit), and sends us a reply + // 7. We unwrap the reply and send a reply to the editor. + applyEdit(WE, "Fix applied.", std::move(Reply)); +} + +void ClangdLSPServer::onCommandApplyTweak(const TweakArgs &Args, + Callback<llvm::json::Value> Reply) { + auto Action = [this, Reply = std::move(Reply)]( + llvm::Expected<Tweak::Effect> R) mutable { + if (!R) + return Reply(R.takeError()); + + assert(R->ShowMessage || (!R->ApplyEdits.empty() && "tweak has no effect")); + + if (R->ShowMessage) { + ShowMessageParams Msg; + Msg.message = *R->ShowMessage; + Msg.type = MessageType::Info; + ShowMessage(Msg); + } + // When no edit is specified, make sure we Reply(). + if (R->ApplyEdits.empty()) + return Reply("Tweak applied."); + + if (auto Err = validateEdits(*Server, R->ApplyEdits)) + return Reply(std::move(Err)); + + WorkspaceEdit WE; + for (const auto &It : R->ApplyEdits) { + WE.changes[URI::createFile(It.first()).toString()] = + It.second.asTextEdits(); + } + // ApplyEdit will take care of calling Reply(). + return applyEdit(std::move(WE), "Tweak applied.", std::move(Reply)); + }; + Server->applyTweak(Args.file.file(), Args.selection, Args.tweakID, + std::move(Action)); +} + +void ClangdLSPServer::applyEdit(WorkspaceEdit WE, llvm::json::Value Success, + Callback<llvm::json::Value> Reply) { + ApplyWorkspaceEditParams Edit; + Edit.edit = std::move(WE); + ApplyWorkspaceEdit( + Edit, [Reply = std::move(Reply), SuccessMessage = std::move(Success)]( + llvm::Expected<ApplyWorkspaceEditResponse> Response) mutable { + if (!Response) + return Reply(Response.takeError()); + if (!Response->applied) { + std::string Reason = Response->failureReason + ? *Response->failureReason + : "unknown reason"; + return Reply(error("edits were not applied: {0}", Reason)); + } + return Reply(SuccessMessage); + }); } void ClangdLSPServer::onWorkspaceSymbol( const WorkspaceSymbolParams &Params, Callback<std::vector<SymbolInformation>> Reply) { Server->workspaceSymbols( - Params.query, CCOpts.Limit, + Params.query, Params.limit.getValueOr(Opts.CodeComplete.Limit), [Reply = std::move(Reply), this](llvm::Expected<std::vector<SymbolInformation>> Items) mutable { if (!Items) @@ -813,38 +796,41 @@ void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params, Callback<llvm::Optional<Range>> Reply) { - Server->prepareRename(Params.textDocument.uri.file(), Params.position, - RenameOpts, std::move(Reply)); + Server->prepareRename( + Params.textDocument.uri.file(), Params.position, /*NewName*/ llvm::None, + Opts.Rename, + [Reply = std::move(Reply)](llvm::Expected<RenameResult> Result) mutable { + if (!Result) + return Reply(Result.takeError()); + return Reply(std::move(Result->Target)); + }); } void ClangdLSPServer::onRename(const RenameParams &Params, Callback<WorkspaceEdit> Reply) { Path File = std::string(Params.textDocument.uri.file()); - if (!DraftMgr.getDraft(File)) + if (!Server->getDraft(File)) return Reply(llvm::make_error<LSPError>( "onRename called for non-added file", ErrorCode::InvalidParams)); - Server->rename( - File, Params.position, Params.newName, RenameOpts, - [File, Params, Reply = std::move(Reply), - this](llvm::Expected<FileEdits> Edits) mutable { - if (!Edits) - return Reply(Edits.takeError()); - if (auto Err = validateEdits(DraftMgr, *Edits)) - return Reply(std::move(Err)); - WorkspaceEdit Result; - Result.changes.emplace(); - for (const auto &Rep : *Edits) { - (*Result.changes)[URI::createFile(Rep.first()).toString()] = - Rep.second.asTextEdits(); - } - Reply(Result); - }); + Server->rename(File, Params.position, Params.newName, Opts.Rename, + [File, Params, Reply = std::move(Reply), + this](llvm::Expected<RenameResult> R) mutable { + if (!R) + return Reply(R.takeError()); + if (auto Err = validateEdits(*Server, R->GlobalChanges)) + return Reply(std::move(Err)); + WorkspaceEdit Result; + for (const auto &Rep : R->GlobalChanges) { + Result.changes[URI::createFile(Rep.first()).toString()] = + Rep.second.asTextEdits(); + } + Reply(Result); + }); } void ClangdLSPServer::onDocumentDidClose( const DidCloseTextDocumentParams &Params) { PathRef File = Params.textDocument.uri.file(); - DraftMgr.removeDraft(File); Server->removeDocument(File); { @@ -852,10 +838,6 @@ FixItsMap.erase(File); } { - std::lock_guard<std::mutex> HLock(HighlightingsMutex); - FileToHighlightings.erase(File); - } - { std::lock_guard<std::mutex> HLock(SemanticTokensMutex); LastSemanticTokens.erase(File); } @@ -866,55 +848,45 @@ // executed after it returns. PublishDiagnosticsParams Notification; Notification.uri = URIForFile::canonicalize(File, /*TUPath=*/File); - publishDiagnostics(Notification); + PublishDiagnostics(Notification); } void ClangdLSPServer::onDocumentOnTypeFormatting( const DocumentOnTypeFormattingParams &Params, Callback<std::vector<TextEdit>> Reply) { auto File = Params.textDocument.uri.file(); - auto Code = DraftMgr.getDraft(File); - if (!Code) - return Reply(llvm::make_error<LSPError>( - "onDocumentOnTypeFormatting called for non-added file", - ErrorCode::InvalidParams)); - - Reply(Server->formatOnType(Code->Contents, File, Params.position, Params.ch)); + Server->formatOnType(File, Params.position, Params.ch, std::move(Reply)); } void ClangdLSPServer::onDocumentRangeFormatting( const DocumentRangeFormattingParams &Params, Callback<std::vector<TextEdit>> Reply) { auto File = Params.textDocument.uri.file(); - auto Code = DraftMgr.getDraft(File); - if (!Code) - return Reply(llvm::make_error<LSPError>( - "onDocumentRangeFormatting called for non-added file", - ErrorCode::InvalidParams)); - - auto ReplacementsOrError = - Server->formatRange(Code->Contents, File, Params.range); - if (ReplacementsOrError) - Reply(replacementsToEdits(Code->Contents, ReplacementsOrError.get())); - else - Reply(ReplacementsOrError.takeError()); + auto Code = Server->getDraft(File); + Server->formatFile(File, Params.range, + [Code = std::move(Code), Reply = std::move(Reply)]( + llvm::Expected<tooling::Replacements> Result) mutable { + if (Result) + Reply(replacementsToEdits(*Code, Result.get())); + else + Reply(Result.takeError()); + }); } void ClangdLSPServer::onDocumentFormatting( const DocumentFormattingParams &Params, Callback<std::vector<TextEdit>> Reply) { auto File = Params.textDocument.uri.file(); - auto Code = DraftMgr.getDraft(File); - if (!Code) - return Reply(llvm::make_error<LSPError>( - "onDocumentFormatting called for non-added file", - ErrorCode::InvalidParams)); - - auto ReplacementsOrError = Server->formatFile(Code->Contents, File); - if (ReplacementsOrError) - Reply(replacementsToEdits(Code->Contents, ReplacementsOrError.get())); - else - Reply(ReplacementsOrError.takeError()); + auto Code = Server->getDraft(File); + Server->formatFile(File, + /*Rng=*/llvm::None, + [Code = std::move(Code), Reply = std::move(Reply)]( + llvm::Expected<tooling::Replacements> Result) mutable { + if (Result) + Reply(replacementsToEdits(*Code, Result.get())); + else + Reply(Result.takeError()); + }); } /// The functions constructs a flattened view of the DocumentSymbol hierarchy. @@ -922,7 +894,6 @@ static std::vector<SymbolInformation> flattenSymbolHierarchy(llvm::ArrayRef<DocumentSymbol> Symbols, const URIForFile &FileURI) { - std::vector<SymbolInformation> Results; std::function<void(const DocumentSymbol &, llvm::StringRef)> Process = [&](const DocumentSymbol &S, llvm::Optional<llvm::StringRef> ParentName) { @@ -961,6 +932,12 @@ }); } +void ClangdLSPServer::onFoldingRange( + const FoldingRangeParams &Params, + Callback<std::vector<FoldingRange>> Reply) { + Server->foldingRanges(Params.textDocument.uri.file(), std::move(Reply)); +} + static llvm::Optional<Command> asCommand(const CodeAction &Action) { Command Cmd; if (Action.command && Action.edit) @@ -968,8 +945,8 @@ if (Action.command) { Cmd = *Action.command; } else if (Action.edit) { - Cmd.command = std::string(Command::CLANGD_APPLY_FIX_COMMAND); - Cmd.workspaceEdit = *Action.edit; + Cmd.command = std::string(APPLY_FIX_COMMAND); + Cmd.argument = *Action.edit; } else { return None; } @@ -982,23 +959,30 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params, Callback<llvm::json::Value> Reply) { URIForFile File = Params.textDocument.uri; - auto Code = DraftMgr.getDraft(File.file()); - if (!Code) - return Reply(llvm::make_error<LSPError>( - "onCodeAction called for non-added file", ErrorCode::InvalidParams)); + // Checks whether a particular CodeActionKind is included in the response. + auto KindAllowed = [Only(Params.context.only)](llvm::StringRef Kind) { + if (Only.empty()) + return true; + return llvm::any_of(Only, [&](llvm::StringRef Base) { + return Kind.consume_front(Base) && (Kind.empty() || Kind.startswith(".")); + }); + }; + // We provide a code action for Fixes on the specified diagnostics. std::vector<CodeAction> FixIts; - for (const Diagnostic &D : Params.context.diagnostics) { - for (auto &F : getFixes(File.file(), D)) { - FixIts.push_back(toCodeAction(F, Params.textDocument.uri)); - FixIts.back().diagnostics = {D}; + if (KindAllowed(CodeAction::QUICKFIX_KIND)) { + for (const Diagnostic &D : Params.context.diagnostics) { + for (auto &F : getFixes(File.file(), D)) { + FixIts.push_back(toCodeAction(F, Params.textDocument.uri)); + FixIts.back().diagnostics = {D}; + } } } // Now enumerate the semantic code actions. auto ConsumeActions = - [Reply = std::move(Reply), File, Code = std::move(*Code), - Selection = Params.range, FixIts = std::move(FixIts), this]( + [Reply = std::move(Reply), File, Selection = Params.range, + FixIts = std::move(FixIts), this]( llvm::Expected<std::vector<ClangdServer::TweakRef>> Tweaks) mutable { if (!Tweaks) return Reply(Tweaks.takeError()); @@ -1008,6 +992,20 @@ for (const auto &T : *Tweaks) Actions.push_back(toCodeAction(T, File, Selection)); + // If there's exactly one quick-fix, call it "preferred". + // We never consider refactorings etc as preferred. + CodeAction *OnlyFix = nullptr; + for (auto &Action : Actions) { + if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) { + if (OnlyFix) { + OnlyFix->isPreferred = false; + break; + } + Action.isPreferred = true; + OnlyFix = &Action; + } + } + if (SupportsCodeAction) return Reply(llvm::json::Array(Actions)); std::vector<Command> Commands; @@ -1017,8 +1015,12 @@ } return Reply(llvm::json::Array(Commands)); }; - - Server->enumerateTweaks(File.file(), Params.range, std::move(ConsumeActions)); + Server->enumerateTweaks( + File.file(), Params.range, + [this, KindAllowed(std::move(KindAllowed))](const Tweak &T) { + return Opts.TweakFilter(T) && KindAllowed(T.kind()); + }, + std::move(ConsumeActions)); } void ClangdLSPServer::onCompletion(const CompletionParams &Params, @@ -1029,15 +1031,18 @@ vlog("ignored auto-triggered completion, preceding char did not match"); return Reply(CompletionList()); } - Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, - [Reply = std::move(Reply), + auto Opts = this->Opts.CodeComplete; + if (Params.limit && *Params.limit >= 0) + Opts.Limit = *Params.limit; + Server->codeComplete(Params.textDocument.uri.file(), Params.position, Opts, + [Reply = std::move(Reply), Opts, this](llvm::Expected<CodeCompleteResult> List) mutable { if (!List) return Reply(List.takeError()); CompletionList LSPList; LSPList.isIncomplete = List->HasMore; for (const auto &R : List->Completions) { - CompletionItem C = R.render(CCOpts); + CompletionItem C = R.render(Opts); C.kind = adjustKindToCapability( C.kind, SupportedCompletionItemKinds); LSPList.items.push_back(std::move(C)); @@ -1183,6 +1188,31 @@ std::move(Reply)); } +void ClangdLSPServer::onPrepareCallHierarchy( + const CallHierarchyPrepareParams &Params, + Callback<std::vector<CallHierarchyItem>> Reply) { + Server->prepareCallHierarchy(Params.textDocument.uri.file(), Params.position, + std::move(Reply)); +} + +void ClangdLSPServer::onCallHierarchyIncomingCalls( + const CallHierarchyIncomingCallsParams &Params, + Callback<std::vector<CallHierarchyIncomingCall>> Reply) { + Server->incomingCalls(Params.item, std::move(Reply)); +} + +void ClangdLSPServer::onCallHierarchyOutgoingCalls( + const CallHierarchyOutgoingCallsParams &Params, + Callback<std::vector<CallHierarchyOutgoingCall>> Reply) { + // FIXME: To be implemented. + Reply(std::vector<CallHierarchyOutgoingCall>{}); +} + +void ClangdLSPServer::onInlayHints(const InlayHintsParams &Params, + Callback<std::vector<InlayHint>> Reply) { + Server->inlayHints(Params.textDocument.uri.file(), std::move(Reply)); +} + void ClangdLSPServer::applyConfiguration( const ConfigurationSettings &Settings) { // Per-file update to the compilation database. @@ -1200,18 +1230,26 @@ } } - reparseOpenFilesIfNeeded( + Server->reparseOpenFilesIfNeeded( [&](llvm::StringRef File) { return ModifiedFiles.count(File) != 0; }); } -void ClangdLSPServer::publishTheiaSemanticHighlighting( - const TheiaSemanticHighlightingParams &Params) { - notify("textDocument/semanticHighlighting", Params); +void ClangdLSPServer::maybeExportMemoryProfile() { + if (!trace::enabled() || !ShouldProfile()) + return; + + static constexpr trace::Metric MemoryUsage( + "memory_usage", trace::Metric::Value, "component_name"); + trace::Span Tracer("ProfileBrief"); + MemoryTree MT; + profile(MT); + record(MT, "clangd_lsp_server", MemoryUsage); } -void ClangdLSPServer::publishDiagnostics( - const PublishDiagnosticsParams &Params) { - notify("textDocument/publishDiagnostics", Params); +void ClangdLSPServer::maybeCleanupMemory() { + if (!Opts.MemoryCleanup || !ShouldCleanupMemory()) + return; + Opts.MemoryCleanup(); } // FIXME: This function needs to be properly tested. @@ -1222,14 +1260,39 @@ void ClangdLSPServer::onReference(const ReferenceParams &Params, Callback<std::vector<Location>> Reply) { - Server->findReferences(Params.textDocument.uri.file(), Params.position, - CCOpts.Limit, - [Reply = std::move(Reply)]( - llvm::Expected<ReferencesResult> Refs) mutable { - if (!Refs) - return Reply(Refs.takeError()); - return Reply(std::move(Refs->References)); - }); + Server->findReferences( + Params.textDocument.uri.file(), Params.position, Opts.ReferencesLimit, + [Reply = std::move(Reply), + IncludeDecl(Params.context.includeDeclaration)]( + llvm::Expected<ReferencesResult> Refs) mutable { + if (!Refs) + return Reply(Refs.takeError()); + // Filter out declarations if the client asked. + std::vector<Location> Result; + Result.reserve(Refs->References.size()); + for (auto &Ref : Refs->References) { + bool IsDecl = Ref.Attributes & ReferencesResult::Declaration; + if (IncludeDecl || !IsDecl) + Result.push_back(std::move(Ref.Loc)); + } + return Reply(std::move(Result)); + }); +} + +void ClangdLSPServer::onGoToImplementation( + const TextDocumentPositionParams &Params, + Callback<std::vector<Location>> Reply) { + Server->findImplementations( + Params.textDocument.uri.file(), Params.position, + [Reply = std::move(Reply)]( + llvm::Expected<std::vector<LocatedSymbol>> Overrides) mutable { + if (!Overrides) + return Reply(Overrides.takeError()); + std::vector<Location> Impls; + for (const LocatedSymbol &Sym : *Overrides) + Impls.push_back(Sym.PreferredDeclaration); + return Reply(std::move(Impls)); + }); } void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params, @@ -1304,9 +1367,9 @@ }); } -void ClangdLSPServer::onSemanticTokensEdits( - const SemanticTokensEditsParams &Params, - Callback<SemanticTokensOrEdits> CB) { +void ClangdLSPServer::onSemanticTokensDelta( + const SemanticTokensDeltaParams &Params, + Callback<SemanticTokensOrDelta> CB) { Server->semanticHighlights( Params.textDocument.uri.file(), [this, PrevResultID(Params.previousResultId), @@ -1316,7 +1379,7 @@ return CB(HT.takeError()); std::vector<SemanticToken> Toks = toSemanticTokens(*HT); - SemanticTokensOrEdits Result; + SemanticTokensOrDelta Result; { std::lock_guard<std::mutex> Lock(SemanticTokensMutex); auto &Last = LastSemanticTokens[File]; @@ -1324,8 +1387,8 @@ if (PrevResultID == Last.resultId) { Result.edits = diffTokens(Last.tokens, Toks); } else { - vlog("semanticTokens/edits: wanted edits vs {0} but last result " - "had ID {1}. Returning full token list.", + vlog("semanticTokens/full/delta: wanted edits vs {0} but last " + "result had ID {1}. Returning full token list.", PrevResultID, Last.resultId); Result.tokens = Toks; } @@ -1339,56 +1402,97 @@ }); } -ClangdLSPServer::ClangdLSPServer( - class Transport &Transp, const FileSystemProvider &FSProvider, - const clangd::CodeCompleteOptions &CCOpts, - const clangd::RenameOptions &RenameOpts, - llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, - llvm::Optional<OffsetEncoding> ForcedOffsetEncoding, - const ClangdServer::Options &Opts) - : BackgroundContext(Context::current().clone()), Transp(Transp), - MsgHandler(new MessageHandler(*this)), FSProvider(FSProvider), - CCOpts(CCOpts), RenameOpts(RenameOpts), +void ClangdLSPServer::onMemoryUsage(const NoParams &, + Callback<MemoryTree> Reply) { + llvm::BumpPtrAllocator DetailAlloc; + MemoryTree MT(&DetailAlloc); + profile(MT); + Reply(std::move(MT)); +} + +void ClangdLSPServer::onAST(const ASTParams &Params, + Callback<llvm::Optional<ASTNode>> CB) { + Server->getAST(Params.textDocument.uri.file(), Params.range, std::move(CB)); +} + +ClangdLSPServer::ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS, + const ClangdLSPServer::Options &Opts) + : ShouldProfile(/*Period=*/std::chrono::minutes(5), + /*Delay=*/std::chrono::minutes(1)), + ShouldCleanupMemory(/*Period=*/std::chrono::minutes(1), + /*Delay=*/std::chrono::minutes(1)), + BackgroundContext(Context::current().clone()), Transp(Transp), + MsgHandler(new MessageHandler(*this)), TFS(TFS), SupportedSymbolKinds(defaultSymbolKinds()), - SupportedCompletionItemKinds(defaultCompletionItemKinds()), - UseDirBasedCDB(UseDirBasedCDB), - CompileCommandsDir(std::move(CompileCommandsDir)), ClangdServerOpts(Opts), - NegotiatedOffsetEncoding(ForcedOffsetEncoding) { + SupportedCompletionItemKinds(defaultCompletionItemKinds()), Opts(Opts) { + if (Opts.ConfigProvider) { + assert(!Opts.ContextProvider && + "Only one of ConfigProvider and ContextProvider allowed!"); + this->Opts.ContextProvider = ClangdServer::createConfiguredContextProvider( + Opts.ConfigProvider, this); + } + LSPBinder Bind(this->Handlers, *this); + Bind.method("initialize", this, &ClangdLSPServer::onInitialize); +} + +void ClangdLSPServer::bindMethods(LSPBinder &Bind, + const ClientCapabilities &Caps) { // clang-format off - MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize); - MsgHandler->bind("initialized", &ClangdLSPServer::onInitialized); - MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown); - MsgHandler->bind("sync", &ClangdLSPServer::onSync); - MsgHandler->bind("textDocument/rangeFormatting", &ClangdLSPServer::onDocumentRangeFormatting); - MsgHandler->bind("textDocument/onTypeFormatting", &ClangdLSPServer::onDocumentOnTypeFormatting); - MsgHandler->bind("textDocument/formatting", &ClangdLSPServer::onDocumentFormatting); - MsgHandler->bind("textDocument/codeAction", &ClangdLSPServer::onCodeAction); - MsgHandler->bind("textDocument/completion", &ClangdLSPServer::onCompletion); - MsgHandler->bind("textDocument/signatureHelp", &ClangdLSPServer::onSignatureHelp); - MsgHandler->bind("textDocument/definition", &ClangdLSPServer::onGoToDefinition); - MsgHandler->bind("textDocument/declaration", &ClangdLSPServer::onGoToDeclaration); - MsgHandler->bind("textDocument/references", &ClangdLSPServer::onReference); - MsgHandler->bind("textDocument/switchSourceHeader", &ClangdLSPServer::onSwitchSourceHeader); - MsgHandler->bind("textDocument/prepareRename", &ClangdLSPServer::onPrepareRename); - MsgHandler->bind("textDocument/rename", &ClangdLSPServer::onRename); - MsgHandler->bind("textDocument/hover", &ClangdLSPServer::onHover); - MsgHandler->bind("textDocument/documentSymbol", &ClangdLSPServer::onDocumentSymbol); - MsgHandler->bind("workspace/executeCommand", &ClangdLSPServer::onCommand); - MsgHandler->bind("textDocument/documentHighlight", &ClangdLSPServer::onDocumentHighlight); - MsgHandler->bind("workspace/symbol", &ClangdLSPServer::onWorkspaceSymbol); - MsgHandler->bind("textDocument/didOpen", &ClangdLSPServer::onDocumentDidOpen); - MsgHandler->bind("textDocument/didClose", &ClangdLSPServer::onDocumentDidClose); - MsgHandler->bind("textDocument/didChange", &ClangdLSPServer::onDocumentDidChange); - MsgHandler->bind("textDocument/didSave", &ClangdLSPServer::onDocumentDidSave); - MsgHandler->bind("workspace/didChangeWatchedFiles", &ClangdLSPServer::onFileEvent); - MsgHandler->bind("workspace/didChangeConfiguration", &ClangdLSPServer::onChangeConfiguration); - MsgHandler->bind("textDocument/symbolInfo", &ClangdLSPServer::onSymbolInfo); - MsgHandler->bind("textDocument/typeHierarchy", &ClangdLSPServer::onTypeHierarchy); - MsgHandler->bind("typeHierarchy/resolve", &ClangdLSPServer::onResolveTypeHierarchy); - MsgHandler->bind("textDocument/selectionRange", &ClangdLSPServer::onSelectionRange); - MsgHandler->bind("textDocument/documentLink", &ClangdLSPServer::onDocumentLink); - MsgHandler->bind("textDocument/semanticTokens", &ClangdLSPServer::onSemanticTokens); - MsgHandler->bind("textDocument/semanticTokens/edits", &ClangdLSPServer::onSemanticTokensEdits); + Bind.notification("initialized", this, &ClangdLSPServer::onInitialized); + Bind.method("shutdown", this, &ClangdLSPServer::onShutdown); + Bind.method("sync", this, &ClangdLSPServer::onSync); + Bind.method("textDocument/rangeFormatting", this, &ClangdLSPServer::onDocumentRangeFormatting); + Bind.method("textDocument/onTypeFormatting", this, &ClangdLSPServer::onDocumentOnTypeFormatting); + Bind.method("textDocument/formatting", this, &ClangdLSPServer::onDocumentFormatting); + Bind.method("textDocument/codeAction", this, &ClangdLSPServer::onCodeAction); + Bind.method("textDocument/completion", this, &ClangdLSPServer::onCompletion); + Bind.method("textDocument/signatureHelp", this, &ClangdLSPServer::onSignatureHelp); + Bind.method("textDocument/definition", this, &ClangdLSPServer::onGoToDefinition); + Bind.method("textDocument/declaration", this, &ClangdLSPServer::onGoToDeclaration); + Bind.method("textDocument/implementation", this, &ClangdLSPServer::onGoToImplementation); + Bind.method("textDocument/references", this, &ClangdLSPServer::onReference); + Bind.method("textDocument/switchSourceHeader", this, &ClangdLSPServer::onSwitchSourceHeader); + Bind.method("textDocument/prepareRename", this, &ClangdLSPServer::onPrepareRename); + Bind.method("textDocument/rename", this, &ClangdLSPServer::onRename); + Bind.method("textDocument/hover", this, &ClangdLSPServer::onHover); + Bind.method("textDocument/documentSymbol", this, &ClangdLSPServer::onDocumentSymbol); + Bind.method("workspace/executeCommand", this, &ClangdLSPServer::onCommand); + Bind.method("textDocument/documentHighlight", this, &ClangdLSPServer::onDocumentHighlight); + Bind.method("workspace/symbol", this, &ClangdLSPServer::onWorkspaceSymbol); + Bind.method("textDocument/ast", this, &ClangdLSPServer::onAST); + Bind.notification("textDocument/didOpen", this, &ClangdLSPServer::onDocumentDidOpen); + Bind.notification("textDocument/didClose", this, &ClangdLSPServer::onDocumentDidClose); + Bind.notification("textDocument/didChange", this, &ClangdLSPServer::onDocumentDidChange); + Bind.notification("textDocument/didSave", this, &ClangdLSPServer::onDocumentDidSave); + Bind.notification("workspace/didChangeWatchedFiles", this, &ClangdLSPServer::onFileEvent); + Bind.notification("workspace/didChangeConfiguration", this, &ClangdLSPServer::onChangeConfiguration); + Bind.method("textDocument/symbolInfo", this, &ClangdLSPServer::onSymbolInfo); + Bind.method("textDocument/typeHierarchy", this, &ClangdLSPServer::onTypeHierarchy); + Bind.method("typeHierarchy/resolve", this, &ClangdLSPServer::onResolveTypeHierarchy); + Bind.method("textDocument/prepareCallHierarchy", this, &ClangdLSPServer::onPrepareCallHierarchy); + Bind.method("callHierarchy/incomingCalls", this, &ClangdLSPServer::onCallHierarchyIncomingCalls); + Bind.method("callHierarchy/outgoingCalls", this, &ClangdLSPServer::onCallHierarchyOutgoingCalls); + Bind.method("textDocument/selectionRange", this, &ClangdLSPServer::onSelectionRange); + Bind.method("textDocument/documentLink", this, &ClangdLSPServer::onDocumentLink); + Bind.method("textDocument/semanticTokens/full", this, &ClangdLSPServer::onSemanticTokens); + Bind.method("textDocument/semanticTokens/full/delta", this, &ClangdLSPServer::onSemanticTokensDelta); + Bind.method("clangd/inlayHints", this, &ClangdLSPServer::onInlayHints); + Bind.method("$/memoryUsage", this, &ClangdLSPServer::onMemoryUsage); + if (Opts.FoldingRanges) + Bind.method("textDocument/foldingRange", this, &ClangdLSPServer::onFoldingRange); + Bind.command(APPLY_FIX_COMMAND, this, &ClangdLSPServer::onCommandApplyEdit); + Bind.command(APPLY_TWEAK_COMMAND, this, &ClangdLSPServer::onCommandApplyTweak); + + ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit"); + PublishDiagnostics = Bind.outgoingNotification("textDocument/publishDiagnostics"); + ShowMessage = Bind.outgoingNotification("window/showMessage"); + NotifyFileStatus = Bind.outgoingNotification("textDocument/clangd.fileStatus"); + CreateWorkDoneProgress = Bind.outgoingMethod("window/workDoneProgress/create"); + BeginWorkDoneProgress = Bind.outgoingNotification("$/progress"); + ReportWorkDoneProgress = Bind.outgoingNotification("$/progress"); + EndWorkDoneProgress = Bind.outgoingNotification("$/progress"); + if(Caps.SemanticTokenRefreshSupport) + SemanticTokensRefresh = Bind.outgoingMethod("workspace/semanticTokens/refresh"); // clang-format on } @@ -1410,6 +1514,11 @@ return CleanExit && ShutdownRequestReceived; } +void ClangdLSPServer::profile(MemoryTree &MT) const { + if (Server) + Server->profile(MT.child("clangd_server")); +} + std::vector<Fix> ClangdLSPServer::getFixes(llvm::StringRef File, const clangd::Diagnostic &D) { std::lock_guard<std::mutex> Lock(FixItsMutex); @@ -1435,38 +1544,17 @@ const CompletionParams &Params) const { if (Params.context.triggerKind != CompletionTriggerKind::TriggerCharacter) return true; - auto Code = DraftMgr.getDraft(Params.textDocument.uri.file()); + auto Code = Server->getDraft(Params.textDocument.uri.file()); if (!Code) return true; // completion code will log the error for untracked doc. - auto Offset = positionToOffset(Code->Contents, Params.position, + auto Offset = positionToOffset(*Code, Params.position, /*AllowColumnsBeyondLineLength=*/false); if (!Offset) { vlog("could not convert position '{0}' to offset for file '{1}'", Params.position, Params.textDocument.uri.file()); return true; } - return allowImplicitCompletion(Code->Contents, *Offset); -} - -void ClangdLSPServer::onHighlightingsReady( - PathRef File, llvm::StringRef Version, - std::vector<HighlightingToken> Highlightings) { - std::vector<HighlightingToken> Old; - std::vector<HighlightingToken> HighlightingsCopy = Highlightings; - { - std::lock_guard<std::mutex> Lock(HighlightingsMutex); - Old = std::move(FileToHighlightings[File]); - FileToHighlightings[File] = std::move(HighlightingsCopy); - } - // LSP allows us to send incremental edits of highlightings. Also need to diff - // to remove highlightings from tokens that should no longer have them. - std::vector<LineHighlightings> Diffed = diffHighlightings(Highlightings, Old); - TheiaSemanticHighlightingParams Notification; - Notification.TextDocument.uri = - URIForFile::canonicalize(File, /*TUPath=*/File); - Notification.TextDocument.version = decodeVersion(Version); - Notification.Lines = toTheiaSemanticHighlightingInformation(Diffed); - publishTheiaSemanticHighlighting(Notification); + return allowImplicitCompletion(*Code, *Offset); } void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version, @@ -1491,12 +1579,16 @@ } // Send a notification to the LSP client. - publishDiagnostics(Notification); + PublishDiagnostics(Notification); } void ClangdLSPServer::onBackgroundIndexProgress( const BackgroundQueue::Stats &Stats) { static const char ProgressToken[] = "backgroundIndexProgress"; + + // The background index did some work, maybe we need to cleanup + maybeCleanupMemory(); + std::lock_guard<std::mutex> Lock(BackgroundIndexProgressMutex); auto NotifyProgress = [this](const BackgroundQueue::Stats &Stats) { @@ -1504,22 +1596,22 @@ WorkDoneProgressBegin Begin; Begin.percentage = true; Begin.title = "indexing"; - progress(ProgressToken, std::move(Begin)); + BeginWorkDoneProgress({ProgressToken, std::move(Begin)}); BackgroundIndexProgressState = BackgroundIndexProgress::Live; } if (Stats.Completed < Stats.Enqueued) { assert(Stats.Enqueued > Stats.LastIdle); WorkDoneProgressReport Report; - Report.percentage = 100.0 * (Stats.Completed - Stats.LastIdle) / + Report.percentage = 100 * (Stats.Completed - Stats.LastIdle) / (Stats.Enqueued - Stats.LastIdle); Report.message = llvm::formatv("{0}/{1}", Stats.Completed - Stats.LastIdle, Stats.Enqueued - Stats.LastIdle); - progress(ProgressToken, std::move(Report)); + ReportWorkDoneProgress({ProgressToken, std::move(Report)}); } else { assert(Stats.Completed == Stats.Enqueued); - progress(ProgressToken, WorkDoneProgressEnd()); + EndWorkDoneProgress({ProgressToken, WorkDoneProgressEnd()}); BackgroundIndexProgressState = BackgroundIndexProgress::Empty; } }; @@ -1541,8 +1633,8 @@ BackgroundIndexProgressState = BackgroundIndexProgress::Creating; WorkDoneProgressCreateParams CreateRequest; CreateRequest.token = ProgressToken; - call<std::nullptr_t>( - "window/workDoneProgress/create", CreateRequest, + CreateWorkDoneProgress( + CreateRequest, [this, NotifyProgress](llvm::Expected<std::nullptr_t> E) { std::lock_guard<std::mutex> Lock(BackgroundIndexProgressMutex); if (E) { @@ -1573,19 +1665,17 @@ (Status.ASTActivity.K == ASTAction::Building || Status.ASTActivity.K == ASTAction::RunningAction)) return; - notify("textDocument/clangd.fileStatus", Status.render(File)); + NotifyFileStatus(Status.render(File)); } -void ClangdLSPServer::reparseOpenFilesIfNeeded( - llvm::function_ref<bool(llvm::StringRef File)> Filter) { - // Reparse only opened files that were modified. - for (const Path &FilePath : DraftMgr.getActiveFiles()) - if (Filter(FilePath)) - if (auto Draft = DraftMgr.getDraft(FilePath)) // else disappeared in race? - Server->addDocument(FilePath, std::move(Draft->Contents), - encodeVersion(Draft->Version), - WantDiagnostics::Auto); +void ClangdLSPServer::onSemanticsMaybeChanged(PathRef File) { + if (SemanticTokensRefresh) { + SemanticTokensRefresh(NoParams{}, [](llvm::Expected<std::nullptr_t> E) { + if (E) + return; + elog("Failed to refresh semantic tokens: {0}", E.takeError()); + }); + } } - } // namespace clangd } // namespace clang