Skip to content

Commit

Permalink
correctly detect last parameter when discarding function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Dec 18, 2023
1 parent 54d2672 commit 1264b19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/features/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ fn handleUnusedFunctionParameter(builder: *Builder, actions: *std.ArrayListUnman
// ) void { ... }
// We have to be able to detect both cases.
const fn_proto_param = payload.get(tree).?;
const param_end = offsets.tokenToLoc(tree, ast.paramLastToken(tree, fn_proto_param)).end;
const last_param_token = ast.paramLastToken(tree, fn_proto_param);

const found_comma = std.mem.startsWith(
u8,
std.mem.trimLeft(u8, tree.source[param_end..], " \n"),
",",
);
const potential_comma_token = last_param_token + 1;
const found_comma = potential_comma_token < tree.tokens.len and tree.tokens.items(.tag)[potential_comma_token] == .comma;

const new_text = try createDiscardText(builder, identifier_name, token_starts[node_tokens[payload.func]], true, !found_comma);
const potential_r_paren_token = last_param_token + @intFromBool(found_comma) + 1;
const is_last_param = potential_r_paren_token < tree.tokens.len and tree.tokens.items(.tag)[potential_r_paren_token] == .r_paren;

const new_text = try createDiscardText(builder, identifier_name, token_starts[node_tokens[payload.func]], true, is_last_param);

const index = token_starts[node_tokens[block]] + 1;

Expand Down
11 changes: 11 additions & 0 deletions tests/lsp_features/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ test "code actions - discard function parameter" {
\\}
\\
);
try testAutofix(
\\fn foo(a: void, b: void, c: void,) void {}
\\
,
\\fn foo(a: void, b: void, c: void,) void {
\\ _ = a; // autofix
\\ _ = b; // autofix
\\ _ = c; // autofix
\\}
\\
);
}

test "code actions - discard captures" {
Expand Down

0 comments on commit 1264b19

Please sign in to comment.