10#include "llvm/ADT/StringRef.h"
11#include "llvm/Support/ScopedPrinter.h"
24class AnnotateHighlightings :
public Tweak {
26 const char *id() const final;
28 bool prepare(const Selection &Inputs)
override {
return true; }
29 Expected<Effect> apply(
const Selection &Inputs)
override;
31 std::string title()
const override {
return "Annotate highlighting tokens"; }
32 llvm::StringLiteral kind()
const override {
35 bool hidden()
const override {
return true; }
39Expected<Tweak::Effect> AnnotateHighlightings::apply(
const Selection &Inputs) {
40 const Decl *CommonDecl =
nullptr;
41 for (
auto *N = Inputs.ASTSelection.commonAncestor(); N && !CommonDecl;
43 CommonDecl = N->ASTNode.get<Decl>();
45 std::vector<HighlightingToken> HighlightingTokens;
54 const std::vector<Decl *> BackupScopes =
55 Inputs.AST->getASTContext().getTraversalScope();
57 Inputs.AST->getASTContext().setTraversalScope(
58 {
const_cast<Decl *
>(CommonDecl)});
62 Inputs.AST->getASTContext().setTraversalScope(BackupScopes);
64 auto &SM = Inputs.AST->getSourceManager();
65 tooling::Replacements Result;
66 llvm::StringRef FilePath = SM.getFilename(Inputs.Cursor);
67 for (
const auto &Token : HighlightingTokens) {
68 assert(Token.R.start.line == Token.R.end.line &&
69 "Token must be at the same line");
72 return InsertOffset.takeError();
74 std::string Comment =
"/* ";
75 Comment.append(llvm::to_string(Token.Kind));
77 I <= static_cast<unsigned>(HighlightingModifier::LastModifier); ++I) {
78 if (Token.Modifiers & (1 << I)) {
81 Comment.push_back(
']');
84 Comment.append(
" */");
85 auto InsertReplacement =
86 tooling::Replacement(FilePath, *InsertOffset, 0, Comment);
87 if (
auto Err = Result.add(InsertReplacement))
88 return std::move(Err);
90 return Effect::mainFileEdit(SM, std::move(Result));
#define REGISTER_TWEAK(Subclass)
An interface base for small context-sensitive refactoring actions.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens)
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++ -*-===//
static const llvm::StringLiteral REFACTOR_KIND