Skip to content

Commit 18fa729

Browse files
committed
[clangd] Fix the annotate tweak after rL366893
Summary: After rL366893, the annoate tweak is not activated when we select the whole file (the commonAncestor is TUDecl but we intend to return null). This patch fixes this, and also avoid traversing the TUDecl. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65210 llvm-svn: 366996
1 parent 985e52a commit 18fa729

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,39 @@ class AnnotateHighlightings : public Tweak {
2323
public:
2424
const char *id() const override final;
2525

26-
bool prepare(const Selection &Inputs) override {
27-
for (auto N = Inputs.ASTSelection.commonAncestor(); N && !InterestedDecl;
28-
N = N->Parent)
29-
InterestedDecl = N->ASTNode.get<Decl>();
30-
return InterestedDecl;
31-
}
26+
bool prepare(const Selection &Inputs) override { return true; }
3227
Expected<Effect> apply(const Selection &Inputs) override;
3328

3429
std::string title() const override { return "Annotate highlighting tokens"; }
3530
Intent intent() const override { return Refactor; }
3631
bool hidden() const override { return true; }
37-
38-
private:
39-
const Decl *InterestedDecl = nullptr;
4032
};
4133
REGISTER_TWEAK(AnnotateHighlightings)
4234

4335
Expected<Tweak::Effect> AnnotateHighlightings::apply(const Selection &Inputs) {
44-
// Store the existing scopes.
45-
const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
46-
// Narrow the traversal scope to the selected node.
47-
Inputs.AST.getASTContext().setTraversalScope(
48-
{const_cast<Decl *>(InterestedDecl)});
49-
auto HighlightingTokens = getSemanticHighlightings(Inputs.AST);
50-
// Restore the traversal scope.
51-
Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
36+
// TUDecl is always the root ancestor.
37+
const Decl *CommonDecl =
38+
Inputs.ASTSelection.root().ASTNode.get<TranslationUnitDecl>();
39+
for (auto N = Inputs.ASTSelection.commonAncestor(); N && !CommonDecl;
40+
N = N->Parent)
41+
CommonDecl = N->ASTNode.get<Decl>();
5242

43+
std::vector<HighlightingToken> HighlightingTokens;
44+
if (llvm::isa<TranslationUnitDecl>(CommonDecl)) {
45+
// We only annotate tokens in the main file, if CommonDecl is a TUDecl,
46+
// we use the default traversal scope (which is the top level decls of the
47+
// main file).
48+
HighlightingTokens = getSemanticHighlightings(Inputs.AST);
49+
} else {
50+
// Store the existing scopes.
51+
const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
52+
// Narrow the traversal scope to the selected node.
53+
Inputs.AST.getASTContext().setTraversalScope(
54+
{const_cast<Decl *>(CommonDecl)});
55+
HighlightingTokens = getSemanticHighlightings(Inputs.AST);
56+
// Restore the traversal scope.
57+
Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
58+
}
5359
auto &SM = Inputs.AST.getSourceManager();
5460
tooling::Replacements Result;
5561
for (const auto &Token : HighlightingTokens) {

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,20 @@ TEST(TweakTest, ExtractVariable) {
487487
TEST(TweakTest, AnnotateHighlightings) {
488488
llvm::StringLiteral ID = "AnnotateHighlightings";
489489
checkAvailable(ID, "^vo^id^ ^f(^) {^}^"); // available everywhere.
490+
checkAvailable(ID, "[[int a; int b;]]");
490491
const char *Input = "void ^f() {}";
491492
const char *Output = "void /* entity.name.function.cpp */f() {}";
492493
checkTransform(ID, Input, Output);
494+
495+
checkTransform(ID,
496+
R"cpp(
497+
[[void f1();
498+
void f2();]]
499+
)cpp",
500+
R"cpp(
501+
void /* entity.name.function.cpp */f1();
502+
void /* entity.name.function.cpp */f2();
503+
)cpp");
493504
}
494505

495506
TEST(TweakTest, ExpandMacro) {

0 commit comments

Comments
 (0)