zig fmt: handle comments in switch case value list

This commit is contained in:
Isaac Freund 2021-02-24 12:29:17 +01:00
parent 1b8eca030e
commit 15c7c6ab97
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
2 changed files with 34 additions and 1 deletions

View File

@ -4202,6 +4202,37 @@ test "zig fmt: respect extra newline between switch items" {
); );
} }
test "zig fmt: insert trailing comma if there are comments between switch values" {
try testTransform(
\\const a = switch (b) {
\\ .c => {},
\\
\\ .d, // foobar
\\ .e
\\ => f,
\\
\\ .g, .h
\\ // comment
\\ => i,
\\};
\\
,
\\const a = switch (b) {
\\ .c => {},
\\
\\ .d, // foobar
\\ .e,
\\ => f,
\\
\\ .g,
\\ .h,
\\ // comment
\\ => i,
\\};
\\
);
}
test "zig fmt: error for invalid bit range" { test "zig fmt: error for invalid bit range" {
try testError( try testError(
\\var x: []align(0:0:0)u8 = bar; \\var x: []align(0:0:0)u8 = bar;

View File

@ -1533,7 +1533,9 @@ fn renderSwitchCase(
} else if (switch_case.ast.values.len == 1) { } else if (switch_case.ast.values.len == 1) {
// render on one line and drop the trailing comma if any // render on one line and drop the trailing comma if any
try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space); try renderExpression(gpa, ais, tree, switch_case.ast.values[0], .space);
} else if (trailing_comma) { } else if (trailing_comma or
hasComment(tree, tree.firstToken(switch_case.ast.values[0]), switch_case.ast.arrow_token))
{
// Render each value on a new line // Render each value on a new line
try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma); try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma);
} else { } else {