From 37c6afa5b4c25e008206d8a036e02b7fd7189459 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 May 2018 00:31:47 -0400 Subject: [PATCH] zig fmt: line comment between if block and else keyword --- std/zig/parse.zig | 1 + std/zig/parser_test.zig | 67 +++++++++++++++++++++-------------------- std/zig/render.zig | 9 ++++++ 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/std/zig/parse.zig b/std/zig/parse.zig index 4f1e3dcefe..094d368c23 100644 --- a/std/zig/parse.zig +++ b/std/zig/parse.zig @@ -1017,6 +1017,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree { continue; }, State.Else => |dest| { + while (try eatLineComment(arena, &tok_it, &tree)) |_| { } if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| { const node = try arena.construct(ast.Node.Else { .base = ast.Node {.id = ast.Node.Id.Else }, diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index ea26f823b5..38e61fe8fe 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -1,3 +1,38 @@ +test "zig fmt: line comment between if block and else keyword" { + try testTransform( + \\test "aoeu" { + \\ // 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 + \\ // another comment + \\ else { + \\ return Complex(f32).new(x, y - y); + \\ } + \\} + , + \\test "aoeu" { + \\ // 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 + \\ // another comment + \\ else { + \\ return Complex(f32).new(x, y - y); + \\ } + \\} + \\ + ); +} + test "zig fmt: same line comments in expression" { try testCanonical( \\test "aoeu" { @@ -9,38 +44,6 @@ test "zig fmt: same line comments in expression" { ); } -//test "zig fmt: line comment between if block and else keyword" { -// try testTransform( -// test "aoeu" { -// // 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 "aoeu" { -// // 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: add comma on last switch prong" { try testTransform( \\test "aoeu" { diff --git a/std/zig/render.zig b/std/zig/render.zig index 9ce8ba0cbf..980addc77a 100644 --- a/std/zig/render.zig +++ b/std/zig/render.zig @@ -852,6 +852,15 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind }, ast.Node.Id.Else => { const else_node = @fieldParentPtr(ast.Node.Else, "base", base); + + var prev_tok_index = else_node.else_token - 1; + while (tree.tokens.at(prev_tok_index).id == Token.Id.LineComment) : (prev_tok_index -= 1) { } + prev_tok_index += 1; + while (prev_tok_index < else_node.else_token) : (prev_tok_index += 1) { + try stream.print("{}\n", tree.tokenSlice(prev_tok_index)); + try stream.writeByteNTimes(' ', indent); + } + try stream.print("{}", tree.tokenSlice(else_node.else_token)); const block_body = switch (else_node.body.id) {