-
-
Notifications
You must be signed in to change notification settings - Fork 71
feat(code-action): add quickfix to remove extra rec keyword #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"} | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 = truefor some diagnostic ID, why those code actions from diagnosticfix()cannot bepreferredby default?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.