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"
24LLVM_INSTANTIATE_REGISTRY(llvm::Registry<clang::clangd::Tweak>)
34void 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");
47std::vector<std::unique_ptr<Tweak>>
49 std::vector<std::unique_ptr<Tweak>>
All;
50 for (
const auto &
E : TweakRegistry::entries())
51 All.emplace_back(
E.instantiate());
53 for (
auto &
M : *Modules)
54 M.contributeTweaks(
All);
61 unsigned RangeBegin,
unsigned RangeEnd,
63 llvm::vfs::FileSystem *FS)
64 : Index(Index),
AST(&
AST), SelectionBegin(RangeBegin),
65 SelectionEnd(RangeEnd), ASTSelection(std::move(ASTSelection)), FS(FS) {
67 Code = SM.getBufferData(SM.getMainFileID());
68 Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
71std::vector<std::unique_ptr<Tweak>>
73 llvm::function_ref<
bool(
const Tweak &)> Filter,
77 std::vector<std::unique_ptr<Tweak>> Available;
78 for (
auto &T : getAllTweaks(Modules)) {
79 if (!Filter(*T) || !T->prepare(S))
81 Available.push_back(std::move(T));
85 [](
const std::unique_ptr<Tweak> &L,
86 const std::unique_ptr<Tweak> &R) {
return L->id() < R->id(); });
90llvm::Expected<std::unique_ptr<Tweak>>
93 for (
auto &T : getAllTweaks(Modules)) {
97 return error(
"failed to prepare() tweak {0}",
ID);
100 return error(
"tweak ID {0} is invalid",
ID);
103llvm::Expected<std::pair<Path, Edit>>
105 tooling::Replacements Replacements) {
106 Edit Ed(SM.getBufferData(FID), std::move(Replacements));
107 if (
const auto FE = SM.getFileEntryRefForID(FID))
109 return std::make_pair(*FilePath, std::move(Ed));
110 return error(
"Failed to get absolute path for edited file: {0}",
111 SM.getFileEntryRefForID(FID)->getName());
114llvm::Expected<Tweak::Effect>
116 tooling::Replacements Replacements) {
117 auto PathAndEdit = fileEdit(SM, SM.getMainFileID(), std::move(Replacements));
119 return PathAndEdit.takeError();
121 E.ApplyEdits.try_emplace(PathAndEdit->first, PathAndEdit->second);
const google::protobuf::Message & M
A FeatureModuleSet is a collection of feature modules installed in clangd.
Stores and provides access to parsed AST.
SourceManager & getSourceManager()
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
An interface base for small context-sensitive refactoring actions.
llvm::Registry< Tweak > TweakRegistry
A handy typedef to save some typing.
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S, const FeatureModuleSet *Modules)
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
std::optional< std::string > getCanonicalPath(const FileEntryRef F, FileManager &FileMgr)
Get the canonical path of F.
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.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A set of edits generated for a single file.
static llvm::Expected< Tweak::Effect > mainFileEdit(const SourceManager &SM, tooling::Replacements Replacements)
Creates an effect with an Edit for the main file.
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.
Input to prepare and apply tweaks.
SourceLocation Cursor
A location of the cursor in the editor.
Selection(const SymbolIndex *Index, ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd, SelectionTree ASTSelection, llvm::vfs::FileSystem *VFS)
ParsedAST * AST
The parsed active file. Never null. (Pointer so Selection is movable).
llvm::StringRef Code
The text of the active document.