authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-21 20:25:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-21 20:25:31-07:00
log621ad241d6dfbde60ee8a5b1d0dcd7d9cf29f8f3
tree9d3f70712ae2fb7a1205acc14db2f72bde03383b
parenta17a5ca3a85c92aa933a8eaac66198b31b8b294c

zig fmt: if nested


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

lib/std/zig/parser_test.zig+20-20
......@@ -1449,26 +1449,26 @@ test "zig fmt: if-else with comment before else" {
14491449 );
14501450}
14511451
1452//test "zig fmt: if nested" {
1453// try testCanonical(
1454// \\pub fn foo() void {
1455// \\ return if ((aInt & bInt) >= 0)
1456// \\ if (aInt < bInt)
1457// \\ GE_LESS
1458// \\ else if (aInt == bInt)
1459// \\ GE_EQUAL
1460// \\ else
1461// \\ GE_GREATER
1462// \\ else if (aInt > bInt)
1463// \\ GE_LESS
1464// \\ else if (aInt == bInt)
1465// \\ GE_EQUAL
1466// \\ else
1467// \\ GE_GREATER;
1468// \\}
1469// \\
1470// );
1471//}
1452test "zig fmt: if nested" {
1453 try testCanonical(
1454 \\pub fn foo() void {
1455 \\ return if ((aInt & bInt) >= 0)
1456 \\ if (aInt < bInt)
1457 \\ GE_LESS
1458 \\ else if (aInt == bInt)
1459 \\ GE_EQUAL
1460 \\ else
1461 \\ GE_GREATER
1462 \\ else if (aInt > bInt)
1463 \\ GE_LESS
1464 \\ else if (aInt == bInt)
1465 \\ GE_EQUAL
1466 \\ else
1467 \\ GE_GREATER;
1468 \\}
1469 \\
1470 );
1471}
14721472
14731473test "zig fmt: respect line breaks in if-else" {
14741474 try testCanonical(
lib/std/zig/render.zig+9-1
......@@ -984,7 +984,8 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
984984 try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // (
985985 try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition
986986
987 if (nodeIsBlock(node_tags[while_node.ast.then_expr])) {
987 const then_tag = node_tags[while_node.ast.then_expr];
988 if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) {
988989 if (while_node.payload_token) |payload_token| {
989990 try renderToken(ais, tree, payload_token - 2, .space); // )
990991 try renderToken(ais, tree, payload_token - 1, .none); // |
......@@ -2128,6 +2129,13 @@ fn nodeIsBlock(tag: ast.Node.Tag) bool {
21282129 };
21292130}
21302131
2132fn nodeIsIf(tag: ast.Node.Tag) bool {
2133 return switch (tag) {
2134 .@"if", .if_simple => true,
2135 else => false,
2136 };
2137}
2138
21312139fn nodeCausesSliceOpSpace(tag: ast.Node.Tag) bool {
21322140 return switch (tag) {
21332141 .@"catch",