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_ExtraWith:
IsPreferred = true;
break;
default:
Expand Down
78 changes: 78 additions & 0 deletions nixd/tools/nixd/test/code-action/extra-with/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 with` action for unused with expression.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///extra-with.nix
with {}; 1
```

<-- textDocument/codeAction(2)


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

The action should remove `with {};` leaving just `1`.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 4,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 0
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove `with` expression"
```

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

Test `remove extra with` action with multiline expression.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///multiline-with.nix
with lib;
1 + 2
```

<-- textDocument/codeAction(2)


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

The action should remove the `with lib;` part.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "line": 0
CHECK: "start":
CHECK: "line": 0
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove `with` expression"
```

```json
{"jsonrpc":"2.0","method":"exit"}
```
69 changes: 69 additions & 0 deletions nixd/tools/nixd/test/code-action/extra-with/needed-with.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 with` action is NOT offered when with is actually used.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///needed-with.nix
with lib; foo
```

<-- textDocument/codeAction(2)


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

No "remove `with` expression" action should be offered because `foo` is
resolved through the `with lib` scope.

```
CHECK: "id": 2,
CHECK-NOT: "remove `with` expression"
```

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