authorgravatar for curtistatewilkinson@gmail.comCurtis Tate Wilkinson <curtistatewilkinson@gmail.com> 2022-03-15 08:10:59+10:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-14 23:10:59+01:00
log3bb4c0c78978a80a3e441552591f0209f032bfc7
tree906e35ba6c225d41df1f22e992e58c8b087dd2cc
parent5ea94e7715607e986298908536cdd3d9dfdd0ce9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

zig fmt: Resolve #11131 loss of comment on switch cases

Correct switch cases dropping comments in certain situations by checking for the presence of the comment before collapsing to one line.

2 files changed, 19 insertions(+), 4 deletions(-)

lib/std/zig/parser_test.zig+13
...@@ -1791,6 +1791,19 @@ test "zig fmt: switch comment before prong" {...@@ -1791,6 +1791,19 @@ test "zig fmt: switch comment before prong" {
1791 );1791 );
1792}1792}
17931793
1794test "zig fmt: switch comment after prong" {
1795 try testCanonical(
1796 \\comptime {
1797 \\ switch (a) {
1798 \\ 0,
1799 \\ // hi
1800 \\ => {},
1801 \\ }
1802 \\}
1803 \\
1804 );
1805}
1806
1794test "zig fmt: struct literal no trailing comma" {1807test "zig fmt: struct literal no trailing comma" {
1795 try testTransform(1808 try testTransform(
1796 \\const a = foo{ .x = 1, .y = 2 };1809 \\const a = foo{ .x = 1, .y = 2 };
lib/std/zig/render.zig+6-4
...@@ -1505,16 +1505,18 @@ fn renderSwitchCase(...@@ -1505,16 +1505,18 @@ fn renderSwitchCase(
1505 const node_tags = tree.nodes.items(.tag);1505 const node_tags = tree.nodes.items(.tag);
1506 const token_tags = tree.tokens.items(.tag);1506 const token_tags = tree.tokens.items(.tag);
1507 const trailing_comma = token_tags[switch_case.ast.arrow_token - 1] == .comma;1507 const trailing_comma = token_tags[switch_case.ast.arrow_token - 1] == .comma;
1508 const has_comment_before_arrow = blk: {
1509 if (switch_case.ast.values.len == 0) break :blk false;
1510 break :blk hasComment(tree, tree.firstToken(switch_case.ast.values[0]), switch_case.ast.arrow_token);
1511 };
15081512
1509 // Render everything before the arrow1513 // Render everything before the arrow
1510 if (switch_case.ast.values.len == 0) {1514 if (switch_case.ast.values.len == 0) {
1511 try renderToken(ais, tree, switch_case.ast.arrow_token - 1, .space); // else keyword1515 try renderToken(ais, tree, switch_case.ast.arrow_token - 1, .space); // else keyword
1512 } else if (switch_case.ast.values.len == 1) {1516 } else if (switch_case.ast.values.len == 1 and !has_comment_before_arrow) {
1513 // render on one line and drop the trailing comma if any1517 // render on one line and drop the trailing comma if any
1514 try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space);1518 try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space);
1515 } else if (trailing_comma or1519 } else if (trailing_comma or has_comment_before_arrow) {
1516 hasComment(tree, tree.firstToken(switch_case.ast.values[0]), switch_case.ast.arrow_token))
1517 {
1518 // Render each value on a new line1520 // Render each value on a new line
1519 try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma);1521 try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma);
1520 } else {1522 } else {