Commit 621ad241d6

Andrew Kelley <andrew@ziglang.org>
2021-02-22 04:25:31
zig fmt: if nested
1 parent a17a5ca
Changed files (2)
lib/std/zig/parser_test.zig
@@ -1449,26 +1449,26 @@ test "zig fmt: if-else with comment before else" {
     );
 }
 
-//test "zig fmt: if nested" {
-//    try testCanonical(
-//        \\pub fn foo() void {
-//        \\    return if ((aInt & bInt) >= 0)
-//        \\        if (aInt < bInt)
-//        \\            GE_LESS
-//        \\        else if (aInt == bInt)
-//        \\            GE_EQUAL
-//        \\        else
-//        \\            GE_GREATER
-//        \\    else if (aInt > bInt)
-//        \\        GE_LESS
-//        \\    else if (aInt == bInt)
-//        \\        GE_EQUAL
-//        \\    else
-//        \\        GE_GREATER;
-//        \\}
-//        \\
-//    );
-//}
+test "zig fmt: if nested" {
+    try testCanonical(
+        \\pub fn foo() void {
+        \\    return if ((aInt & bInt) >= 0)
+        \\        if (aInt < bInt)
+        \\            GE_LESS
+        \\        else if (aInt == bInt)
+        \\            GE_EQUAL
+        \\        else
+        \\            GE_GREATER
+        \\    else if (aInt > bInt)
+        \\        GE_LESS
+        \\    else if (aInt == bInt)
+        \\        GE_EQUAL
+        \\    else
+        \\        GE_GREATER;
+        \\}
+        \\
+    );
+}
 
 test "zig fmt: respect line breaks in if-else" {
     try testCanonical(
lib/std/zig/render.zig
@@ -984,7 +984,8 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
     try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // (
     try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition
 
-    if (nodeIsBlock(node_tags[while_node.ast.then_expr])) {
+    const then_tag = node_tags[while_node.ast.then_expr];
+    if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) {
         if (while_node.payload_token) |payload_token| {
             try renderToken(ais, tree, payload_token - 2, .space); // )
             try renderToken(ais, tree, payload_token - 1, .none); // |
@@ -2128,6 +2129,13 @@ fn nodeIsBlock(tag: ast.Node.Tag) bool {
     };
 }
 
+fn nodeIsIf(tag: ast.Node.Tag) bool {
+    return switch (tag) {
+        .@"if", .if_simple => true,
+        else => false,
+    };
+}
+
 fn nodeCausesSliceOpSpace(tag: ast.Node.Tag) bool {
     return switch (tag) {
         .@"catch",