authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-10 23:26:38+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-11 00:40:05-07:00
log00664dbdcea9d17617ec78c6933f627f0661f81e
treed53959843d2d1a042b965da6bd32d1f79be4d1f8
parentc9bc8b9d0cf1ca7d21ddb60d08a4cdd0730a253d

zig fmt: Fix alignment of initializer elements

Resetting `column_counter` is not needed as the effective column number is calculated by taking that value modulo `row_size`. Closes #7289

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

lib/std/zig/parser_test.zig+19-1
...@@ -286,7 +286,7 @@ test "zig fmt: respect line breaks after var declarations" {...@@ -286,7 +286,7 @@ test "zig fmt: respect line breaks after var declarations" {
286 \\ lookup_tables[6][@truncate(u8, self.crc >> 8)] ^286 \\ lookup_tables[6][@truncate(u8, self.crc >> 8)] ^
287 \\ lookup_tables[7][@truncate(u8, self.crc >> 0)];287 \\ lookup_tables[7][@truncate(u8, self.crc >> 0)];
288 \\288 \\
289 ); 289 );
290}290}
291291
292test "zig fmt: multiline string mixed with comments" {292test "zig fmt: multiline string mixed with comments" {
...@@ -563,6 +563,24 @@ test "zig fmt: anon literal in array" {...@@ -563,6 +563,24 @@ test "zig fmt: anon literal in array" {
563 );563 );
564}564}
565565
566test "zig fmt: alignment in anonymous literal" {
567 try testTransform(
568 \\const a = .{
569 \\ "U", "L", "F",
570 \\ "U'",
571 \\ "L'",
572 \\ "F'",
573 \\};
574 \\
575 ,
576 \\const a = .{
577 \\ "U", "L", "F",
578 \\ "U'", "L'", "F'",
579 \\};
580 \\
581 );
582}
583
566test "zig fmt: anon struct literal syntax" {584test "zig fmt: anon struct literal syntax" {
567 try testCanonical(585 try testCanonical(
568 \\const x = .{586 \\const x = .{
lib/std/zig/render.zig+6-6
...@@ -813,12 +813,10 @@ fn renderExpression(...@@ -813,12 +813,10 @@ fn renderExpression(
813 const expr_last_token = expr.*.lastToken() + 1;813 const expr_last_token = expr.*.lastToken() + 1;
814 const next_expr = section_exprs[i + 1];814 const next_expr = section_exprs[i + 1];
815 const loc = tree.tokenLocation(tree.token_locs[expr_last_token].start, next_expr.*.firstToken());815 const loc = tree.tokenLocation(tree.token_locs[expr_last_token].start, next_expr.*.firstToken());
816 if (loc.line == 0) {816
817 column_counter += 1;817 column_counter += 1;
818 } else {818
819 single_line = false;819 if (loc.line != 0) single_line = false;
820 column_counter = 0;
821 }
822 } else {820 } else {
823 single_line = false;821 single_line = false;
824 column_counter = 0;822 column_counter = 0;
...@@ -2655,6 +2653,8 @@ fn copyFixingWhitespace(ais: anytype, slice: []const u8) @TypeOf(ais.*).Error!vo...@@ -2655,6 +2653,8 @@ fn copyFixingWhitespace(ais: anytype, slice: []const u8) @TypeOf(ais.*).Error!vo
2655 };2653 };
2656}2654}
26572655
2656// Returns the number of nodes in `expr` that are on the same line as `rtoken`,
2657// or null if they all are on the same line.
2658fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex) ?usize {2658fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex) ?usize {
2659 const first_token = exprs[0].firstToken();2659 const first_token = exprs[0].firstToken();
2660 const first_loc = tree.tokenLocation(tree.token_locs[first_token].start, rtoken);2660 const first_loc = tree.tokenLocation(tree.token_locs[first_token].start, rtoken);