Skip to content

Commit 7801686

Browse files
authored
Merge pull request #1065 from julia-vscode/fe/replace-unused
Implement quickfix action to replace unused assignment name with _.
2 parents a590ff3 + 3b1a021 commit 7801686

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/requests/actions.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ function remove_farg_name(x, server, conn)
321321
JSONRPC.send(conn, workspace_applyEdit_request_type, ApplyWorkspaceEditParams(missing, WorkspaceEdit(missing, TextDocumentEdit[tde])))
322322
end
323323

324+
function remove_unused_assignment_name(x, _, conn)
325+
x1 = StaticLint.get_parent_fexpr(x, x -> StaticLint.haserror(x) && StaticLint.errorof(x) == StaticLint.UnusedBinding && x isa EXPR && x.head === :IDENTIFIER)
326+
file, offset = get_file_loc(x1)
327+
tde = TextDocumentEdit(VersionedTextDocumentIdentifier(file._uri, file._version), TextEdit[
328+
TextEdit(Range(file, offset .+ (0:x1.span)), "_")
329+
])
330+
JSONRPC.send(conn, workspace_applyEdit_request_type, ApplyWorkspaceEditParams(missing, WorkspaceEdit(missing, TextDocumentEdit[tde])))
331+
end
332+
324333
function double_to_triple_equal(x, _, conn)
325334
x1 = StaticLint.get_parent_fexpr(x, y -> StaticLint.haserror(y) && StaticLint.errorof(y) in (StaticLint.NothingEquality, StaticLint.NothingNotEq))
326335
file, offset = get_file_loc(x1)
@@ -383,6 +392,15 @@ LSActions["DeleteUnusedFunctionArgumentName"] = ServerAction(
383392
remove_farg_name,
384393
)
385394

395+
LSActions["ReplaceUnusedAssignmentName"] = ServerAction(
396+
"ReplaceUnusedAssignmentName",
397+
"Replace unused assignment name with _.",
398+
CodeActionKinds.QuickFix,
399+
missing,
400+
(x, params) -> StaticLint.is_in_fexpr(x, x -> StaticLint.haserror(x) && StaticLint.errorof(x) == StaticLint.UnusedBinding && x isa EXPR && x.head === :IDENTIFIER),
401+
remove_unused_assignment_name,
402+
)
403+
386404
LSActions["CompareNothingWithTripleEqual"] = ServerAction(
387405
"CompareNothingWithTripleEqual",
388406
"Change ==/!= to ===/!==.",

test/requests/actions.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ end
5454
LanguageServer.workspace_executeCommand_request(LanguageServer.ExecuteCommandParams(missing, c.command, c.arguments), server, server.jr_endpoint)
5555
end
5656

57+
@testset "unused assignment" begin
58+
doc = settestdoc("function f()\n x = 1 + 2\n return 3\nend\n")
59+
60+
@test any(c.command == "ReplaceUnusedAssignmentName" for c in action_request_test(1, 4))
61+
c = filter(c -> c.command == "ReplaceUnusedAssignmentName", action_request_test(1, 4))[1]
62+
63+
LanguageServer.workspace_executeCommand_request(LanguageServer.ExecuteCommandParams(missing, c.command, c.arguments), server, server.jr_endpoint)
64+
end
65+
5766
@testset "===/!== for nothing comparison" begin
5867
for str in ("x = 1\nif x == nothing end", "x = 1\nif x != nothing end")
5968
doc = settestdoc(str)

0 commit comments

Comments
 (0)