Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
zig fmt: Preserve trailing comma after single-item switch case
Browse files Browse the repository at this point in the history
  • Loading branch information
castholm authored and Vexu committed Feb 18, 2024
1 parent ab6317d commit 3cafb96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,10 @@ test "zig fmt: switch cases trailing comma" {
\\ 1,2,3 => {},
\\ 4,5, => {},
\\ 6... 8, => {},
\\ 9 ...
\\ 10 => {},
\\ 11 => {},
\\ 12, => {},
\\ else => {},
\\ }
\\}
Expand All @@ -2240,7 +2244,12 @@ test "zig fmt: switch cases trailing comma" {
\\ 4,
\\ 5,
\\ => {},
\\ 6...8 => {},
\\ 6...8,
\\ => {},
\\ 9...10 => {},
\\ 11 => {},
\\ 12,
\\ => {},
\\ else => {},
\\ }
\\}
Expand Down
3 changes: 0 additions & 3 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1894,9 +1894,6 @@ fn renderSwitchCase(
// Render everything before the arrow
if (switch_case.ast.values.len == 0) {
try renderToken(r, switch_case.ast.arrow_token - 1, .space); // else keyword
} else if (switch_case.ast.values.len == 1 and !has_comment_before_arrow) {
// render on one line and drop the trailing comma if any
try renderExpression(r, switch_case.ast.values[0], .space);
} else if (trailing_comma or has_comment_before_arrow) {
// Render each value on a new line
try renderExpressions(r, switch_case.ast.values, .comma);
Expand Down

0 comments on commit 3cafb96

Please sign in to comment.