zig fmt: fix nested if

This commit is contained in:
Hong Shick Pak 2019-08-29 21:10:26 -07:00
parent 10541c8fc8
commit 9f4d44bc49
2 changed files with 25 additions and 1 deletions

View File

@ -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 {

View File

@ -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); // )