Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions nixd/lib/Controller/CodeAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,100 @@ bool isValidNixIdentifier(const std::string &S) {
return Keywords.find(S) == Keywords.end();
}

/// \brief Check if an ExprAttrs can be flattened (no rec, inherit, dynamic).
/// Returns the Binds node if flattenable, nullptr otherwise.
const nixf::Binds *getFlattenableBinds(const nixf::ExprAttrs &Attrs) {
// Block if recursive attribute set
if (Attrs.isRecursive())
return nullptr;

const nixf::Binds *B = Attrs.binds();
if (!B || B->bindings().empty())
return nullptr;

// Check all bindings: must be plain Binding nodes (no Inherit)
// and all attribute names must be static (no dynamic ${} interpolation)
for (const auto &Child : B->bindings()) {
if (Child->kind() != nixf::Node::NK_Binding)
return nullptr; // Inherit node found

const auto &Bind = static_cast<const nixf::Binding &>(*Child);
for (const auto &Name : Bind.path().names()) {
if (!Name->isStatic())
return nullptr; // Dynamic attribute name
}
}

return B;
}

/// \brief Add flatten action for nested attribute sets.
/// Transforms: { foo = { bar = 1; }; } -> { foo.bar = 1; }
void addFlattenAttrsAction(const nixf::Node &N,
const nixf::ParentMapAnalysis &PM,
const std::string &FileURI, llvm::StringRef Src,
std::vector<CodeAction> &Actions) {
// Find if we're inside a Binding
const nixf::Node *BindingNode = PM.upTo(N, nixf::Node::NK_Binding);
if (!BindingNode)
return;

const auto &Bind = static_cast<const nixf::Binding &>(*BindingNode);

// Check if the binding's value is an ExprAttrs
if (!Bind.value() || Bind.value()->kind() != nixf::Node::NK_ExprAttrs)
return;

const auto &NestedAttrs = static_cast<const nixf::ExprAttrs &>(*Bind.value());

// Check if flattenable
const nixf::Binds *NestedBinds = getFlattenableBinds(NestedAttrs);
if (!NestedBinds)
return;

// Check outer path is static too
for (const auto &Name : Bind.path().names()) {
if (!Name->isStatic())
return;
}

// Build the flattened text
std::string NewText;
const auto &NestedBindings = NestedBinds->bindings();

// Pre-allocate to reduce reallocations. The +40 accounts for inner path,
// " = ", value text, and ";". May under-allocate for complex expressions
// but still reduces reallocations significantly.
const std::string_view OuterPath = Bind.path().src(Src);
size_t EstimatedSize = NestedBindings.size() * (OuterPath.size() + 40);
NewText.reserve(EstimatedSize);

for (size_t I = 0; I < NestedBindings.size(); ++I) {
const auto &InnerBind =
static_cast<const nixf::Binding &>(*NestedBindings[I]);

// Build path: outer.inner
const std::string_view InnerPath = InnerBind.path().src(Src);

NewText += OuterPath;
NewText += ".";
NewText += InnerPath;
NewText += " = ";

if (InnerBind.value()) {
NewText += InnerBind.value()->src(Src);
}
NewText += ";";

if (I + 1 < NestedBindings.size())
NewText += " ";
}

Actions.emplace_back(createSingleEditAction(
"Flatten nested attribute set", CodeAction::REFACTOR_REWRITE_KIND,
FileURI, toLSPRange(Src, Bind.range()), std::move(NewText)));
}

/// \brief Add refactoring code actions for attribute names (quote/unquote).
void addAttrNameActions(const nixf::Node &N, const nixf::ParentMapAnalysis &PM,
const std::string &FileURI, llvm::StringRef Src,
Expand Down Expand Up @@ -144,6 +238,8 @@ void Controller::onCodeAction(const lspserver::CodeActionParams &Params,
nixf::PositionRange NixfRange = toNixfRange(Range);
if (const nixf::Node *N = TU->ast()->descend(NixfRange)) {
addAttrNameActions(*N, *TU->parentMap(), FileURI, TU->src(), Actions);
addFlattenAttrsAction(*N, *TU->parentMap(), FileURI, TU->src(),
Actions);
}
}

Expand Down
93 changes: 93 additions & 0 deletions nixd/tools/nixd/test/code-action/flatten-attrs-deep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# RUN: nixd --lit-test < %s | FileCheck %s

Test that flatten action works with already-dotted outer paths.

<-- initialize(0)

```json
{
"jsonrpc":"2.0",
"id":0,
"method":"initialize",
"params":{
"processId":123,
"rootPath":"",
"capabilities":{
},
"trace":"off"
}
}
```


<-- textDocument/didOpen

```nix file:///flatten-attrs-deep.nix
{ a.b = { c = 1; }; }
```

<-- textDocument/codeAction(2)


```json
{
"jsonrpc":"2.0",
"id":2,
"method":"textDocument/codeAction",
"params":{
"textDocument":{
"uri":"file:///flatten-attrs-deep.nix"
},
"range":{
"start":{
"line": 0,
"character":2
},
"end":{
"line":0,
"character":5
}
},
"context":{
"diagnostics":[],
"triggerKind":2
}
}
}
```

No Quote action since "a.b" is a path, not a simple identifier.

```
CHECK: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": [
CHECK-NEXT: {
CHECK-NEXT: "edit": {
CHECK-NEXT: "changes": {
CHECK-NEXT: "file:///flatten-attrs-deep.nix": [
CHECK-NEXT: {
CHECK-NEXT: "newText": "a.b.c = 1;",
CHECK-NEXT: "range": {
CHECK-NEXT: "end": {
CHECK-NEXT: "character": 19,
CHECK-NEXT: "line": 0
CHECK-NEXT: },
CHECK-NEXT: "start": {
CHECK-NEXT: "character": 2,
CHECK-NEXT: "line": 0
CHECK-NEXT: }
CHECK-NEXT: }
CHECK-NEXT: }
CHECK-NEXT: ]
CHECK-NEXT: }
CHECK-NEXT: },
CHECK-NEXT: "kind": "refactor.rewrite",
CHECK-NEXT: "title": "Flatten nested attribute set"
CHECK-NEXT: }
CHECK-NEXT: ]
```

```json
{"jsonrpc":"2.0","method":"exit"}
```
69 changes: 69 additions & 0 deletions nixd/tools/nixd/test/code-action/flatten-attrs-dynamic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# RUN: nixd --lit-test < %s | FileCheck %s

Test that flatten action is NOT offered when nested attrset contains dynamic attribute names.

<-- initialize(0)

```json
{
"jsonrpc":"2.0",
"id":0,
"method":"initialize",
"params":{
"processId":123,
"rootPath":"",
"capabilities":{
},
"trace":"off"
}
}
```


<-- textDocument/didOpen

```nix file:///flatten-attrs-dynamic.nix
{ foo = { ${x} = 1; }; }
```

<-- textDocument/codeAction(2)


```json
{
"jsonrpc":"2.0",
"id":2,
"method":"textDocument/codeAction",
"params":{
"textDocument":{
"uri":"file:///flatten-attrs-dynamic.nix"
},
"range":{
"start":{
"line": 0,
"character":2
},
"end":{
"line":0,
"character":5
}
},
"context":{
"diagnostics":[],
"triggerKind":2
}
}
}
```

Dynamic attrsets should not offer flatten action.

```
CHECK: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NOT: "Flatten nested attribute set"
```

```json
{"jsonrpc":"2.0","method":"exit"}
```
69 changes: 69 additions & 0 deletions nixd/tools/nixd/test/code-action/flatten-attrs-empty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# RUN: nixd --lit-test < %s | FileCheck %s

Test that flatten action is NOT offered for empty nested attribute sets.

<-- initialize(0)

```json
{
"jsonrpc":"2.0",
"id":0,
"method":"initialize",
"params":{
"processId":123,
"rootPath":"",
"capabilities":{
},
"trace":"off"
}
}
```


<-- textDocument/didOpen

```nix file:///flatten-attrs-empty.nix
{ foo = { }; }
```

<-- textDocument/codeAction(2)


```json
{
"jsonrpc":"2.0",
"id":2,
"method":"textDocument/codeAction",
"params":{
"textDocument":{
"uri":"file:///flatten-attrs-empty.nix"
},
"range":{
"start":{
"line": 0,
"character":2
},
"end":{
"line":0,
"character":5
}
},
"context":{
"diagnostics":[],
"triggerKind":2
}
}
}
```

Empty attrsets should not offer flatten action.

```
CHECK: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NOT: "Flatten nested attribute set"
```

```json
{"jsonrpc":"2.0","method":"exit"}
```
Loading