[clangd] Make the tweak filter a parameter to enumerateTweaks. NFC

(Required for CodeActionContext.only)

Differential Revision: https://reviews.llvm.org/D88724
This commit is contained in:
Sam McCall 2020-10-02 11:34:40 +02:00
parent 027e7a7721
commit 7530b254e9
4 changed files with 24 additions and 15 deletions

View File

@ -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,

View File

@ -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<bool(const Tweak &)> TweakFilter = [](const Tweak &T) {
return !T.hidden(); // only enable non-hidden tweaks.
};
};
ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS,

View File

@ -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<std::vector<TweakRef>> CB) {
void ClangdServer::enumerateTweaks(
PathRef File, Range Sel, llvm::unique_function<bool(const Tweak &)> Filter,
Callback<std::vector<TweakRef>> 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<InputsAndAST> InpAST) mutable {
Filter =
std::move(Filter)](Expected<InputsAndAST> 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<TweakRef> Res;
// Don't allow a tweak to fire more than once across ambiguous selections.
llvm::DenseSet<llvm::StringRef> 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());

View File

@ -163,11 +163,6 @@ public:
/// Enable preview of FoldingRanges feature.
bool FoldingRanges = false;
/// Returns true if the tweak should be enabled.
std::function<bool(const Tweak &)> 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<bool(const Tweak &)> Filter,
Callback<std::vector<TweakRef>> 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<bool(const Tweak &)> TweakFilter;
// GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
llvm::StringMap<llvm::Optional<FuzzyFindRequest>>
CachedCompletionFuzzyFindRequestByFile;