Go to the documentation of this file.
19 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_ACTIONS_TWEAK_H
20 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_ACTIONS_TWEAK_H
28 #include "clang/Tooling/Core/Replacement.h"
29 #include "llvm/ADT/Optional.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Support/Error.h"
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;
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);
143 llvm::Expected<std::unique_ptr<Tweak>>
prepareTweak(StringRef TweakID,
144 const Tweak::Selection &S);
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.
Selection(const SymbolIndex *Index, ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd, SelectionTree ASTSelection)
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
unsigned SelectionBegin
The begin offset of the selection.
SelectionTree ASTSelection
The AST nodes that were selected.
virtual bool prepare(const Selection &Sel)=0
Run the first stage of the action.
Provide information to the user.
std::vector< std::unique_ptr< Tweak > > prepareTweaks(const Tweak::Selection &S, llvm::function_ref< bool(const Tweak &)> Filter)
Calls prepare() on all tweaks that satisfy the filter, returning those that can run on the selection.
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.
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.
Apply changes that preserve the behavior of the code.
llvm::Optional< std::string > ShowMessage
A message to be displayed to the user.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.
Stores and provides access to parsed AST.
virtual Intent intent() const =0
Describes what kind of action this is.