diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -50,6 +50,10 @@ /// per-request, but LSP allows limited/no customizations. clangd::CodeCompleteOptions CodeComplete; clangd::RenameOptions Rename; + /// Returns true if the tweak should be enabled. + std::function TweakFilter = [](const Tweak &T) { + return !T.hidden(); // only enable non-hidden tweaks. + }; }; ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS, diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -1030,7 +1030,15 @@ return Reply(llvm::json::Array(Commands)); }; - Server->enumerateTweaks(File.file(), Params.range, std::move(ConsumeActions)); + Server->enumerateTweaks( + File.file(), Params.range, + [&](const Tweak &T) { + if (!Opts.TweakFilter(T)) + return false; + // FIXME: also consider CodeActionContext.only + return true; + }, + std::move(ConsumeActions)); } void ClangdLSPServer::onCompletion(const CompletionParams &Params, diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -163,11 +163,6 @@ /// Enable preview of FoldingRanges feature. bool FoldingRanges = false; - /// Returns true if the tweak should be enabled. - std::function TweakFilter = [](const Tweak &T) { - return !T.hidden(); // only enable non-hidden tweaks. - }; - explicit operator TUScheduler::Options() const; }; // Sensible default options for use in tests. @@ -294,7 +289,9 @@ llvm::StringLiteral Kind; }; /// Enumerate the code tweaks available to the user at a specified point. + /// Tweaks where Filter returns false will not be checked or included. void enumerateTweaks(PathRef File, Range Sel, + llvm::unique_function Filter, Callback> CB); /// Apply the code tweak with a specified \p ID. @@ -382,8 +379,6 @@ // If true, preserve the type for recovery AST. bool PreserveRecoveryASTType = false; - std::function TweakFilter; - // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex) llvm::StringMap> CachedCompletionFuzzyFindRequestByFile; diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -180,7 +180,7 @@ SuggestMissingIncludes(Opts.SuggestMissingIncludes), BuildRecoveryAST(Opts.BuildRecoveryAST), PreserveRecoveryASTType(Opts.PreserveRecoveryASTType), - TweakFilter(Opts.TweakFilter), WorkspaceRoot(Opts.WorkspaceRoot), + WorkspaceRoot(Opts.WorkspaceRoot), // Pass a callback into `WorkScheduler` to extract symbols from a newly // parsed file and rebuild the file index synchronously each time an AST // is parsed. @@ -492,13 +492,15 @@ return std::move(Result); } -void ClangdServer::enumerateTweaks(PathRef File, Range Sel, - Callback> CB) { +void ClangdServer::enumerateTweaks( + PathRef File, Range Sel, llvm::unique_function Filter, + Callback> CB) { // Tracks number of times a tweak has been offered. static constexpr trace::Metric TweakAvailable( "tweak_available", trace::Metric::Counter, "tweak_id"); auto Action = [File = File.str(), Sel, CB = std::move(CB), - this](Expected InpAST) mutable { + Filter = + std::move(Filter)](Expected InpAST) mutable { if (!InpAST) return CB(InpAST.takeError()); auto Selections = tweakSelection(Sel, *InpAST); @@ -507,11 +509,11 @@ std::vector Res; // Don't allow a tweak to fire more than once across ambiguous selections. llvm::DenseSet PreparedTweaks; - auto Filter = [&](const Tweak &T) { - return TweakFilter(T) && !PreparedTweaks.count(T.id()); + auto DeduplicatingFilter = [&](const Tweak &T) { + return Filter(T) && !PreparedTweaks.count(T.id()); }; for (const auto &Sel : *Selections) { - for (auto &T : prepareTweaks(*Sel, Filter)) { + for (auto &T : prepareTweaks(*Sel, DeduplicatingFilter)) { Res.push_back({T->id(), T->title(), T->kind()}); PreparedTweaks.insert(T->id()); TweakAvailable.record(1, T->id());