12 #include "llvm/ADT/None.h" 13 #include "llvm/ADT/Optional.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/StringMap.h" 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/Support/Error.h" 18 #include "llvm/Support/Registry.h" 23 LLVM_INSTANTIATE_REGISTRY(llvm::Registry<clang::clangd::Tweak>)
33 void validateRegistry() {
35 llvm::StringSet<> Seen;
36 for (
const auto &E : TweakRegistry::entries()) {
39 assert(E.instantiate()->id() == E.getName() &&
40 "id should be equal to class name");
41 assert(Seen.try_emplace(E.getName()).second &&
"duplicate check id");
49 : AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
50 ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) {
52 Code = SM.getBufferData(SM.getMainFileID());
53 Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
56 std::vector<std::unique_ptr<Tweak>>
58 llvm::function_ref<
bool(
const Tweak &)> Filter) {
61 std::vector<std::unique_ptr<Tweak>> Available;
62 for (
const auto &E : TweakRegistry::entries()) {
63 std::unique_ptr<Tweak> T = E.instantiate();
64 if (!Filter(*T) || !T->prepare(S))
66 Available.push_back(std::move(T));
70 [](
const std::unique_ptr<Tweak> &L,
71 const std::unique_ptr<Tweak> &R) {
return L->id() < R->id(); });
77 auto It = llvm::find_if(
78 TweakRegistry::entries(),
79 [ID](
const TweakRegistry::entry &E) {
return E.getName() == ID; });
80 if (It == TweakRegistry::end())
81 return llvm::createStringError(llvm::inconvertibleErrorCode(),
82 "id of the tweak is invalid");
83 std::unique_ptr<Tweak> T = It->instantiate();
85 return llvm::createStringError(llvm::inconvertibleErrorCode(),
86 "failed to prepare() a check");
90 llvm::Expected<std::pair<Path, Edit>>
92 tooling::Replacements Replacements) {
93 Edit Ed(SM.getBufferData(FID), std::move(Replacements));
95 return std::make_pair(*FilePath, std::move(Ed));
96 return llvm::createStringError(
97 llvm::inconvertibleErrorCode(),
98 "Failed to get absolute path for edited file: " +
99 SM.getFileEntryForID(FID)->getName());
102 llvm::Expected<Tweak::Effect>
104 tooling::Replacements Replacements) {
105 auto PathAndEdit = fileEdit(SM, SM.getMainFileID(), std::move(Replacements));
107 return PathAndEdit.takeError();
109 E.
ApplyEdits.try_emplace(PathAndEdit->first, PathAndEdit->second);
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
llvm::StringRef Code
The text of the active document.
Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd)
llvm::Registry< Tweak > TweakRegistry
A handy typedef to save some typing.
llvm::StringMap< Edit > ApplyEdits
A mapping from file path(the one used for accessing the underlying VFS) to edits. ...
SourceLocation Cursor
A location of the cursor in the editor.
Input to prepare and apply tweaks.
static llvm::Expected< Tweak::Effect > mainFileEdit(const SourceManager &SM, tooling::Replacements Replacements)
Creates an effect with an Edit for the main file.
Stores and provides access to parsed AST.
SourceManager & getSourceManager()
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.
llvm::Optional< std::string > getCanonicalPath(const FileEntry *F, const SourceManager &SourceMgr)
Get the canonical path of F.
An interface base for small context-sensitive refactoring actions.
A set of edits generated for a single file.
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...