-
-
Notifications
You must be signed in to change notification settings - Fork 71
feat(code-action): add remove unused formal parameter quickfix #778
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
Open
takeokunn
wants to merge
2
commits into
main
Choose a base branch
from
feature/add-code-action-12
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
69 changes: 69 additions & 0 deletions
69
nixd/tools/nixd/test/code-action/remove-unused-formal/all-used-negative.md
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 no action is offered when all formals are used. | ||
|
|
||
| <-- initialize(0) | ||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc":"2.0", | ||
| "id":0, | ||
| "method":"initialize", | ||
| "params":{ | ||
| "processId":123, | ||
| "rootPath":"", | ||
| "capabilities":{ | ||
| }, | ||
| "trace":"off" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| <-- textDocument/didOpen | ||
|
|
||
| ```nix file:///all-used.nix | ||
| {x, y}: x + y | ||
| ``` | ||
|
|
||
| <-- textDocument/codeAction(2) | ||
|
|
||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc":"2.0", | ||
| "id":2, | ||
| "method":"textDocument/codeAction", | ||
| "params":{ | ||
| "textDocument":{ | ||
| "uri":"file:///all-used.nix" | ||
| }, | ||
| "range":{ | ||
| "start":{ | ||
| "line": 0, | ||
| "character": 4 | ||
| }, | ||
| "end":{ | ||
| "line":0, | ||
| "character": 5 | ||
| } | ||
| }, | ||
| "context":{ | ||
| "diagnostics":[], | ||
| "triggerKind":2 | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| No remove unused formal action should be offered since all formals are used. | ||
|
|
||
| ``` | ||
| CHECK: "id": 2, | ||
| CHECK-NEXT: "jsonrpc": "2.0", | ||
| CHECK-NEXT: "result": [] | ||
| ``` | ||
|
|
||
| ```json | ||
| {"jsonrpc":"2.0","method":"exit"} | ||
| ``` |
70 changes: 70 additions & 0 deletions
70
nixd/tools/nixd/test/code-action/remove-unused-formal/basic.md
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,70 @@ | ||
| # RUN: nixd --lit-test < %s | FileCheck %s | ||
|
|
||
| Test basic `remove unused formal` action. | ||
|
|
||
| <-- initialize(0) | ||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc":"2.0", | ||
| "id":0, | ||
| "method":"initialize", | ||
| "params":{ | ||
| "processId":123, | ||
| "rootPath":"", | ||
| "capabilities":{ | ||
| }, | ||
| "trace":"off" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| <-- textDocument/didOpen | ||
|
|
||
| ```nix file:///basic.nix | ||
| {x, y}: x | ||
| ``` | ||
|
|
||
| <-- textDocument/codeAction(2) | ||
|
|
||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc":"2.0", | ||
| "id":2, | ||
| "method":"textDocument/codeAction", | ||
| "params":{ | ||
| "textDocument":{ | ||
| "uri":"file:///basic.nix" | ||
| }, | ||
| "range":{ | ||
| "start":{ | ||
| "line": 0, | ||
| "character": 4 | ||
| }, | ||
| "end":{ | ||
| "line":0, | ||
| "character": 5 | ||
| } | ||
| }, | ||
| "context":{ | ||
| "diagnostics":[], | ||
| "triggerKind":2 | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The action should remove `, y` from the formals since `y` is unused. | ||
|
|
||
| ``` | ||
| CHECK: "id": 2, | ||
| CHECK: "isPreferred": true, | ||
| CHECK: "kind": "quickfix", | ||
| CHECK: "title": "remove unused formal `y`" | ||
| ``` | ||
|
|
||
| ```json | ||
| {"jsonrpc":"2.0","method":"exit"} | ||
| ``` |
70 changes: 70 additions & 0 deletions
70
nixd/tools/nixd/test/code-action/remove-unused-formal/first-formal.md
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,70 @@ | ||
| # RUN: nixd --lit-test < %s | FileCheck %s | ||
|
|
||
| Test removing the first formal (special case for comma handling). | ||
|
|
||
| <-- initialize(0) | ||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc":"2.0", | ||
| "id":0, | ||
| "method":"initialize", | ||
| "params":{ | ||
| "processId":123, | ||
| "rootPath":"", | ||
| "capabilities":{ | ||
| }, | ||
| "trace":"off" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| <-- textDocument/didOpen | ||
|
|
||
| ```nix file:///first-formal.nix | ||
| {x, y}: y | ||
| ``` | ||
|
|
||
| <-- textDocument/codeAction(2) | ||
|
|
||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc":"2.0", | ||
| "id":2, | ||
| "method":"textDocument/codeAction", | ||
| "params":{ | ||
| "textDocument":{ | ||
| "uri":"file:///first-formal.nix" | ||
| }, | ||
| "range":{ | ||
| "start":{ | ||
| "line": 0, | ||
| "character": 1 | ||
| }, | ||
| "end":{ | ||
| "line":0, | ||
| "character": 2 | ||
| } | ||
| }, | ||
| "context":{ | ||
| "diagnostics":[], | ||
| "triggerKind":2 | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The action should remove `x, ` from the formals since `x` is unused. | ||
|
|
||
| ``` | ||
| CHECK: "id": 2, | ||
| CHECK: "isPreferred": true, | ||
| CHECK: "kind": "quickfix", | ||
| CHECK: "title": "remove unused formal `x`" | ||
| ``` | ||
|
|
||
| ```json | ||
| {"jsonrpc":"2.0","method":"exit"} | ||
| ``` |
Oops, something went wrong.
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.
Since
libnixfstores fixes in astd::vectorand most diagnostics currently yield only a single solution, adding anis_preferredboolean field makes sense primarily as a tie-breaker for multi-fix scenarios. I agree with this addition for LSP alignment (CodeAction.isPreferred), but we should carefully define its interaction with the existing diagnostic structure—specifically how to handle cases where multiple fixes (or none) are marked as preferred to ensure consistent client behavior.One design detail to consider: if a diagnostic provides only a single
fix(), should we mark it asis_preferredby default? In my view, we should be cautious—isPreferredin LSP usually signals that the fix can be safely auto-applied (e.g., via "Fix All"). If a fix has side effects, it might be better to keep itfalseeven if it's the only option. What are your thoughts on the default behavior for single-fix scenarios?libnixf では現在、fix を std::vector で管理しており、ほとんどの Diagnostic が単一の解決策しか提供していないことを考えると、is_preferred という bool フィールドの導入は、主に複数の候補が存在する場合の優先順位付けとして機能すると理解しています。LSP の仕様(CodeAction.isPreferred)に準拠させるという点では賛成ですが、既存の Diagnostic 構造との論理的な関係については慎重に定義すべきだと考えています。具体的には、複数の fix が preferred とマークされている場合、あるいは一つもマークされていない場合に、クライアント側で一貫した挙動を保証するための設計が必要です。
設計上の詳細について一点確認させてください。ある Diagnostic に対して fix() が一つしかない場合、それをデフォルトで is_preferred とすべきでしょうか?個人的には、少し慎重になる必要があると考えています。LSP における isPreferred は通常、その修正が(「Fix All」などを通じて)安全に自動適用できることを意味します。たとえ解決策が一つしかなくても、副作用があるような修正の場合は false のままにしておく方が適切かもしれません。単一 fix の場合のデフォルト挙動について、どのようにお考えでしょうか?