Skip to content
Closed
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
1 change: 1 addition & 0 deletions nixd/lib/Controller/CodeAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void Controller::onCodeAction(const lspserver::CodeActionParams &Params,
bool IsPreferred = false;
switch (D.kind()) {
case nixf::Diagnostic::DK_UnusedDefLet:
case nixf::Diagnostic::DK_ExtraRecursive:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are also #786, #787, #788 PRs, they all just add isPreferred = true for some diagnostic ID, why those code actions from diagnostic fix() cannot be preferred by default?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also these PRs are conflict to #778?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, You're right — these PRs all add the same pattern and they conflict with #778.

#778 introduces Fix::prefer() so each fix declares itself as preferred at creation time, which removes this switch statement.

I'll close #785#788, get #778 merged first, then open a single follow-up PR that adds .prefer() to the remaining diagnostic fixes (extra-rec, extra-with, empty-inherit, redundant-paren) together with their tests.

IsPreferred = true;
break;
default:
Expand Down
78 changes: 78 additions & 0 deletions nixd/tools/nixd/test/code-action/extra-rec/basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# RUN: nixd --lit-test < %s | FileCheck %s

Test basic `remove extra rec` action for unnecessary recursive attribute set.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///extra-rec.nix
rec { x = 1; }
```

<-- textDocument/codeAction(2)


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

The action should remove the `rec` keyword since no binding references another.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 3,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 0
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove `rec` keyword"
```

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

Test `remove extra rec` action for empty recursive attribute set.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///empty-rec.nix
rec { }
```

<-- textDocument/codeAction(2)


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

The action should remove the `rec` keyword since the attrset has no bindings at all.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 3,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 0
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove `rec` keyword"
```

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

Test `remove extra rec` action with multiline attribute set.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///extra-rec-multiline.nix
rec {
x = 1;
y = 2;
}
```

<-- textDocument/codeAction(2)


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

The action should remove the `rec` keyword since neither `x` nor `y` references the other.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 3,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 0
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove `rec` keyword"
```

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

Test that `remove extra rec` is NOT offered when `rec` is actually needed.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///needed-rec.nix
rec { x = 1; y = x; }
```

<-- textDocument/codeAction(2)


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

No code action should be offered because `y` depends on `x`, making `rec` necessary.

```
CHECK: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": []
```

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