From 15c7c6ab970830a87b6aa502a369fb2b29b933c5 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Wed, 24 Feb 2021 12:29:17 +0100 Subject: [PATCH] zig fmt: handle comments in switch case value list --- lib/std/zig/parser_test.zig | 31 +++++++++++++++++++++++++++++++ lib/std/zig/render.zig | 4 +++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index c0d8a012c9df6efcdc9f668ebd93400455498b8f..5ac0d6202674c7359186808e45ab3e723673ab43 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -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" { try testError( \\var x: []align(0:0:0)u8 = bar; diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 0331ca2c70b38d73983de205c559c71505c22c6d..604ee4f31288c35022485902e3d48490683e4561 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1533,7 +1533,9 @@ fn renderSwitchCase( } else if (switch_case.ast.values.len == 1) { // render on one line and drop the trailing comma if any 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 try renderExpressions(gpa, ais, tree, switch_case.ast.values, .comma); } else { -- 2.54.0