summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2019-07-12 08:50:20 +0000
committerHaojian Wu <hokein@google.com>2019-07-12 08:50:20 +0000
commit5b2d5adfde094ee332af3c3e970155971c6b1dae (patch)
tree0036c9dd3a1ee90a635fae0dfd4acb89eaa595e0
parent71efad8ae561d453560ed0b9e132fc42b3bf270e (diff)
[clangd] Don't run the prepare for tweaks that are disabled.
Summary: Previously, we ran the prepare, even for the tweaks that are disabled. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64565 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@365882 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--clangd/ClangdServer.cpp8
-rw-r--r--clangd/ClangdServer.h13
-rw-r--r--clangd/refactor/Tweak.cpp6
-rw-r--r--clangd/refactor/Tweak.h8
-rw-r--r--clangd/tool/ClangdMain.cpp14
5 files changed, 25 insertions, 24 deletions
diff --git a/clangd/ClangdServer.cpp b/clangd/ClangdServer.cpp
index 0c1ca98d..142c62c9 100644
--- a/clangd/ClangdServer.cpp
+++ b/clangd/ClangdServer.cpp
@@ -101,7 +101,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
: 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 @@ void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
if (!Selection)
return CB(Selection.takeError());
std::vector<TweakRef> 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/clangd/ClangdServer.h b/clangd/ClangdServer.h
index f0c675dc..fa6783b1 100644
--- a/clangd/ClangdServer.h
+++ b/clangd/ClangdServer.h
@@ -126,10 +126,6 @@ public:
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<std::string> QueryDriverGlobs;
@@ -137,9 +133,10 @@ public:
/// Enable semantic highlighting features.
bool SemanticHighlighting = false;
- /// Returns true if the StringRef is a tweak that should be enabled
- std::function<bool(llvm::StringRef)> TweakFilter =
- [](llvm::StringRef TweakToSearch) { return true; };
+ /// 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.
+ };
};
// Sensible default options for use in tests.
// Features like indexing must be enabled if desired.
@@ -322,7 +319,7 @@ private:
bool SuggestMissingIncludes = false;
bool EnableHiddenFeatures = false;
- std::function<bool(llvm::StringRef)> TweakFilter;
+ std::function<bool(const Tweak &)> TweakFilter;
// GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
llvm::StringMap<llvm::Optional<FuzzyFindRequest>>
diff --git a/clangd/refactor/Tweak.cpp b/clangd/refactor/Tweak.cpp
index 6a19751e..a6d70dbd 100644
--- a/clangd/refactor/Tweak.cpp
+++ b/clangd/refactor/Tweak.cpp
@@ -46,13 +46,15 @@ Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
}
-std::vector<std::unique_ptr<Tweak>> prepareTweaks(const Tweak::Selection &S) {
+std::vector<std::unique_ptr<Tweak>>
+prepareTweaks(const Tweak::Selection &S,
+ llvm::function_ref<bool(const Tweak &)> Filter) {
validateRegistry();
std::vector<std::unique_ptr<Tweak>> Available;
for (const auto &E : TweakRegistry::entries()) {
std::unique_ptr<Tweak> T = E.instantiate();
- if (!T->prepare(S))
+ if (!Filter(*T) || !T->prepare(S))
continue;
Available.push_back(std::move(T));
}
diff --git a/clangd/refactor/Tweak.h b/clangd/refactor/Tweak.h
index 883ad7c3..79e0f3ec 100644
--- a/clangd/refactor/Tweak.h
+++ b/clangd/refactor/Tweak.h
@@ -110,9 +110,11 @@ public:
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<std::unique_ptr<Tweak>> prepareTweaks(const Tweak::Selection &S);
+/// Calls prepare() on all tweaks that satisfy the filter, returning those that
+/// can run on the selection.
+std::vector<std::unique_ptr<Tweak>>
+prepareTweaks(const Tweak::Selection &S,
+ llvm::function_ref<bool(const Tweak &)> Filter);
// Calls prepare() on the tweak with a given ID.
// If prepare() returns false, returns an error.
diff --git a/clangd/tool/ClangdMain.cpp b/clangd/tool/ClangdMain.cpp
index d3195bd0..5db3d5c2 100644
--- a/clangd/tool/ClangdMain.cpp
+++ b/clangd/tool/ClangdMain.cpp
@@ -472,7 +472,6 @@ int main(int argc, char *argv[]) {
}
Opts.StaticIndex = StaticIdx.get();
Opts.AsyncThreadsCount = WorkerThreadsCount;
- Opts.HiddenFeatures = HiddenFeatures;
clangd::CodeCompleteOptions CCOpts;
CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
@@ -531,11 +530,14 @@ int main(int argc, char *argv[]) {
}
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<OffsetEncoding> OffsetEncodingFromFlag;
if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding)
OffsetEncodingFromFlag = ForceOffsetEncoding;