authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-22 23:14:01+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-22 23:51:54+01:00
log550688f427a2303f5e309a269def4d8be532cc54
tree2c054ae7c1f0dfa94f6d5d2c556ccb65f01dfef6
parent4758e0c9a662b59e071b52fbcc931fae138292d7
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: insert trailing comma in switches


2 files changed, 33 insertions(+), 29 deletions(-)

lib/std/zig/parser_test.zig+28-28
......@@ -2103,34 +2103,34 @@ test "zig fmt: same line comments in expression" {
21032103 );
21042104}
21052105
2106//test "zig fmt: add comma on last switch prong" {
2107// try testTransform(
2108// \\test "aoeu" {
2109// \\switch (self.init_arg_expr) {
2110// \\ InitArg.Type => |t| { },
2111// \\ InitArg.None,
2112// \\ InitArg.Enum => { }
2113// \\}
2114// \\ switch (self.init_arg_expr) {
2115// \\ InitArg.Type => |t| { },
2116// \\ InitArg.None,
2117// \\ InitArg.Enum => { }//line comment
2118// \\ }
2119// \\}
2120// ,
2121// \\test "aoeu" {
2122// \\ switch (self.init_arg_expr) {
2123// \\ InitArg.Type => |t| {},
2124// \\ InitArg.None, InitArg.Enum => {},
2125// \\ }
2126// \\ switch (self.init_arg_expr) {
2127// \\ InitArg.Type => |t| {},
2128// \\ InitArg.None, InitArg.Enum => {}, //line comment
2129// \\ }
2130// \\}
2131// \\
2132// );
2133//}
2106test "zig fmt: add comma on last switch prong" {
2107 try testTransform(
2108 \\test "aoeu" {
2109 \\switch (self.init_arg_expr) {
2110 \\ InitArg.Type => |t| { },
2111 \\ InitArg.None,
2112 \\ InitArg.Enum => { }
2113 \\}
2114 \\ switch (self.init_arg_expr) {
2115 \\ InitArg.Type => |t| { },
2116 \\ InitArg.None,
2117 \\ InitArg.Enum => { }//line comment
2118 \\ }
2119 \\}
2120 ,
2121 \\test "aoeu" {
2122 \\ switch (self.init_arg_expr) {
2123 \\ InitArg.Type => |t| {},
2124 \\ InitArg.None, InitArg.Enum => {},
2125 \\ }
2126 \\ switch (self.init_arg_expr) {
2127 \\ InitArg.Type => |t| {},
2128 \\ InitArg.None, InitArg.Enum => {}, //line comment
2129 \\ }
2130 \\}
2131 \\
2132 );
2133}
21342134
21352135test "zig fmt: same-line comment after a statement" {
21362136 try testCanonical(
lib/std/zig/render.zig+5-1
......@@ -1948,7 +1948,7 @@ const Space = enum {
19481948 space,
19491949 /// Output the token lexeme followed by a newline.
19501950 newline,
1951 /// Additionally consume the next token if it is a comma.
1951 /// If the next token is a comma, render it as well. If not, insert one.
19521952 /// In either case, a newline will be inserted afterwards.
19531953 comma,
19541954 /// Additionally consume the next token if it is a comma.
......@@ -1968,6 +1968,10 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp
19681968
19691969 try ais.writer().writeAll(lexeme);
19701970
1971 if (space == .comma and token_tags[token_index + 1] != .comma) {
1972 try ais.writer().writeByte(',');
1973 }
1974
19711975 const comment = try renderComments(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]);
19721976 switch (space) {
19731977 .none => {},