authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-22 17:55:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-22 17:55:19-07:00
logfec51ad7c5fe5f141f8f0c53a1287c5a00613429
tree7c9e33de6ed01338c0bc4c7e6c5cd4e018f09ec1
parent20cfa0b5b62bf712d5aac882f7fde9b8e8cacae3

zig fmt: while


3 files changed, 121 insertions(+), 97 deletions(-)

lib/std/zig/ast.zig+9-4
......@@ -587,11 +587,16 @@ pub const Tree = struct {
587587 .for_simple,
588588 .@"for",
589589 => {
590 // Look for a label and inline.
590591 const main_token = main_tokens[n];
591 return switch (token_tags[main_token - 1]) {
592 .keyword_inline => main_token - 1,
593 else => main_token,
594 } - end_offset;
592 var result = main_token;
593 if (token_tags[result - 1] == .keyword_inline) {
594 result -= 1;
595 }
596 if (token_tags[result - 1] == .colon) {
597 result -= 2;
598 }
599 return result - end_offset;
595600 },
596601 };
597602 }
lib/std/zig/parser_test.zig+98-88
......@@ -1456,17 +1456,17 @@ test "zig fmt: if condition has line break but must not wrap (no fn call comma)"
14561456 );
14571457}
14581458
1459//test "zig fmt: function call with multiline argument" {
1460// try testCanonical(
1461// \\comptime {
1462// \\ self.user_input_options.put(name, UserInputOption{
1463// \\ .name = name,
1464// \\ .used = false,
1465// \\ });
1466// \\}
1467// \\
1468// );
1469//}
1459test "zig fmt: function call with multiline argument" {
1460 try testCanonical(
1461 \\comptime {
1462 \\ self.user_input_options.put(name, UserInputOption{
1463 \\ .name = name,
1464 \\ .used = false,
1465 \\ });
1466 \\}
1467 \\
1468 );
1469}
14701470
14711471test "zig fmt: if-else with comment before else" {
14721472 try testCanonical(
......@@ -1817,14 +1817,24 @@ test "zig fmt: empty block with only comment" {
18171817 );
18181818}
18191819
1820//test "zig fmt: no trailing comma on struct decl" {
1821// try testCanonical(
1822// \\const RoundParam = struct {
1823// \\ k: usize, s: u32, t: u32
1824// \\};
1825// \\
1826// );
1827//}
1820test "zig fmt: trailing commas on struct decl" {
1821 try testTransform(
1822 \\const RoundParam = struct {
1823 \\ k: usize, s: u32, t: u32
1824 \\};
1825 \\const RoundParam = struct {
1826 \\ k: usize, s: u32, t: u32,
1827 \\};
1828 ,
1829 \\const RoundParam = struct { k: usize, s: u32, t: u32 };
1830 \\const RoundParam = struct {
1831 \\ k: usize,
1832 \\ s: u32,
1833 \\ t: u32,
1834 \\};
1835 \\
1836 );
1837}
18281838
18291839test "zig fmt: extra newlines at the end" {
18301840 try testTransform(
......@@ -2975,75 +2985,75 @@ test "zig fmt: switch" {
29752985 );
29762986}
29772987
2978//test "zig fmt: while" {
2979// try testCanonical(
2980// \\test "while" {
2981// \\ while (10 < 1) unreachable;
2982// \\
2983// \\ while (10 < 1) unreachable else unreachable;
2984// \\
2985// \\ while (10 < 1) {
2986// \\ unreachable;
2987// \\ }
2988// \\
2989// \\ while (10 < 1)
2990// \\ unreachable;
2991// \\
2992// \\ var i: usize = 0;
2993// \\ while (i < 10) : (i += 1) {
2994// \\ continue;
2995// \\ }
2996// \\
2997// \\ i = 0;
2998// \\ while (i < 10) : (i += 1)
2999// \\ continue;
3000// \\
3001// \\ i = 0;
3002// \\ var j: usize = 0;
3003// \\ while (i < 10) : ({
3004// \\ i += 1;
3005// \\ j += 1;
3006// \\ }) {
3007// \\ continue;
3008// \\ }
3009// \\
3010// \\ var a: ?u8 = 2;
3011// \\ while (a) |v| : (a = null) {
3012// \\ continue;
3013// \\ }
3014// \\
3015// \\ while (a) |v| : (a = null)
3016// \\ unreachable;
3017// \\
3018// \\ label: while (10 < 0) {
3019// \\ unreachable;
3020// \\ }
3021// \\
3022// \\ const res = while (0 < 10) {
3023// \\ break 7;
3024// \\ } else {
3025// \\ unreachable;
3026// \\ };
3027// \\
3028// \\ const res = while (0 < 10)
3029// \\ break 7
3030// \\ else
3031// \\ unreachable;
3032// \\
3033// \\ var a: anyerror!u8 = 0;
3034// \\ while (a) |v| {
3035// \\ a = error.Err;
3036// \\ } else |err| {
3037// \\ i = 1;
3038// \\ }
3039// \\
3040// \\ comptime var k: usize = 0;
3041// \\ inline while (i < 10) : (i += 1)
3042// \\ j += 2;
3043// \\}
3044// \\
3045// );
3046//}
2988test "zig fmt: while" {
2989 try testCanonical(
2990 \\test "while" {
2991 \\ while (10 < 1) unreachable;
2992 \\
2993 \\ while (10 < 1) unreachable else unreachable;
2994 \\
2995 \\ while (10 < 1) {
2996 \\ unreachable;
2997 \\ }
2998 \\
2999 \\ while (10 < 1)
3000 \\ unreachable;
3001 \\
3002 \\ var i: usize = 0;
3003 \\ while (i < 10) : (i += 1) {
3004 \\ continue;
3005 \\ }
3006 \\
3007 \\ i = 0;
3008 \\ while (i < 10) : (i += 1)
3009 \\ continue;
3010 \\
3011 \\ i = 0;
3012 \\ var j: usize = 0;
3013 \\ while (i < 10) : ({
3014 \\ i += 1;
3015 \\ j += 1;
3016 \\ }) {
3017 \\ continue;
3018 \\ }
3019 \\
3020 \\ var a: ?u8 = 2;
3021 \\ while (a) |v| : (a = null) {
3022 \\ continue;
3023 \\ }
3024 \\
3025 \\ while (a) |v| : (a = null)
3026 \\ unreachable;
3027 \\
3028 \\ label: while (10 < 0) {
3029 \\ unreachable;
3030 \\ }
3031 \\
3032 \\ const res = while (0 < 10) {
3033 \\ break 7;
3034 \\ } else {
3035 \\ unreachable;
3036 \\ };
3037 \\
3038 \\ const res = while (0 < 10)
3039 \\ break 7
3040 \\ else
3041 \\ unreachable;
3042 \\
3043 \\ var a: anyerror!u8 = 0;
3044 \\ while (a) |v| {
3045 \\ a = error.Err;
3046 \\ } else |err| {
3047 \\ i = 1;
3048 \\ }
3049 \\
3050 \\ comptime var k: usize = 0;
3051 \\ inline while (i < 10) : (i += 1)
3052 \\ j += 2;
3053 \\}
3054 \\
3055 );
3056}
30473057
30483058test "zig fmt: for" {
30493059 try testCanonical(
lib/std/zig/render.zig+14-5
......@@ -1006,11 +1006,17 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
10061006 break :blk ident + 1;
10071007 }
10081008 };
1009 const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
1009 const brace_space = if (while_node.ast.cont_expr == 0 and ais.isLineOverIndented())
1010 Space.newline
1011 else
1012 Space.space;
10101013 try renderToken(ais, tree, pipe, brace_space); // |
10111014 } else {
10121015 const rparen = tree.lastToken(while_node.ast.cond_expr) + 1;
1013 const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
1016 const brace_space = if (while_node.ast.cont_expr == 0 and ais.isLineOverIndented())
1017 Space.newline
1018 else
1019 Space.space;
10141020 try renderToken(ais, tree, rparen, brace_space); // rparen
10151021 }
10161022 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
10191025 try renderToken(ais, tree, lparen - 1, .space); // :
10201026 try renderToken(ais, tree, lparen, .none); // lparen
10211027 try renderExpression(ais, tree, while_node.ast.cont_expr, .none);
1022 try renderToken(ais, tree, rparen, .space); // rparen
1028 const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
1029 try renderToken(ais, tree, rparen, brace_space); // rparen
10231030 }
10241031 if (while_node.ast.else_expr != 0) {
10251032 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
10611068 break :blk ident + 1;
10621069 }
10631070 };
1064 try renderToken(ais, tree, pipe, .newline); // |
1071 const after_space: Space = if (while_node.ast.cont_expr != 0) .space else .newline;
1072 try renderToken(ais, tree, pipe, after_space); // |
10651073 } else {
10661074 ais.pushIndent();
1067 try renderToken(ais, tree, rparen, .newline); // rparen
1075 const after_space: Space = if (while_node.ast.cont_expr != 0) .space else .newline;
1076 try renderToken(ais, tree, rparen, after_space); // rparen
10681077 ais.popIndent();
10691078 }
10701079 if (while_node.ast.cont_expr != 0) {