From 83c9a7b4938513be0c7704608cdb9fa52acf6c92 Mon Sep 17 00:00:00 2001 From: Joe Williams Date: Thu, 15 Jun 2023 15:53:33 -0400 Subject: [PATCH] autofix now handles comments in the event of a newline w/ comments (#1236) * autofix now handles comments in the event of a newline w/ comments * oob check * continue statements so we don't waste time checking things we know * added a skip for if the characters are a comment * convert to switch for readability --- src/features/code_actions.zig | 20 +++++++++++++++++--- tests/lsp_features/code_actions.zig | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/features/code_actions.zig b/src/features/code_actions.zig index 11df2f6..487ece6 100644 --- a/src/features/code_actions.zig +++ b/src/features/code_actions.zig @@ -183,9 +183,23 @@ fn handleUnusedCapture( // look for next non-whitespace after last '|'. if its a '{' we can insert discards. // this means bare loop/switch captures (w/out curlies) aren't supported. var block_start = capture_loc.end + 1; - while (block_start < builder.handle.text.len and - std.ascii.isWhitespace(builder.handle.text[block_start])) : (block_start += 1) - {} + var is_comment = false; + while (block_start < builder.handle.text.len) : (block_start += 1) + { + switch (builder.handle.text[block_start]) { + '/' => if (block_start + 1 < builder.handle.text.len and builder.handle.text[block_start + 1] == '/') { + is_comment = true; + // we already know the next character is a `/` so lets skip that iteration + block_start += 1; + }, + // if we go to a new line, drop the is_comment boolean + '\n' => if (is_comment) { + is_comment = false; + }, + //If the character is not a whitespace, and we're not in a comment then break out of the loop + else => |c| if(!std.ascii.isWhitespace(c) and !is_comment) break, + } + } if (builder.handle.text[block_start] != '{') { return; } diff --git a/tests/lsp_features/code_actions.zig b/tests/lsp_features/code_actions.zig index e458987..a83f045 100644 --- a/tests/lsp_features/code_actions.zig +++ b/tests/lsp_features/code_actions.zig @@ -81,6 +81,25 @@ test "code actions - discard captures" { ); } +test "code actions - discard capture with comment" { + try testAutofix( + \\test { + \\ if (1 == 1) |a| + \\ //a + \\ {} + \\} + , + \\test { + \\ if (1 == 1) |a| + \\ //a + \\ { + \\ _ = a; + \\ } + \\} + \\ + ); +} + test "code actions - remove pointless discard" { try testAutofix( \\fn foo(a: u32) u32 {