authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-09 10:29:39+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-09 10:03:45-07:00
log5ebcd8ccafeab0d8ddbdb9723f7e7a50ecc60a5f
tree68e60d45952d665878c169ef84d46a01e021be99
parentcc525bc002e456cc735f66bc4d2c5267a10b6016

zig fmt: Fix rendering of arrays with single row

rowSize used to return null if all the elements were placed on the same line as the right brace, making the rendering logic skip the whole set of elements. Given the usage of rowSize let's just drop the null and always return the number of elements. Fixes #8423

2 files changed, 11 insertions(+), 6 deletions(-)

lib/std/zig/parser_test.zig+5
......@@ -1812,6 +1812,8 @@ test "zig fmt: array literal veritical column alignment" {
18121812 \\ 4,5,600,7,
18131813 \\ 80,
18141814 \\ 9, 10, 11, 0, 13, 14, 15};
1815 \\const a = [12]u8{
1816 \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
18151817 \\
18161818 ,
18171819 \\const a = []u8{
......@@ -1825,6 +1827,9 @@ test "zig fmt: array literal veritical column alignment" {
18251827 \\ 9, 10, 11, 0, 13,
18261828 \\ 14, 15,
18271829 \\};
1830 \\const a = [12]u8{
1831 \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
1832 \\};
18281833 \\
18291834 );
18301835}
lib/std/zig/render.zig+6-6
......@@ -1653,7 +1653,8 @@ fn renderArrayInit(
16531653 try renderToken(ais, tree, array_init.ast.lbrace, .newline);
16541654
16551655 var expr_index: usize = 0;
1656 while (rowSize(tree, array_init.ast.elements[expr_index..], rbrace)) |row_size| {
1656 while (true) {
1657 const row_size = rowSize(tree, array_init.ast.elements[expr_index..], rbrace);
16571658 const row_exprs = array_init.ast.elements[expr_index..];
16581659 // A place to store the width of each expression and its column's maximum
16591660 const widths = try gpa.alloc(usize, row_exprs.len + row_size);
......@@ -1686,7 +1687,7 @@ fn renderArrayInit(
16861687 const maybe_comma = expr_last_token + 1;
16871688 if (token_tags[maybe_comma] == .comma) {
16881689 if (hasSameLineComment(tree, maybe_comma))
1689 break :sec_end i - this_line_size.? + 1;
1690 break :sec_end i - this_line_size + 1;
16901691 }
16911692 }
16921693 break :sec_end row_exprs.len;
......@@ -2500,9 +2501,8 @@ fn nodeCausesSliceOpSpace(tag: ast.Node.Tag) bool {
25002501 };
25012502}
25022503
2503// Returns the number of nodes in `expr` that are on the same line as `rtoken`,
2504// or null if they all are on the same line.
2505fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex) ?usize {
2504// Returns the number of nodes in `expr` that are on the same line as `rtoken`.
2505fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex) usize {
25062506 const token_tags = tree.tokens.items(.tag);
25072507
25082508 const first_token = tree.firstToken(exprs[0]);
......@@ -2510,7 +2510,7 @@ fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex
25102510 const maybe_comma = rtoken - 1;
25112511 if (token_tags[maybe_comma] == .comma)
25122512 return 1;
2513 return null; // no newlines
2513 return exprs.len; // no newlines
25142514 }
25152515
25162516 var count: usize = 1;