10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/ADT/StringMap.h" 12 #include "llvm/Support/Error.h" 13 #include "llvm/Support/Registry.h" 17 LLVM_INSTANTIATE_REGISTRY(llvm::Registry<clang::clangd::Tweak>)
27 void validateRegistry() {
29 llvm::StringSet<> Seen;
30 for (
const auto &E : TweakRegistry::entries()) {
33 assert(E.instantiate()->id() == E.getName() &&
34 "id should be equal to class name");
35 assert(Seen.try_emplace(E.getName()).second &&
"duplicate check id");
43 : AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
44 ASTSelection(AST.getASTContext(), RangeBegin, RangeEnd) {
46 Code = SM.getBufferData(SM.getMainFileID());
47 Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
50 std::vector<std::unique_ptr<Tweak>>
52 llvm::function_ref<
bool(
const Tweak &)> Filter) {
55 std::vector<std::unique_ptr<Tweak>> Available;
56 for (
const auto &E : TweakRegistry::entries()) {
57 std::unique_ptr<Tweak> T = E.instantiate();
58 if (!Filter(*T) || !T->prepare(S))
60 Available.push_back(std::move(T));
64 [](
const std::unique_ptr<Tweak> &L,
65 const std::unique_ptr<Tweak> &R) {
return L->id() < R->id(); });
71 auto It = llvm::find_if(
72 TweakRegistry::entries(),
73 [ID](
const TweakRegistry::entry &E) {
return E.getName() == ID; });
74 if (It == TweakRegistry::end())
75 return llvm::createStringError(llvm::inconvertibleErrorCode(),
76 "id of the tweak is invalid");
77 std::unique_ptr<Tweak> T = It->instantiate();
79 return llvm::createStringError(llvm::inconvertibleErrorCode(),
80 "failed to prepare() a check");
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.
SourceLocation Cursor
A location of the cursor in the editor.
Input to prepare and apply tweaks.
Stores and provides access to parsed AST.
SourceManager & getSourceManager()
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
An interface base for small context-sensitive refactoring actions.
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...