48 const std::string &FileURI, llvm::StringRef Src,
49 std::vector<lspserver::CodeAction> &Actions) {
51 const nixf::Node *BindingNode = PM.
upTo(N, nixf::Node::NK_Binding);
55 const auto &Bind =
static_cast<const nixf::Binding &
>(*BindingNode);
58 if (!Bind.value() || Bind.value()->kind() != nixf::Node::NK_ExprAttrs)
61 const auto &NestedAttrs =
static_cast<const nixf::ExprAttrs &
>(*Bind.value());
64 const nixf::Binds *NestedBinds = getFlattenableBinds(NestedAttrs);
69 for (
const auto &Name : Bind.path().names()) {
70 if (!Name->isStatic())
77 return B.eq() && B.value() &&
79 B.
src(Src).back() ==
';';
81 if (!CompleteBinding(Bind))
84 const size_t Open = NestedAttrs.lCur().offset();
85 const size_t Close = NestedAttrs.rCur().offset() - 1;
86 if (Src[Open] !=
'{' || Src[Close] !=
'}')
89 for (
const auto &Child : NestedBinds->
bindings()) {
90 const auto &Inner =
static_cast<const nixf::Binding &
>(*Child);
91 if (!CompleteBinding(Inner) || Inner.rCur().offset() > Close)
98 for (
const auto &Name : Bind.path().names()) {
99 Prefix += Name->src(Src);
104 const auto AppendSource = [&](
size_t Begin,
size_t End) {
105 NewText += Src.substr(Begin, End - Begin);
109 auto PathTokens = Bind.path().children();
110 std::sort(PathTokens.begin(), PathTokens.end(),
112 return L->lCur().offset() < R->lCur().offset();
114 size_t Pos = Bind.lCur().offset();
115 for (
const auto *Token : PathTokens) {
116 AppendSource(Pos, Token->lCur().offset());
117 Pos = Token->rCur().offset();
119 AppendSource(Pos, Bind.eq()->lCur().offset());
120 AppendSource(Bind.eq()->rCur().offset(), Open);
125 for (
const auto &Child : NestedBinds->
bindings()) {
126 AppendSource(Pos, Child->lCur().offset());
128 Pos = Child->lCur().offset();
130 AppendSource(Pos, Close);
131 AppendSource(Close + 1, Bind.rCur().offset() - 1);
134 "Flatten nested attribute set",
136 toLSPRange(Src, Bind.range()), std::move(NewText)));
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.