22 class AnnotateHighlightings :
public Tweak {
24 const char *id() const override final;
26 bool prepare(const Selection &Inputs)
override {
27 for (
auto N = Inputs.ASTSelection.commonAncestor(); N && !InterestedDecl;
29 InterestedDecl = N->ASTNode.get<Decl>();
30 return InterestedDecl;
32 Expected<Effect> apply(
const Selection &Inputs)
override;
34 std::string title()
const override {
return "Annotate highlighting tokens"; }
35 Intent intent()
const override {
return Refactor; }
36 bool hidden()
const override {
return true; }
39 const Decl *InterestedDecl =
nullptr;
43 Expected<Tweak::Effect> AnnotateHighlightings::apply(
const Selection &Inputs) {
45 const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
47 Inputs.AST.getASTContext().setTraversalScope(
48 {
const_cast<Decl *
>(InterestedDecl)});
51 Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
53 auto &SM = Inputs.AST.getSourceManager();
54 tooling::Replacements
Result;
55 for (
const auto &Token : HighlightingTokens) {
56 assert(Token.R.start.line == Token.R.end.line &&
57 "Token must be at the same line");
60 return InsertOffset.takeError();
62 auto InsertReplacement = tooling::Replacement(
63 SM.getFileEntryForID(SM.getMainFileID())->getName(), *InsertOffset, 0,
65 if (
auto Err = Result.add(InsertReplacement))
66 return std::move(Err);
68 return Effect::applyEdit(Result);
llvm::StringRef toTextMateScope(HighlightingKind Kind)
Converts a HighlightingKind to a corresponding TextMate scope (https://manual.macromates.com/en/language_grammars).
#define REGISTER_TWEAK(Subclass)
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)