From 9f4d44bc491b907dbe11fbc8cd70749606b2236c Mon Sep 17 00:00:00 2001 From: Hong Shick Pak Date: Thu, 29 Aug 2019 21:10:26 -0700 Subject: [PATCH 1/2] 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); // ) From 19964f5dc80792eb50cb3a45bdabe887a92d92cc Mon Sep 17 00:00:00 2001 From: Hong Shick Pak Date: Thu, 29 Aug 2019 21:23:49 -0700 Subject: [PATCH 2/2] run new zig fmt and remove stale comment --- std/special/compiler_rt/comparetf2.zig | 13 +++++++------ std/zig/render.zig | 1 - 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/std/special/compiler_rt/comparetf2.zig b/std/special/compiler_rt/comparetf2.zig index aaaba954d6..05e5974558 100644 --- a/std/special/compiler_rt/comparetf2.zig +++ b/std/special/compiler_rt/comparetf2.zig @@ -38,12 +38,14 @@ pub extern fn __letf2(a: f128, b: f128) c_int { // If at least one of a and b is positive, we get the same result comparing // a and b as signed integers as we would with a floating-point compare. - return if ((aInt & bInt) >= 0) if (aInt < bInt) - LE_LESS - else if (aInt == bInt) - LE_EQUAL + return if ((aInt & bInt) >= 0) + if (aInt < bInt) + LE_LESS + else if (aInt == bInt) + LE_EQUAL + else + LE_GREATER else - LE_GREATER else // Otherwise, both are negative, so we need to flip the sense of the // comparison to get the correct result. (This assumes a twos- or ones- // complement integer representation; if integers are represented in a @@ -73,7 +75,6 @@ pub extern fn __getf2(a: f128, b: f128) c_int { if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED; if ((aAbs | bAbs) == 0) return GE_EQUAL; - // zig fmt issue here, see https://github.com/ziglang/zig/issues/2661 return if ((aInt & bInt) >= 0) if (aInt < bInt) GE_LESS diff --git a/std/zig/render.zig b/std/zig/render.zig index 8a2e2690c0..9db7939a22 100644 --- a/std/zig/render.zig +++ b/std/zig/render.zig @@ -276,7 +276,6 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i } else { try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type, } - } else if (field.type_expr == null and field.value_expr != null) { try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // =