authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-24 12:29:17+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-24 12:29:17+01:00
log15c7c6ab970830a87b6aa502a369fb2b29b933c5
treed169f38dd855e286559b3f2c9883e2cb61ca3247
parent1b8eca030e9dac51361d80428f8276a09f31a8c2
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: handle comments in switch case value list


2 files changed, 34 insertions(+), 1 deletions(-)

lib/std/zig/parser_test.zig+31
...@@ -4202,6 +4202,37 @@ test "zig fmt: respect extra newline between switch items" {...@@ -4202,6 +4202,37 @@ test "zig fmt: respect extra newline between switch items" {
4202 );4202 );
4203}4203}
42044204
4205test "zig fmt: insert trailing comma if there are comments between switch values" {
4206 try testTransform(
4207 \\const a = switch (b) {
4208 \\ .c => {},
4209 \\
4210 \\ .d, // foobar
4211 \\ .e
4212 \\ => f,
4213 \\
4214 \\ .g, .h
4215 \\ // comment
4216 \\ => i,
4217 \\};
4218 \\
4219 ,
4220 \\const a = switch (b) {
4221 \\ .c => {},
4222 \\
4223 \\ .d, // foobar
4224 \\ .e,
4225 \\ => f,
4226 \\
4227 \\ .g,
4228 \\ .h,
4229 \\ // comment
4230 \\ => i,
4231 \\};
4232 \\
4233 );
4234}
4235
4205test "zig fmt: error for invalid bit range" {4236test "zig fmt: error for invalid bit range" {
4206 try testError(4237 try testError(
4207 \\var x: []align(0:0:0)u8 = bar;4238 \\var x: []align(0:0:0)u8 = bar;
lib/std/zig/render.zig+3-1
...@@ -1533,7 +1533,9 @@ fn renderSwitchCase(...@@ -1533,7 +1533,9 @@ fn renderSwitchCase(
1533 } else if (switch_case.ast.values.len == 1) {1533 } else if (switch_case.ast.values.len == 1) {
1534 // render on one line and drop the trailing comma if any1534 // render on one line and drop the trailing comma if any
1535 try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space);1535 try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space);
1536 } else if (trailing_comma) {1536 } else if (trailing_comma or
1537 hasComment(tree, tree.firstToken(switch_case.ast.values[0]), switch_case.ast.arrow_token))
1538 {
1537 // Render each value on a new line1539 // Render each value on a new line
1538 try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma);1540 try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma);
1539 } else {1541 } else {