From fec51ad7c5fe5f141f8f0c53a1287c5a00613429 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 22 Feb 2021 17:55:19 -0700 Subject: [PATCH] zig fmt: while --- lib/std/zig/ast.zig | 13 ++- lib/std/zig/parser_test.zig | 186 +++++++++++++++++++----------------- lib/std/zig/render.zig | 19 +++- 3 files changed, 121 insertions(+), 97 deletions(-) diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 40bb2256ec..abe4993d3e 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -587,11 +587,16 @@ pub const Tree = struct { .for_simple, .@"for", => { + // Look for a label and inline. const main_token = main_tokens[n]; - return switch (token_tags[main_token - 1]) { - .keyword_inline => main_token - 1, - else => main_token, - } - end_offset; + var result = main_token; + if (token_tags[result - 1] == .keyword_inline) { + result -= 1; + } + if (token_tags[result - 1] == .colon) { + result -= 2; + } + return result - end_offset; }, }; } diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index f7197e3853..63ad1d431c 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -1456,17 +1456,17 @@ test "zig fmt: if condition has line break but must not wrap (no fn call comma)" ); } -//test "zig fmt: function call with multiline argument" { -// try testCanonical( -// \\comptime { -// \\ self.user_input_options.put(name, UserInputOption{ -// \\ .name = name, -// \\ .used = false, -// \\ }); -// \\} -// \\ -// ); -//} +test "zig fmt: function call with multiline argument" { + try testCanonical( + \\comptime { + \\ self.user_input_options.put(name, UserInputOption{ + \\ .name = name, + \\ .used = false, + \\ }); + \\} + \\ + ); +} test "zig fmt: if-else with comment before else" { try testCanonical( @@ -1817,14 +1817,24 @@ test "zig fmt: empty block with only comment" { ); } -//test "zig fmt: no trailing comma on struct decl" { -// try testCanonical( -// \\const RoundParam = struct { -// \\ k: usize, s: u32, t: u32 -// \\}; -// \\ -// ); -//} +test "zig fmt: trailing commas on struct decl" { + try testTransform( + \\const RoundParam = struct { + \\ k: usize, s: u32, t: u32 + \\}; + \\const RoundParam = struct { + \\ k: usize, s: u32, t: u32, + \\}; + , + \\const RoundParam = struct { k: usize, s: u32, t: u32 }; + \\const RoundParam = struct { + \\ k: usize, + \\ s: u32, + \\ t: u32, + \\}; + \\ + ); +} test "zig fmt: extra newlines at the end" { try testTransform( @@ -2975,75 +2985,75 @@ test "zig fmt: switch" { ); } -//test "zig fmt: while" { -// try testCanonical( -// \\test "while" { -// \\ while (10 < 1) unreachable; -// \\ -// \\ while (10 < 1) unreachable else unreachable; -// \\ -// \\ while (10 < 1) { -// \\ unreachable; -// \\ } -// \\ -// \\ while (10 < 1) -// \\ unreachable; -// \\ -// \\ var i: usize = 0; -// \\ while (i < 10) : (i += 1) { -// \\ continue; -// \\ } -// \\ -// \\ i = 0; -// \\ while (i < 10) : (i += 1) -// \\ continue; -// \\ -// \\ i = 0; -// \\ var j: usize = 0; -// \\ while (i < 10) : ({ -// \\ i += 1; -// \\ j += 1; -// \\ }) { -// \\ continue; -// \\ } -// \\ -// \\ var a: ?u8 = 2; -// \\ while (a) |v| : (a = null) { -// \\ continue; -// \\ } -// \\ -// \\ while (a) |v| : (a = null) -// \\ unreachable; -// \\ -// \\ label: while (10 < 0) { -// \\ unreachable; -// \\ } -// \\ -// \\ const res = while (0 < 10) { -// \\ break 7; -// \\ } else { -// \\ unreachable; -// \\ }; -// \\ -// \\ const res = while (0 < 10) -// \\ break 7 -// \\ else -// \\ unreachable; -// \\ -// \\ var a: anyerror!u8 = 0; -// \\ while (a) |v| { -// \\ a = error.Err; -// \\ } else |err| { -// \\ i = 1; -// \\ } -// \\ -// \\ comptime var k: usize = 0; -// \\ inline while (i < 10) : (i += 1) -// \\ j += 2; -// \\} -// \\ -// ); -//} +test "zig fmt: while" { + try testCanonical( + \\test "while" { + \\ while (10 < 1) unreachable; + \\ + \\ while (10 < 1) unreachable else unreachable; + \\ + \\ while (10 < 1) { + \\ unreachable; + \\ } + \\ + \\ while (10 < 1) + \\ unreachable; + \\ + \\ var i: usize = 0; + \\ while (i < 10) : (i += 1) { + \\ continue; + \\ } + \\ + \\ i = 0; + \\ while (i < 10) : (i += 1) + \\ continue; + \\ + \\ i = 0; + \\ var j: usize = 0; + \\ while (i < 10) : ({ + \\ i += 1; + \\ j += 1; + \\ }) { + \\ continue; + \\ } + \\ + \\ var a: ?u8 = 2; + \\ while (a) |v| : (a = null) { + \\ continue; + \\ } + \\ + \\ while (a) |v| : (a = null) + \\ unreachable; + \\ + \\ label: while (10 < 0) { + \\ unreachable; + \\ } + \\ + \\ const res = while (0 < 10) { + \\ break 7; + \\ } else { + \\ unreachable; + \\ }; + \\ + \\ const res = while (0 < 10) + \\ break 7 + \\ else + \\ unreachable; + \\ + \\ var a: anyerror!u8 = 0; + \\ while (a) |v| { + \\ a = error.Err; + \\ } else |err| { + \\ i = 1; + \\ } + \\ + \\ comptime var k: usize = 0; + \\ inline while (i < 10) : (i += 1) + \\ j += 2; + \\} + \\ + ); +} test "zig fmt: for" { try testCanonical( diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index cd48013b42..0c7564e31e 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1006,11 +1006,17 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa break :blk ident + 1; } }; - const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space; + const brace_space = if (while_node.ast.cont_expr == 0 and ais.isLineOverIndented()) + Space.newline + else + Space.space; try renderToken(ais, tree, pipe, brace_space); // | } else { const rparen = tree.lastToken(while_node.ast.cond_expr) + 1; - const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space; + const brace_space = if (while_node.ast.cont_expr == 0 and ais.isLineOverIndented()) + Space.newline + else + Space.space; try renderToken(ais, tree, rparen, brace_space); // rparen } if (while_node.ast.cont_expr != 0) { @@ -1019,7 +1025,8 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa try renderToken(ais, tree, lparen - 1, .space); // : try renderToken(ais, tree, lparen, .none); // lparen try renderExpression(ais, tree, while_node.ast.cont_expr, .none); - try renderToken(ais, tree, rparen, .space); // rparen + const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space; + try renderToken(ais, tree, rparen, brace_space); // rparen } if (while_node.ast.else_expr != 0) { try renderExpression(ais, tree, while_node.ast.then_expr, Space.space); @@ -1061,10 +1068,12 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa break :blk ident + 1; } }; - try renderToken(ais, tree, pipe, .newline); // | + const after_space: Space = if (while_node.ast.cont_expr != 0) .space else .newline; + try renderToken(ais, tree, pipe, after_space); // | } else { ais.pushIndent(); - try renderToken(ais, tree, rparen, .newline); // rparen + const after_space: Space = if (while_node.ast.cont_expr != 0) .space else .newline; + try renderToken(ais, tree, rparen, after_space); // rparen ais.popIndent(); } if (while_node.ast.cont_expr != 0) {