diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index c601e6e8bf..b0c7b9135e 100644 --- a/std/zig/parser_test.zig +++ b/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 { diff --git a/std/zig/render.zig b/std/zig/render.zig index 5695e8801f..8a2e2690c0 100644 --- a/std/zig/render.zig +++ b/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); // )