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 {...@@ -1017,6 +1017,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1017 continue;1017 continue;
1018 },1018 },
1019 State.Else => |dest| {1019 State.Else => |dest| {
1020 while (try eatLineComment(arena, &tok_it, &tree)) |_| { }
1020 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {1021 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
1021 const node = try arena.construct(ast.Node.Else {1022 const node = try arena.construct(ast.Node.Else {
1022 .base = ast.Node {.id = ast.Node.Id.Else },1023 .base = ast.Node {.id = ast.Node.Id.Else },
std/zig/parser_test.zig+35-32
...@@ -1,3 +1,38 @@...@@ -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
1test "zig fmt: same line comments in expression" {36test "zig fmt: same line comments in expression" {
2 try testCanonical(37 try testCanonical(
3 \\test "aoeu" {38 \\test "aoeu" {
...@@ -9,38 +44,6 @@ test "zig fmt: same line comments in expression" {...@@ -9,38 +44,6 @@ test "zig fmt: same line comments in expression" {
9 );44 );
10}45}
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
44test "zig fmt: add comma on last switch prong" {47test "zig fmt: add comma on last switch prong" {
45 try testTransform(48 try testTransform(
46 \\test "aoeu" {49 \\test "aoeu" {
std/zig/render.zig+9
...@@ -852,6 +852,15 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -852,6 +852,15 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
852 },852 },
853 ast.Node.Id.Else => {853 ast.Node.Id.Else => {
854 const else_node = @fieldParentPtr(ast.Node.Else, "base", base);854 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
855 try stream.print("{}", tree.tokenSlice(else_node.else_token));864 try stream.print("{}", tree.tokenSlice(else_node.else_token));
856865
857 const block_body = switch (else_node.body.id) {866 const block_body = switch (else_node.body.id) {