aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2021-02-22 23:14:01 +0100
committerIsaac Freund <ifreund@ifreund.xyz>2021-02-22 23:51:54 +0100
commit550688f427a2303f5e309a269def4d8be532cc54 (patch)
tree2c054ae7c1f0dfa94f6d5d2c556ccb65f01dfef6 /lib/std
parent4758e0c9a662b59e071b52fbcc931fae138292d7 (diff)
downloadzig-550688f427a2303f5e309a269def4d8be532cc54.tar.gz
zig-550688f427a2303f5e309a269def4d8be532cc54.zip
zig fmt: insert trailing comma in switches
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/zig/parser_test.zig56
-rw-r--r--lib/std/zig/render.zig6
2 files changed, 33 insertions, 29 deletions
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 2c43e04ae5..42df500ddc 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 fe9df25dad..67a7a9a514 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 => {},