Go to the documentation of this file.
19 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_TWEAK_H
20 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_TWEAK_H
27 #include "clang/Tooling/Core/Replacement.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Support/Error.h"
36 class FeatureModuleSet;
52 llvm::vfs::FileSystem *VFS);
70 llvm::vfs::FileSystem *
FS =
nullptr;
84 E.ShowMessage = std::string(S);
91 static llvm::Expected<std::pair<Path, Edit>>
92 fileEdit(
const SourceManager &SM, FileID FID,
93 tooling::Replacements Replacements);
97 static llvm::Expected<Tweak::Effect>
98 mainFileEdit(
const SourceManager &SM, tooling::Replacements Replacements);
101 virtual ~Tweak() =
default;
105 virtual const char *
id()
const = 0;
112 virtual bool prepare(
const Selection &Sel) = 0;
115 virtual Expected<Effect>
apply(
const Selection &Sel) = 0;
120 virtual std::string
title()
const = 0;
123 virtual llvm::StringLiteral
kind()
const = 0;
125 virtual bool hidden()
const {
return false; }
129 #define REGISTER_TWEAK(Subclass) \
130 ::llvm::Registry<::clang::clangd::Tweak>::Add<Subclass> \
131 TweakRegistrationFor##Subclass(#Subclass, ""); \
132 const char *Subclass::id() const { return #Subclass; }
136 std::vector<std::unique_ptr<Tweak>>
138 llvm::function_ref<
bool(
const Tweak &)> Filter,
139 const FeatureModuleSet *Modules);
144 llvm::Expected<std::unique_ptr<Tweak>>
146 const FeatureModuleSet *Modules);
virtual std::string title() const =0
A one-line title of the action that should be shown to the users in the UI.
virtual Expected< Effect > apply(const Selection &Sel)=0
Run the second stage of the action that would produce the actual effect.
static Effect showMessage(StringRef S)
virtual bool hidden() const
Is this a 'hidden' tweak, which are off by default.
SourceLocation Cursor
A location of the cursor in the editor.
ParsedAST * AST
The parsed active file. Never null. (Pointer so Selection is movable).
llvm::StringRef Code
The text of the active document.
unsigned SelectionBegin
The begin offset of the selection.
SelectionTree ASTSelection
The AST nodes that were selected.
std::vector< std::unique_ptr< Tweak > > prepareTweaks(const Tweak::Selection &S, llvm::function_ref< bool(const Tweak &)> Filter, const FeatureModuleSet *Modules)
Calls prepare() on all tweaks that satisfy the filter, returning those that can run on the selection.
virtual bool prepare(const Selection &Sel)=0
Run the first stage of the action.
std::optional< std::string > ShowMessage
A message to be displayed to the user.
unsigned SelectionEnd
The end offset of the selection.
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
static llvm::Expected< std::pair< Path, Edit > > fileEdit(const SourceManager &SM, FileID FID, tooling::Replacements Replacements)
Path is the absolute, symlink-resolved path for the file pointed by FID in SM.
virtual const char * id() const =0
A unique id of the action, it is always equal to the name of the class defining the Tweak.
bool FormatEdits
Whether the edits should be formatted before presenting to the client.
const SymbolIndex * Index
The Index for handling codebase related queries.
static llvm::Expected< Tweak::Effect > mainFileEdit(const SourceManager &SM, tooling::Replacements Replacements)
Creates an effect with an Edit for the main file.
Input to prepare and apply tweaks.
Selection(const SymbolIndex *Index, ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd, SelectionTree ASTSelection, llvm::vfs::FileSystem *VFS)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S, const FeatureModuleSet *Modules)
An interface base for small context-sensitive refactoring actions.
llvm::StringMap< Edit > FileEdits
A mapping from absolute file path (the one used for accessing the underlying VFS) to edits.
llvm::vfs::FileSystem * FS
File system used to access source code (for cross-file tweaks).
Stores and provides access to parsed AST.
virtual llvm::StringLiteral kind() const =0
Describes what kind of action this is.