From 9f4d44bc491b907dbe11fbc8cd70749606b2236c Mon Sep 17 00:00:00 2001 From: Hong Shick Pak Date: Thu, 29 Aug 2019 21:10:26 -0700 Subject: [PATCH] zig fmt: fix nested if --- std/zig/parser_test.zig | 21 +++++++++++++++++++++ std/zig/render.zig | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) 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); // )