mirror of
https://github.com/ziglang/zig.git
synced 2025-12-16 11:13:08 +00:00
zig fmt: handle comments in switch case value list
This commit is contained in:
parent
1b8eca030e
commit
15c7c6ab97
@ -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;
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user