From 550688f427a2303f5e309a269def4d8be532cc54 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 22 Feb 2021 23:14:01 +0100 Subject: [PATCH] zig fmt: insert trailing comma in switches --- lib/std/zig/parser_test.zig | 56 ++++++++++++++++++------------------- lib/std/zig/render.zig | 6 +++- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 2c43e04ae552289e25f92ed4937c74dab58b89b4..42df500ddc037650daf1bf218dae9b210727cfbc 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -2103,34 +2103,34 @@ test "zig fmt: same line comments in expression" { ); } -//test "zig fmt: add comma on last switch prong" { -// try testTransform( -// \\test "aoeu" { -// \\switch (self.init_arg_expr) { -// \\ InitArg.Type => |t| { }, -// \\ InitArg.None, -// \\ InitArg.Enum => { } -// \\} -// \\ switch (self.init_arg_expr) { -// \\ InitArg.Type => |t| { }, -// \\ InitArg.None, -// \\ InitArg.Enum => { }//line comment -// \\ } -// \\} -// , -// \\test "aoeu" { -// \\ switch (self.init_arg_expr) { -// \\ InitArg.Type => |t| {}, -// \\ InitArg.None, InitArg.Enum => {}, -// \\ } -// \\ switch (self.init_arg_expr) { -// \\ InitArg.Type => |t| {}, -// \\ InitArg.None, InitArg.Enum => {}, //line comment -// \\ } -// \\} -// \\ -// ); -//} +test "zig fmt: add comma on last switch prong" { + try testTransform( + \\test "aoeu" { + \\switch (self.init_arg_expr) { + \\ InitArg.Type => |t| { }, + \\ InitArg.None, + \\ InitArg.Enum => { } + \\} + \\ switch (self.init_arg_expr) { + \\ InitArg.Type => |t| { }, + \\ InitArg.None, + \\ InitArg.Enum => { }//line comment + \\ } + \\} + , + \\test "aoeu" { + \\ switch (self.init_arg_expr) { + \\ InitArg.Type => |t| {}, + \\ InitArg.None, InitArg.Enum => {}, + \\ } + \\ switch (self.init_arg_expr) { + \\ InitArg.Type => |t| {}, + \\ InitArg.None, InitArg.Enum => {}, //line comment + \\ } + \\} + \\ + ); +} test "zig fmt: same-line comment after a statement" { try testCanonical( diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index fe9df25dad16f0c5b4fc658de31d00b7642db279..67a7a9a5144d60e2db2753cf1f2887362df971c1 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1948,7 +1948,7 @@ const Space = enum { space, /// Output the token lexeme followed by a newline. newline, - /// Additionally consume the next token if it is a comma. + /// If the next token is a comma, render it as well. If not, insert one. /// In either case, a newline will be inserted afterwards. comma, /// 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 try ais.writer().writeAll(lexeme); + if (space == .comma and token_tags[token_index + 1] != .comma) { + try ais.writer().writeByte(','); + } + const comment = try renderComments(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]); switch (space) { .none => {}, -- 2.54.0