Commit 9f4d44bc49

Hong Shick Pak <hong@hspak.com>
2019-08-30 06:10:26
zig fmt: fix nested if
1 parent 10541c8
Changed files (2)
std/zig/parser_test.zig
@@ -482,6 +482,27 @@ 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: respect line breaks in if-else" {
     try testCanonical(
         \\comptime {
std/zig/render.zig
@@ -1521,9 +1521,12 @@ fn renderExpression(
 
             try renderExpression(allocator, stream, tree, indent, start_col, if_node.condition, Space.None); // condition
 
+            const body_is_if_block = if_node.body.id == ast.Node.Id.If;
             const body_is_block = nodeIsBlock(if_node.body);
 
-            if (body_is_block) {
+            if (body_is_if_block) {
+                try renderExtraNewline(tree, stream, start_col, if_node.body);
+            } else if (body_is_block) {
                 const after_rparen_space = if (if_node.payload == null) Space.BlockStart else Space.Space;
                 try renderToken(tree, stream, rparen, indent, start_col, after_rparen_space); // )