authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-17 00:31:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-17 00:31:47-04:00
log37c6afa5b4c25e008206d8a036e02b7fd7189459
tree6996dd5c0e146acf861a99b9aed5107878098238
parent9ea0e4ca6803cd10cfbffbdf80033a90f0e544d5

zig fmt: line comment between if block and else keyword


3 files changed, 45 insertions(+), 32 deletions(-)

std/zig/parse.zig+1
......@@ -1017,6 +1017,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
10171017 continue;
10181018 },
10191019 State.Else => |dest| {
1020 while (try eatLineComment(arena, &tok_it, &tree)) |_| { }
10201021 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
10211022 const node = try arena.construct(ast.Node.Else {
10221023 .base = ast.Node {.id = ast.Node.Id.Else },
std/zig/parser_test.zig+35-32
......@@ -1,3 +1,38 @@
1test "zig fmt: line comment between if block and else keyword" {
2 try testTransform(
3 \\test "aoeu" {
4 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
5 \\ if ((hx & 0x7fffffff) != 0x7f800000) {
6 \\ return Complex(f32).new(y - y, y - y);
7 \\ }
8 \\ // cexp(-inf +- i inf|nan) = 0 + i0
9 \\ else if (hx & 0x80000000 != 0) {
10 \\ return Complex(f32).new(0, 0);
11 \\ }
12 \\ // cexp(+inf +- i inf|nan) = inf + i nan
13 \\ // another comment
14 \\ else {
15 \\ return Complex(f32).new(x, y - y);
16 \\ }
17 \\}
18 ,
19 \\test "aoeu" {
20 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
21 \\ if ((hx & 0x7fffffff) != 0x7f800000) {
22 \\ return Complex(f32).new(y - y, y - y);
23 \\ } // cexp(-inf +- i inf|nan) = 0 + i0
24 \\ else if (hx & 0x80000000 != 0) {
25 \\ return Complex(f32).new(0, 0);
26 \\ } // cexp(+inf +- i inf|nan) = inf + i nan
27 \\ // another comment
28 \\ else {
29 \\ return Complex(f32).new(x, y - y);
30 \\ }
31 \\}
32 \\
33 );
34}
35
136test "zig fmt: same line comments in expression" {
237 try testCanonical(
338 \\test "aoeu" {
......@@ -9,38 +44,6 @@ test "zig fmt: same line comments in expression" {
944 );
1045}
1146
12//test "zig fmt: line comment between if block and else keyword" {
13// try testTransform(
14// test "aoeu" {
15// // cexp(finite|nan +- i inf|nan) = nan + i nan
16// if ((hx & 0x7fffffff) != 0x7f800000) {
17// return Complex(f32).new(y - y, y - y);
18// }
19// // cexp(-inf +- i inf|nan) = 0 + i0
20// else if (hx & 0x80000000 != 0) {
21// return Complex(f32).new(0, 0);
22// }
23// // cexp(+inf +- i inf|nan) = inf + i nan
24// else {
25// return Complex(f32).new(x, y - y);
26// }
27// }
28// ,
29// test "aoeu" {
30// // cexp(finite|nan +- i inf|nan) = nan + i nan
31// if ((hx & 0x7fffffff) != 0x7f800000) {
32// return Complex(f32).new(y - y, y - y);
33// } // cexp(-inf +- i inf|nan) = 0 + i0
34// else if (hx & 0x80000000 != 0) {
35// return Complex(f32).new(0, 0);
36// } // cexp(+inf +- i inf|nan) = inf + i nan
37// else {
38// return Complex(f32).new(x, y - y);
39// }
40// }
41// );
42//}
43
4447test "zig fmt: add comma on last switch prong" {
4548 try testTransform(
4649 \\test "aoeu" {
std/zig/render.zig+9
......@@ -852,6 +852,15 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
852852 },
853853 ast.Node.Id.Else => {
854854 const else_node = @fieldParentPtr(ast.Node.Else, "base", base);
855
856 var prev_tok_index = else_node.else_token - 1;
857 while (tree.tokens.at(prev_tok_index).id == Token.Id.LineComment) : (prev_tok_index -= 1) { }
858 prev_tok_index += 1;
859 while (prev_tok_index < else_node.else_token) : (prev_tok_index += 1) {
860 try stream.print("{}\n", tree.tokenSlice(prev_tok_index));
861 try stream.writeByteNTimes(' ', indent);
862 }
863
855864 try stream.print("{}", tree.tokenSlice(else_node.else_token));
856865
857866 const block_body = switch (else_node.body.id) {