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 auto &BackupScopes = Inputs.AST->getASTContext().getTraversalScope();
56 Inputs.AST->getASTContext().setTraversalScope(
57 {
const_cast<Decl *
>(CommonDecl)});
61 Inputs.AST->getASTContext().setTraversalScope(BackupScopes);
63 auto &SM = Inputs.AST->getSourceManager();
64 tooling::Replacements Result;
65 llvm::StringRef FilePath = SM.getFilename(Inputs.Cursor);
66 for (
const auto &Token : HighlightingTokens) {
67 assert(Token.R.start.line == Token.R.end.line &&
68 "Token must be at the same line");
71 return InsertOffset.takeError();
73 std::string Comment =
"/* ";
74 Comment.append(llvm::to_string(Token.Kind));
77 if (Token.Modifiers & (1 << I)) {
80 Comment.push_back(
']');
83 Comment.append(
" */");
84 auto InsertReplacement =
85 tooling::Replacement(FilePath, *InsertOffset, 0, Comment);
86 if (
auto Err = Result.add(InsertReplacement))
87 return std::move(Err);
89 return Effect::mainFileEdit(SM, std::move(Result));
const FunctionDecl * Decl
#define REGISTER_TWEAK(Subclass)
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