authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-03 21:51:23-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-03 21:51:23-04:00
log6b27290c681e3cc2acf71d2ef2d78830013bbccb
treef3080130c5634782d55b7b771da53c90aa0708b9
parentce14c543d165efbd926ea6bd654d999c625b366f
parent8a7e2e3479aaeb4069637ef5913e44df30bd45f6
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'Vexu-comment-in-array'


2 files changed, 122 insertions(+), 9 deletions(-)

std/zig/parser_test.zig+88
...@@ -2419,6 +2419,94 @@ test "zig fmt: comment after empty comment" {...@@ -2419,6 +2419,94 @@ test "zig fmt: comment after empty comment" {
2419 );2419 );
2420}2420}
24212421
2422test "zig fmt: line comment in array" {
2423 try testTransform(
2424 \\test "a" {
2425 \\ var arr = [_]u32{
2426 \\ 0
2427 \\ // 1,
2428 \\ // 2,
2429 \\ };
2430 \\}
2431 \\
2432 ,
2433 \\test "a" {
2434 \\ var arr = [_]u32{
2435 \\ 0, // 1,
2436 \\ // 2,
2437 \\ };
2438 \\}
2439 \\
2440 );
2441 try testCanonical(
2442 \\test "a" {
2443 \\ var arr = [_]u32{
2444 \\ 0,
2445 \\ // 1,
2446 \\ // 2,
2447 \\ };
2448 \\}
2449 \\
2450 );
2451}
2452
2453test "zig fmt: comment after params" {
2454 try testTransform(
2455 \\fn a(
2456 \\ b: u32
2457 \\ // c: u32,
2458 \\ // d: u32,
2459 \\) void {}
2460 \\
2461 ,
2462 \\fn a(
2463 \\ b: u32, // c: u32,
2464 \\ // d: u32,
2465 \\) void {}
2466 \\
2467 );
2468 try testCanonical(
2469 \\fn a(
2470 \\ b: u32,
2471 \\ // c: u32,
2472 \\ // d: u32,
2473 \\) void {}
2474 \\
2475 );
2476}
2477
2478test "zig fmt: comment in array initializer/access" {
2479 try testCanonical(
2480 \\test "a" {
2481 \\ var a = x{ //aa
2482 \\ //bb
2483 \\ };
2484 \\ var a = []x{ //aa
2485 \\ //bb
2486 \\ };
2487 \\ var b = [ //aa
2488 \\ _
2489 \\ ]x{ //aa
2490 \\ //bb
2491 \\ 9,
2492 \\ };
2493 \\ var c = b[ //aa
2494 \\ 0
2495 \\ ];
2496 \\ var d = [_
2497 \\ //aa
2498 \\ ]x{ //aa
2499 \\ //bb
2500 \\ 9,
2501 \\ };
2502 \\ var e = d[0
2503 \\ //aa
2504 \\ ];
2505 \\}
2506 \\
2507 );
2508}
2509
2422test "zig fmt: comments at several places in struct init" {2510test "zig fmt: comments at several places in struct init" {
2423 try testTransform(2511 try testTransform(
2424 \\var bar = Bar{2512 \\var bar = Bar{
std/zig/render.zig+34-9
...@@ -483,9 +483,23 @@ fn renderExpression(...@@ -483,9 +483,23 @@ fn renderExpression(
483 },483 },
484484
485 ast.Node.PrefixOp.Op.ArrayType => |array_index| {485 ast.Node.PrefixOp.Op.ArrayType => |array_index| {
486 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // [486 const lbracket = prefix_op_node.op_token;
487 try renderExpression(allocator, stream, tree, indent, start_col, array_index, Space.None);487 const rbracket = tree.nextToken(array_index.lastToken());
488 try renderToken(tree, stream, tree.nextToken(array_index.lastToken()), indent, start_col, Space.None); // ]488
489 try renderToken(tree, stream, lbracket, indent, start_col, Space.None); // [
490
491 const starts_with_comment = tree.tokens.at(lbracket + 1).id == .LineComment;
492 const ends_with_comment = tree.tokens.at(rbracket - 1).id == .LineComment;
493 const new_indent = if (ends_with_comment) indent + indent_delta else indent;
494 const new_space = if (ends_with_comment) Space.Newline else Space.None;
495 try renderExpression(allocator, stream, tree, new_indent, start_col, array_index, new_space);
496 if (starts_with_comment) {
497 try stream.writeByte('\n');
498 }
499 if (ends_with_comment or starts_with_comment) {
500 try stream.writeByteNTimes(' ', indent);
501 }
502 try renderToken(tree, stream, rbracket, indent, start_col, Space.None); // ]
489 },503 },
490 ast.Node.PrefixOp.Op.BitNot,504 ast.Node.PrefixOp.Op.BitNot,
491 ast.Node.PrefixOp.Op.BoolNot,505 ast.Node.PrefixOp.Op.BoolNot,
...@@ -580,7 +594,18 @@ fn renderExpression(...@@ -580,7 +594,18 @@ fn renderExpression(
580594
581 try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs, Space.None);595 try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs, Space.None);
582 try renderToken(tree, stream, lbracket, indent, start_col, Space.None); // [596 try renderToken(tree, stream, lbracket, indent, start_col, Space.None); // [
583 try renderExpression(allocator, stream, tree, indent, start_col, index_expr, Space.None);597
598 const starts_with_comment = tree.tokens.at(lbracket + 1).id == .LineComment;
599 const ends_with_comment = tree.tokens.at(rbracket - 1).id == .LineComment;
600 const new_indent = if (ends_with_comment) indent + indent_delta else indent;
601 const new_space = if (ends_with_comment) Space.Newline else Space.None;
602 try renderExpression(allocator, stream, tree, new_indent, start_col, index_expr, new_space);
603 if (starts_with_comment) {
604 try stream.writeByte('\n');
605 }
606 if (ends_with_comment or starts_with_comment) {
607 try stream.writeByteNTimes(' ', indent);
608 }
584 return renderToken(tree, stream, rbracket, indent, start_col, space); // ]609 return renderToken(tree, stream, rbracket, indent, start_col, space); // ]
585 },610 },
586611
...@@ -615,7 +640,7 @@ fn renderExpression(...@@ -615,7 +640,7 @@ fn renderExpression(
615640
616 if (field_inits.len == 0) {641 if (field_inits.len == 0) {
617 try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs, Space.None);642 try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs, Space.None);
618 try renderToken(tree, stream, lbrace, indent, start_col, Space.None);643 try renderToken(tree, stream, lbrace, indent + indent_delta, start_col, Space.None);
619 return renderToken(tree, stream, suffix_op.rtoken, indent, start_col, space);644 return renderToken(tree, stream, suffix_op.rtoken, indent, start_col, space);
620 }645 }
621646
...@@ -714,7 +739,7 @@ fn renderExpression(...@@ -714,7 +739,7 @@ fn renderExpression(
714 try renderToken(tree, stream, lbrace, indent, start_col, Space.None);739 try renderToken(tree, stream, lbrace, indent, start_col, Space.None);
715 return renderToken(tree, stream, suffix_op.rtoken, indent, start_col, space);740 return renderToken(tree, stream, suffix_op.rtoken, indent, start_col, space);
716 }741 }
717 if (exprs.len == 1) {742 if (exprs.len == 1 and tree.tokens.at(exprs.at(0).*.lastToken() + 1).id == .RBrace) {
718 const expr = exprs.at(0).*;743 const expr = exprs.at(0).*;
719744
720 try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs, Space.None);745 try renderExpression(allocator, stream, tree, indent, start_col, suffix_op.lhs, Space.None);
...@@ -775,7 +800,7 @@ fn renderExpression(...@@ -775,7 +800,7 @@ fn renderExpression(
775 while (it.next()) |expr| : (i += 1) {800 while (it.next()) |expr| : (i += 1) {
776 counting_stream.bytes_written = 0;801 counting_stream.bytes_written = 0;
777 var dummy_col: usize = 0;802 var dummy_col: usize = 0;
778 try renderExpression(allocator, &counting_stream.stream, tree, 0, &dummy_col, expr.*, Space.None);803 try renderExpression(allocator, &counting_stream.stream, tree, indent, &dummy_col, expr.*, Space.None);
779 const width = @intCast(usize, counting_stream.bytes_written);804 const width = @intCast(usize, counting_stream.bytes_written);
780 const col = i % row_size;805 const col = i % row_size;
781 column_widths[col] = std.math.max(column_widths[col], width);806 column_widths[col] = std.math.max(column_widths[col], width);
...@@ -1191,8 +1216,8 @@ fn renderExpression(...@@ -1191,8 +1216,8 @@ fn renderExpression(
1191 });1216 });
11921217
1193 const src_params_trailing_comma = blk: {1218 const src_params_trailing_comma = blk: {
1194 const maybe_comma = tree.prevToken(rparen);1219 const maybe_comma = tree.tokens.at(rparen - 1).id;
1195 break :blk tree.tokens.at(maybe_comma).id == Token.Id.Comma;1220 break :blk maybe_comma == .Comma or maybe_comma == .LineComment;
1196 };1221 };
11971222
1198 if (!src_params_trailing_comma) {1223 if (!src_params_trailing_comma) {