Commit 37c6afa5b4
Changed files (3)
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 },
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" {
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) {