From 0d1b47362c7623ba923a6831dab0989133caf9ab Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 May 2018 22:41:05 -0400 Subject: [PATCH] zig fmt: if-else with comment before else --- std/zig/parser_test.zig | 18 ++++++++++++++++++ std/zig/render.zig | 20 ++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 30e7eb3e7..0f8b2d798 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -1,3 +1,21 @@ +test "zig fmt: if-else with comment before else" { + try testCanonical( + \\comptime { + \\ // cexp(finite|nan +- i inf|nan) = nan + i nan + \\ if ((hx & 0x7fffffff) != 0x7f800000) { + \\ return Complex(f32).new(y - y, y - y); + \\ } // cexp(-inf +- i inf|nan) = 0 + i0 + \\ else if (hx & 0x80000000 != 0) { + \\ return Complex(f32).new(0, 0); + \\ } // cexp(+inf +- i inf|nan) = inf + i nan + \\ else { + \\ return Complex(f32).new(x, y - y); + \\ } + \\} + \\ + ); +} + 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 90bc74fd6..af8f3f385 100644 --- a/std/zig/render.zig +++ b/std/zig/render.zig @@ -1341,7 +1341,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind } if (if_node.@"else") |@"else"| { - try renderExpression(allocator, stream, tree, indent, if_node.body, Space.Space); + try renderExpression(allocator, stream, tree, indent, if_node.body, Space.SpaceOrOutdent); return renderExpression(allocator, stream, tree, indent, &@"else".base, space); } else { return renderExpression(allocator, stream, tree, indent, if_node.body, space); @@ -1685,8 +1685,8 @@ const Space = enum { Newline, Comma, Space, + SpaceOrOutdent, NoNewline, - NoIndent, NoComment, }; @@ -1717,7 +1717,7 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent if (next_token.id != Token.Id.LineComment) { switch (space) { - Space.None, Space.NoNewline, Space.NoIndent => return, + Space.None, Space.NoNewline => return, Space.Newline => { if (next_token.id == Token.Id.MultilineStringLiteralLine) { return; @@ -1725,7 +1725,7 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent return stream.write("\n"); } }, - Space.Space => return stream.writeByte(' '), + Space.Space, Space.SpaceOrOutdent => return stream.writeByte(' '), Space.NoComment, Space.Comma => unreachable, } } @@ -1756,7 +1756,11 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent }; try stream.writeByteNTimes(' ', next_line_indent); }, - Space.Newline, Space.NoIndent => { + Space.SpaceOrOutdent => { + try stream.writeByte('\n'); + try stream.writeByteNTimes(' ', indent); + }, + Space.Newline => { if (next_token.id == Token.Id.MultilineStringLiteralLine) { return; } else { @@ -1783,7 +1787,7 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent next_token = tree.tokens.at(token_index + offset); if (next_token.id != Token.Id.LineComment) { switch (space) { - Space.Newline, Space.NoIndent => { + Space.Newline => { if (next_token.id == Token.Id.MultilineStringLiteralLine) { return; } else { @@ -1800,6 +1804,10 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent }; try stream.writeByteNTimes(' ', next_line_indent); }, + Space.SpaceOrOutdent => { + try stream.writeByte('\n'); + try stream.writeByteNTimes(' ', indent); + }, Space.NoNewline => {}, Space.NoComment, Space.Comma => unreachable, }