[PATCH] D65210: [clangd] Fix the annotate tweak after rL366893

Haojian Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 01:47:49 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL366996: [clangd] Fix the annotate tweak after rL366893 (authored by hokein, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D65210?vs=211692&id=211694#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65210/new/

https://reviews.llvm.org/D65210

Files:
  clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp
  clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp


Index: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
@@ -487,9 +487,20 @@
 TEST(TweakTest, AnnotateHighlightings) {
   llvm::StringLiteral ID = "AnnotateHighlightings";
   checkAvailable(ID, "^vo^id^ ^f(^) {^}^"); // available everywhere.
+  checkAvailable(ID, "[[int a; int b;]]");
   const char *Input = "void ^f() {}";
   const char *Output = "void /* entity.name.function.cpp */f() {}";
   checkTransform(ID, Input, Output);
+
+  checkTransform(ID,
+  R"cpp(
+[[void f1();
+void f2();]]
+)cpp",
+  R"cpp(
+void /* entity.name.function.cpp */f1();
+void /* entity.name.function.cpp */f2();
+)cpp");
 }
 
 TEST(TweakTest, ExpandMacro) {
Index: clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp
@@ -23,33 +23,39 @@
 public:
   const char *id() const override final;
 
-  bool prepare(const Selection &Inputs) override {
-    for (auto N = Inputs.ASTSelection.commonAncestor(); N && !InterestedDecl;
-         N = N->Parent)
-      InterestedDecl = N->ASTNode.get<Decl>();
-    return InterestedDecl;
-  }
+  bool prepare(const Selection &Inputs) override { return true; }
   Expected<Effect> apply(const Selection &Inputs) override;
 
   std::string title() const override { return "Annotate highlighting tokens"; }
   Intent intent() const override { return Refactor; }
   bool hidden() const override { return true; }
-
-private:
-  const Decl *InterestedDecl = nullptr;
 };
 REGISTER_TWEAK(AnnotateHighlightings)
 
 Expected<Tweak::Effect> AnnotateHighlightings::apply(const Selection &Inputs) {
-  // Store the existing scopes.
-  const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
-  // Narrow the traversal scope to the selected node.
-  Inputs.AST.getASTContext().setTraversalScope(
-      {const_cast<Decl *>(InterestedDecl)});
-  auto HighlightingTokens = getSemanticHighlightings(Inputs.AST);
-  // Restore the traversal scope.
-  Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
-
+  // TUDecl is always the root ancestor.
+  const Decl *CommonDecl =
+      Inputs.ASTSelection.root().ASTNode.get<TranslationUnitDecl>();
+  for (auto N = Inputs.ASTSelection.commonAncestor(); N && !CommonDecl;
+       N = N->Parent)
+    CommonDecl = N->ASTNode.get<Decl>();
+
+  std::vector<HighlightingToken> HighlightingTokens;
+  if (llvm::isa<TranslationUnitDecl>(CommonDecl)) {
+    // We only annotate tokens in the main file, if CommonDecl is a TUDecl,
+    // we use the default traversal scope (which is the top level decls of the
+    // main file).
+    HighlightingTokens = getSemanticHighlightings(Inputs.AST);
+  } else {
+    // Store the existing scopes.
+    const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
+    // Narrow the traversal scope to the selected node.
+    Inputs.AST.getASTContext().setTraversalScope(
+        {const_cast<Decl *>(CommonDecl)});
+    HighlightingTokens = getSemanticHighlightings(Inputs.AST);
+    // Restore the traversal scope.
+    Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
+  }
   auto &SM = Inputs.AST.getSourceManager();
   tooling::Replacements Result;
   for (const auto &Token : HighlightingTokens) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65210.211694.patch
Type: text/x-patch
Size: 3577 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190725/8f843862/attachment.bin>


More information about the llvm-commits mailing list