13 #include "llvm/ADT/None.h" 14 #include "llvm/ADT/Optional.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/StringMap.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Error.h" 19 #include "llvm/Support/Registry.h" 24 LLVM_INSTANTIATE_REGISTRY(llvm::Registry<clang::clangd::Tweak>)
34 void validateRegistry() {
36 llvm::StringSet<> Seen;
37 for (
const auto &
E : TweakRegistry::entries()) {
40 assert(
E.instantiate()->id() ==
E.getName() &&
41 "id should be equal to class name");
42 assert(Seen.try_emplace(
E.getName()).second &&
"duplicate check id");
49 unsigned RangeBegin,
unsigned RangeEnd)
50 : Index(Index), AST(&AST), SelectionBegin(RangeBegin),
51 SelectionEnd(RangeEnd),
52 ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) {
54 Code = SM.getBufferData(SM.getMainFileID());
55 Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
58 std::vector<std::unique_ptr<Tweak>>
60 llvm::function_ref<
bool(
const Tweak &)> Filter) {
63 std::vector<std::unique_ptr<Tweak>> Available;
64 for (
const auto &
E : TweakRegistry::entries()) {
65 std::unique_ptr<Tweak> T =
E.instantiate();
66 if (!Filter(*T) || !T->prepare(S))
68 Available.push_back(std::move(T));
72 [](
const std::unique_ptr<Tweak> &L,
73 const std::unique_ptr<Tweak> &R) {
return L->id() < R->id(); });
79 auto It = llvm::find_if(
80 TweakRegistry::entries(),
81 [ID](
const TweakRegistry::entry &
E) {
return E.getName() == ID; });
82 if (It == TweakRegistry::end())
83 return llvm::createStringError(llvm::inconvertibleErrorCode(),
84 "id of the tweak is invalid");
85 std::unique_ptr<Tweak> T = It->instantiate();
87 return llvm::createStringError(llvm::inconvertibleErrorCode(),
88 "failed to prepare() a check");
92 llvm::Expected<std::pair<Path, Edit>>
94 tooling::Replacements Replacements) {
95 Edit Ed(SM.getBufferData(FID), std::move(Replacements));
97 return std::make_pair(*FilePath, std::move(Ed));
98 return llvm::createStringError(
99 llvm::inconvertibleErrorCode(),
100 "Failed to get absolute path for edited file: " +
101 SM.getFileEntryForID(FID)->getName());
104 llvm::Expected<Tweak::Effect>
106 tooling::Replacements Replacements) {
107 auto PathAndEdit = fileEdit(SM, SM.getMainFileID(), std::move(Replacements));
109 return PathAndEdit.takeError();
111 E.
ApplyEdits.try_emplace(PathAndEdit->first, PathAndEdit->second);
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
llvm::StringRef Code
The text of the active document.
llvm::Registry< Tweak > TweakRegistry
A handy typedef to save some typing.
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.
Selection(const SymbolIndex *Index, ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd)
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...
const SymbolIndex * Index