[PATCH] D64565: [clangd] Don't run the prepare for tweaks that are disabled.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 11 06:49:25 PDT 2019


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added subscribers: kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.

Previously, we ran the prepare, even for the tweaks that are disabled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64565

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h


Index: clang-tools-extra/clangd/refactor/Tweak.h
===================================================================
--- clang-tools-extra/clangd/refactor/Tweak.h
+++ 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<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,
+              std::function<bool(const Tweak &)> Filter);
 
 // Calls prepare() on the tweak with a given ID.
 // If prepare() returns false, returns an error.
Index: clang-tools-extra/clangd/refactor/Tweak.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Tweak.cpp
+++ clang-tools-extra/clangd/refactor/Tweak.cpp
@@ -46,13 +46,15 @@
   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,
+              std::function<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));
   }
Index: clang-tools-extra/clangd/ClangdServer.h
===================================================================
--- clang-tools-extra/clangd/ClangdServer.h
+++ clang-tools-extra/clangd/ClangdServer.h
@@ -137,8 +137,9 @@
     /// 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 StringRef is a tweak ID that should be enabled.
+    std::function<bool(llvm::StringRef)> TweakFilter =
+        [](llvm::StringRef TweakToSearch) { return true; };
   };
   // Sensible default options for use in tests.
   // Features like indexing must be enabled if desired.
Index: clang-tools-extra/clangd/ClangdServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdServer.cpp
+++ clang-tools-extra/clangd/ClangdServer.cpp
@@ -334,11 +334,11 @@
     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, [this](const Tweak &T) {
+           return TweakFilter(T.id()) && (!T.hidden() || EnableHiddenFeatures);
+         }))
       Res.push_back({T->id(), T->title(), T->intent()});
-    }
+
     CB(std::move(Res));
   };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64565.209207.patch
Type: text/x-patch
Size: 3249 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190711/6824d24e/attachment.bin>


More information about the cfe-commits mailing list