17const nixf::Binds *getFlattenableBinds(
const nixf::ExprAttrs &Attrs) {
22 const nixf::Binds *B = Attrs.
binds();
28 for (
const auto &Child : B->
bindings()) {
29 if (Child->kind() != nixf::Node::NK_Binding)
32 const auto &Bind =
static_cast<const nixf::Binding &
>(*Child);
33 for (
const auto &Name : Bind.path().names()) {
34 if (!Name->isStatic())
46 const std::string &FileURI, llvm::StringRef Src,
47 std::vector<lspserver::CodeAction> &Actions) {
49 const nixf::Node *BindingNode = PM.
upTo(N, nixf::Node::NK_Binding);
53 const auto &Bind =
static_cast<const nixf::Binding &
>(*BindingNode);
56 if (!Bind.value() || Bind.value()->kind() != nixf::Node::NK_ExprAttrs)
59 const auto &NestedAttrs =
static_cast<const nixf::ExprAttrs &
>(*Bind.value());
62 const nixf::Binds *NestedBinds = getFlattenableBinds(NestedAttrs);
67 for (
const auto &Name : Bind.path().names()) {
68 if (!Name->isStatic())
74 const auto &NestedBindings = NestedBinds->
bindings();
79 const std::string_view OuterPath = Bind.path().src(Src);
80 size_t EstimatedSize = NestedBindings.size() * (OuterPath.size() + 40);
81 NewText.reserve(EstimatedSize);
83 for (
size_t I = 0; I < NestedBindings.size(); ++I) {
84 const auto &InnerBind =
88 const std::string_view InnerPath = InnerBind.
path().
src(Src);
95 if (InnerBind.value()) {
96 NewText += InnerBind.value()->src(Src);
100 if (I + 1 < NestedBindings.size())
105 "Flatten nested attribute set",
107 toLSPRange(Src, Bind.range()), std::move(NewText)));
Convert between LSP and nixf types.
Code action for flattening nested attribute sets.
Shared utilities for code actions.
const AttrPath & path() const
const std::vector< std::shared_ptr< Node > > & bindings() const
const Binds * binds() const
std::string_view src(std::string_view Src) const
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
void addFlattenAttrsAction(const nixf::Node &N, const nixf::ParentMapAnalysis &PM, const std::string &FileURI, llvm::StringRef Src, std::vector< lspserver::CodeAction > &Actions)
Add flatten action for nested attribute sets.
lspserver::CodeAction createSingleEditAction(const std::string &Title, llvm::StringLiteral Kind, const std::string &FileURI, const lspserver::Range &EditRange, std::string NewText)
Create a CodeAction with a single text edit.
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
static const llvm::StringLiteral REFACTOR_REWRITE_KIND