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_RedundantParen:
IsPreferred = true;
break;
default:
Expand Down
86 changes: 86 additions & 0 deletions nixd/tools/nixd/test/code-action/redundant-paren/basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# RUN: nixd --lit-test < %s | FileCheck %s

Test basic `remove redundant parens` action for unnecessary parentheses.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///redundant-paren.nix
(1)
```

<-- textDocument/codeAction(2)


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

The action should remove both `(` and `)` since the inner expression `1` is simple.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 1,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 0
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 3,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 2,
CHECK: "line": 0
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove ( and )"
```

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

Test `remove redundant parens` 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-paren.nix
(
1
)
```

<-- textDocument/codeAction(2)


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

The action should remove `(` on line 0 and `)` on line 2.

```
CHECK: "id": 2,
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 1,
CHECK: "line": 0
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 0
CHECK: "newText": "",
CHECK: "range":
CHECK: "end":
CHECK: "character": 1,
CHECK: "line": 2
CHECK: "start":
CHECK: "character": 0,
CHECK: "line": 2
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove ( and )"
```

```json
{"jsonrpc":"2.0","method":"exit"}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# RUN: nixd --lit-test < %s | FileCheck %s

Test that `remove redundant parens` is NOT offered when parentheses are needed.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///needed-paren.nix
(1 + 2)
```

<-- textDocument/codeAction(2)


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

No code action should be offered because `1 + 2` is a binary operation, making parentheses meaningful.

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

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

Test `remove redundant parens` action for nested parentheses `((1))`.

<-- initialize(0)

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


<-- textDocument/didOpen

```nix file:///nested-paren.nix
((1))
```

<-- textDocument/codeAction(2)


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

Both inner and outer parentheses are redundant, so two quickfix actions should be offered.

```
CHECK: "id": 2,
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove ( and )"
CHECK: "isPreferred": true,
CHECK: "kind": "quickfix",
CHECK: "title": "remove ( and )"
```

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