10 #include "llvm/ADT/StringRef.h"
23 class AnnotateHighlightings :
public Tweak {
25 const char *id() const override final;
27 bool prepare(const Selection &
Inputs)
override {
return true; }
28 Expected<Effect> apply(
const Selection &
Inputs)
override;
30 std::string title()
const override {
return "Annotate highlighting tokens"; }
31 Intent intent()
const override {
return Refactor; }
32 bool hidden()
const override {
return true; }
36 Expected<Tweak::Effect> AnnotateHighlightings::apply(
const Selection &
Inputs) {
37 const Decl *CommonDecl =
nullptr;
38 for (
auto N =
Inputs.ASTSelection.commonAncestor(); N && !CommonDecl;
40 CommonDecl = N->ASTNode.get<
Decl>();
42 std::vector<HighlightingToken> HighlightingTokens;
50 const auto &BackupScopes =
Inputs.AST->getASTContext().getTraversalScope();
52 Inputs.AST->getASTContext().setTraversalScope(
53 {const_cast<Decl *>(CommonDecl)});
56 Inputs.AST->getASTContext().setTraversalScope(BackupScopes);
58 auto &SM =
Inputs.AST->getSourceManager();
59 tooling::Replacements Result;
60 llvm::StringRef FilePath = SM.getFilename(
Inputs.Cursor);
61 for (
const auto &Token : HighlightingTokens) {
62 assert(Token.R.start.line == Token.R.end.line &&
63 "Token must be at the same line");
66 return InsertOffset.takeError();
68 auto InsertReplacement = tooling::Replacement(
69 FilePath, *InsertOffset, 0,
71 if (
auto Err = Result.add(InsertReplacement))
72 return std::move(Err);
74 return Effect::mainFileEdit(SM, std::move(Result));