From 7530b254e93a18991a86d145eff066fbe88d6164 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 2 Oct 2020 11:34:40 +0200 Subject: [PATCH] [clangd] Make the tweak filter a parameter to enumerateTweaks. NFC (Required for CodeActionContext.only) Differential Revision: https://reviews.llvm.org/D88724 --- clang-tools-extra/clangd/ClangdLSPServer.cpp | 10 +++++++++- clang-tools-extra/clangd/ClangdLSPServer.h | 4 ++++ clang-tools-extra/clangd/ClangdServer.cpp | 16 +++++++++------- clang-tools-extra/clangd/ClangdServer.h | 9 ++------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index e5ea4ccc6b8c..9ed635c88e71 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -1030,7 +1030,15 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params, 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/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h index e8823d37c55d..a853a4087156 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -50,6 +50,10 @@ public: /// 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/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index 68afa49514a9..93e3b10b50d5 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -180,7 +180,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB, 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 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST) { 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 @@ void ClangdServer::enumerateTweaks(PathRef File, Range Sel, 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()); diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h index 7322b71e57ce..efba7ace6489 100644 --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -163,11 +163,6 @@ public: /// 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 @@ public: 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 @@ private: // If true, preserve the type for recovery AST. bool PreserveRecoveryASTType = false; - std::function TweakFilter; - // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex) llvm::StringMap> CachedCompletionFuzzyFindRequestByFile;