authorgravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-09-10 23:35:18+10:00
committergravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-09-18 20:34:00+10:00
log1aacedf6e197ea212025dccad622894a44eb5461
tree753937d51842652ab0e927a1cc63692ba00cb81b
parent40b6e86a999ce80b9f71c7a88df6186400f151ac

zig fmt: Fix regression in ArrayInitializers


2 files changed, 55 insertions(+), 25 deletions(-)

lib/std/zig/parser_test.zig+38-3
...@@ -3516,9 +3516,13 @@ test "zig fmt: multiline string literals should play nice with array initializer...@@ -3516,9 +3516,13 @@ test "zig fmt: multiline string literals should play nice with array initializer
3516 \\ .{(3516 \\ .{(
3517 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3517 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3518 \\ )},3518 \\ )},
3519 \\ .{ "xxxxxxx", "xxx", (3519 \\ .{
3520 \\ \\ xxx3520 \\ "xxxxxxx", "xxx",
3521 \\ ), "xxx", "xxx" },3521 \\ (
3522 \\ \\ xxx
3523 \\ ),
3524 \\ "xxx", "xxx",
3525 \\ },
3522 \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" }, .{ "xxxxxxx", "xxx", "xxx", "xxx" },3526 \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" }, .{ "xxxxxxx", "xxx", "xxx", "xxx" },
3523 \\ "aaaaaaa", "bbbbbb", "ccccc", // -3527 \\ "aaaaaaa", "bbbbbb", "ccccc", // -
3524 \\ "dddd", ("eee"), ("fff"),3528 \\ "dddd", ("eee"), ("fff"),
...@@ -3601,6 +3605,37 @@ test "zig fmt: single argument trailing commas in @builtins()" {...@@ -3601,6 +3605,37 @@ test "zig fmt: single argument trailing commas in @builtins()" {
3601 );3605 );
3602}3606}
36033607
3608test "zig fmt: trailing comma should force multiline 1 column" {
3609 try testTransform(
3610 \\pub const UUID_NULL: uuid_t = [16]u8{0,0,0,0,};
3611 \\
3612 ,
3613 \\pub const UUID_NULL: uuid_t = [16]u8{
3614 \\ 0,
3615 \\ 0,
3616 \\ 0,
3617 \\ 0,
3618 \\};
3619 \\
3620 );
3621}
3622
3623test "zig fmt: function params should align nicely" {
3624 try testCanonical(
3625 \\pub fn foo() void {
3626 \\ cases.addRuntimeSafety("slicing operator with sentinel",
3627 \\ \\const std = @import("std");
3628 \\ ++ check_panic_msg ++
3629 \\ \\pub fn main() void {
3630 \\ \\ var buf = [4]u8{'a','b','c',0};
3631 \\ \\ const slice = buf[0..:0];
3632 \\ \\}
3633 \\ );
3634 \\}
3635 \\
3636 );
3637}
3638
3604const std = @import("std");3639const std = @import("std");
3605const mem = std.mem;3640const mem = std.mem;
3606const warn = std.debug.warn;3641const warn = std.debug.warn;
lib/std/zig/render.zig+17-22
...@@ -733,14 +733,14 @@ fn renderExpression(...@@ -733,14 +733,14 @@ fn renderExpression(
733 }733 }
734734
735 // scan to find row size735 // scan to find row size
736 if (rowSize(tree, exprs, rtoken, false) != null) {736 if (rowSize(tree, exprs, rtoken) != null) {
737 {737 {
738 ais.pushIndentNextLine();738 ais.pushIndentNextLine();
739 defer ais.popIndent();739 defer ais.popIndent();
740 try renderToken(tree, ais, lbrace, Space.Newline);740 try renderToken(tree, ais, lbrace, Space.Newline);
741741
742 var expr_index: usize = 0;742 var expr_index: usize = 0;
743 while (rowSize(tree, exprs[expr_index..], rtoken, true)) |row_size| {743 while (rowSize(tree, exprs[expr_index..], rtoken)) |row_size| {
744 const row_exprs = exprs[expr_index..];744 const row_exprs = exprs[expr_index..];
745 // A place to store the width of each expression and its column's maximum745 // A place to store the width of each expression and its column's maximum
746 var widths = try allocator.alloc(usize, row_exprs.len + row_size);746 var widths = try allocator.alloc(usize, row_exprs.len + row_size);
...@@ -757,14 +757,14 @@ fn renderExpression(...@@ -757,14 +757,14 @@ fn renderExpression(
757 // Find next row with trailing comment (if any) to end the current section757 // Find next row with trailing comment (if any) to end the current section
758 var section_end = sec_end: {758 var section_end = sec_end: {
759 var this_line_first_expr: usize = 0;759 var this_line_first_expr: usize = 0;
760 var this_line_size = rowSize(tree, row_exprs, rtoken, true);760 var this_line_size = rowSize(tree, row_exprs, rtoken);
761 for (row_exprs) |expr, i| {761 for (row_exprs) |expr, i| {
762 // Ignore comment on first line of this section762 // Ignore comment on first line of this section
763 if (i == 0 or tree.tokensOnSameLine(row_exprs[0].firstToken(), expr.lastToken())) continue;763 if (i == 0 or tree.tokensOnSameLine(row_exprs[0].firstToken(), expr.lastToken())) continue;
764 // Track start of line containing comment764 // Track start of line containing comment
765 if (!tree.tokensOnSameLine(row_exprs[this_line_first_expr].firstToken(), expr.lastToken())) {765 if (!tree.tokensOnSameLine(row_exprs[this_line_first_expr].firstToken(), expr.lastToken())) {
766 this_line_first_expr = i;766 this_line_first_expr = i;
767 this_line_size = rowSize(tree, row_exprs[this_line_first_expr..], rtoken, true);767 this_line_size = rowSize(tree, row_exprs[this_line_first_expr..], rtoken);
768 }768 }
769769
770 const maybe_comma = expr.lastToken() + 1;770 const maybe_comma = expr.lastToken() + 1;
...@@ -860,7 +860,7 @@ fn renderExpression(...@@ -860,7 +860,7 @@ fn renderExpression(
860 continue;860 continue;
861 }861 }
862 }862 }
863 if (single_line) {863 if (single_line and row_size != 1) {
864 try renderToken(tree, ais, comma, Space.Space); // ,864 try renderToken(tree, ais, comma, Space.Space); // ,
865 continue;865 continue;
866 }866 }
...@@ -1087,7 +1087,8 @@ fn renderExpression(...@@ -1087,7 +1087,8 @@ fn renderExpression(
1087 const params = call.params();1087 const params = call.params();
1088 for (params) |param_node, i| {1088 for (params) |param_node, i| {
1089 const maybe_comment = param_node.firstToken() - 1;1089 const maybe_comment = param_node.firstToken() - 1;
1090 if (param_node.*.tag == .MultilineStringLiteral or tree.token_ids[maybe_comment] == .LineComment) {1090 const maybe_multiline_string = param_node.firstToken();
1091 if (tree.token_ids[maybe_multiline_string] == .MultilineStringLiteralLine or tree.token_ids[maybe_comment] == .LineComment) {
1091 ais.pushIndentOneShot();1092 ais.pushIndentOneShot();
1092 }1093 }
10931094
...@@ -2629,7 +2630,16 @@ fn copyFixingWhitespace(ais: anytype, slice: []const u8) @TypeOf(ais.*).Error!vo...@@ -2629,7 +2630,16 @@ fn copyFixingWhitespace(ais: anytype, slice: []const u8) @TypeOf(ais.*).Error!vo
2629 };2630 };
2630}2631}
26312632
2632fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex, force: bool) ?usize {2633fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex) ?usize {
2634 const first_token = exprs[0].firstToken();
2635 const first_loc = tree.tokenLocation(tree.token_locs[first_token].start, rtoken);
2636 if (first_loc.line == 0) {
2637 const maybe_comma = tree.prevToken(rtoken);
2638 if (tree.token_ids[maybe_comma] == .Comma)
2639 return 1;
2640 return null; // no newlines
2641 }
2642
2633 var count: usize = 1;2643 var count: usize = 1;
2634 for (exprs) |expr, i| {2644 for (exprs) |expr, i| {
2635 if (i + 1 < exprs.len) {2645 if (i + 1 < exprs.len) {
...@@ -2638,21 +2648,6 @@ fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex, force: b...@@ -2638,21 +2648,6 @@ fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex, force: b
2638 if (loc.line != 0) return count;2648 if (loc.line != 0) return count;
2639 count += 1;2649 count += 1;
2640 } else {2650 } else {
2641 if (force) return count;
2642 const expr_last_token = expr.lastToken();
2643 const loc = tree.tokenLocation(tree.token_locs[expr_last_token].start, rtoken);
2644 if (loc.line == 0) {
2645 // all on one line
2646 const src_has_trailing_comma = trailblk: {
2647 const maybe_comma = tree.prevToken(rtoken);
2648 break :trailblk tree.token_ids[maybe_comma] == .Comma;
2649 };
2650 if (src_has_trailing_comma) {
2651 return 1; // force row size 1
2652 } else {
2653 return null; // no newlines
2654 }
2655 }
2656 return count;2651 return count;
2657 }2652 }
2658 }2653 }