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 @@ -126,10 +126,6 @@ bool SuggestMissingIncludes = false; - /// Enable hidden features mostly useful to clangd developers. - /// e.g. tweaks to dump the AST. - bool HiddenFeatures = false; - /// Clangd will execute compiler drivers matching one of these globs to /// fetch system include path. std::vector QueryDriverGlobs; @@ -137,9 +133,10 @@ /// Enable semantic highlighting features. bool SemanticHighlighting = false; - /// Returns true if the StringRef is a tweak that should be enabled - std::function TweakFilter = - [](llvm::StringRef TweakToSearch) { return true; }; + /// Returns true if the tweak should be enabled. + std::function TweakFilter = [](const Tweak &T) { + return !T.hidden(); // only enable non-hidden tweaks. + }; }; // Sensible default options for use in tests. // Features like indexing must be enabled if desired. @@ -322,7 +319,7 @@ bool SuggestMissingIncludes = false; bool EnableHiddenFeatures = false; - std::function TweakFilter; + std::function TweakFilter; // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex) llvm::StringMap> 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 @@ -101,7 +101,7 @@ : nullptr), GetClangTidyOptions(Opts.GetClangTidyOptions), SuggestMissingIncludes(Opts.SuggestMissingIncludes), - EnableHiddenFeatures(Opts.HiddenFeatures), TweakFilter(Opts.TweakFilter), + TweakFilter(Opts.TweakFilter), 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 @@ -333,11 +333,9 @@ if (!Selection) return CB(Selection.takeError()); std::vector Res; - for (auto &T : prepareTweaks(*Selection)) { - if (!TweakFilter(T->id()) || (T->hidden() && !EnableHiddenFeatures)) - continue; + for (auto &T : prepareTweaks(*Selection, TweakFilter)) Res.push_back({T->id(), T->title(), T->intent()}); - } + CB(std::move(Res)); }; diff --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h --- a/clang-tools-extra/clangd/refactor/Tweak.h +++ b/clang-tools-extra/clangd/refactor/Tweak.h @@ -110,9 +110,11 @@ TweakRegistrationFor##Subclass(#Subclass, /*Description=*/""); \ const char *Subclass::id() const { return #Subclass; } -/// Calls prepare() on all tweaks, returning those that can run on the -/// selection. -std::vector> prepareTweaks(const Tweak::Selection &S); +/// Calls prepare() on all tweaks that satisfy the filter, returning those that +/// can run on the selection. +std::vector> +prepareTweaks(const Tweak::Selection &S, + llvm::function_ref Filter); // Calls prepare() on the tweak with a given ID. // If prepare() returns false, returns an error. diff --git a/clang-tools-extra/clangd/refactor/Tweak.cpp b/clang-tools-extra/clangd/refactor/Tweak.cpp --- a/clang-tools-extra/clangd/refactor/Tweak.cpp +++ b/clang-tools-extra/clangd/refactor/Tweak.cpp @@ -46,13 +46,15 @@ Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin); } -std::vector> prepareTweaks(const Tweak::Selection &S) { +std::vector> +prepareTweaks(const Tweak::Selection &S, + llvm::function_ref Filter) { validateRegistry(); std::vector> Available; for (const auto &E : TweakRegistry::entries()) { std::unique_ptr T = E.instantiate(); - if (!T->prepare(S)) + if (!Filter(*T) || !T->prepare(S)) continue; Available.push_back(std::move(T)); } diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -472,7 +472,6 @@ } Opts.StaticIndex = StaticIdx.get(); Opts.AsyncThreadsCount = WorkerThreadsCount; - Opts.HiddenFeatures = HiddenFeatures; clangd::CodeCompleteOptions CCOpts; CCOpts.IncludeIneligibleResults = IncludeIneligibleResults; @@ -531,11 +530,14 @@ } Opts.SuggestMissingIncludes = SuggestMissingIncludes; Opts.QueryDriverGlobs = std::move(QueryDriverGlobs); - if (TweakList.getNumOccurrences()) - Opts.TweakFilter = [&](llvm::StringRef TweakToSearch) { - // return true if any tweak matches the TweakToSearch - return llvm::find(TweakList, TweakToSearch) != TweakList.end(); - }; + + Opts.TweakFilter = [&](const Tweak &T) { + if (T.hidden() && !HiddenFeatures) + return false; + if (TweakList.getNumOccurrences()) + return llvm::is_contained(TweakList, T.id()); + return true; + }; llvm::Optional OffsetEncodingFromFlag; if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding) OffsetEncodingFromFlag = ForceOffsetEncoding;