diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index a526e1fa67..1205dbadab 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -286,7 +286,7 @@ test "zig fmt: respect line breaks after var declarations" { \\ lookup_tables[6][@truncate(u8, self.crc >> 8)] ^ \\ lookup_tables[7][@truncate(u8, self.crc >> 0)]; \\ - ); + ); } test "zig fmt: multiline string mixed with comments" { @@ -563,6 +563,24 @@ test "zig fmt: anon literal in array" { ); } +test "zig fmt: alignment in anonymous literal" { + try testTransform( + \\const a = .{ + \\ "U", "L", "F", + \\ "U'", + \\ "L'", + \\ "F'", + \\}; + \\ + , + \\const a = .{ + \\ "U", "L", "F", + \\ "U'", "L'", "F'", + \\}; + \\ + ); +} + test "zig fmt: anon struct literal syntax" { try testCanonical( \\const x = .{ diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 3c026e5f21..f73979aa6b 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -813,12 +813,10 @@ fn renderExpression( const expr_last_token = expr.*.lastToken() + 1; const next_expr = section_exprs[i + 1]; const loc = tree.tokenLocation(tree.token_locs[expr_last_token].start, next_expr.*.firstToken()); - if (loc.line == 0) { - column_counter += 1; - } else { - single_line = false; - column_counter = 0; - } + + column_counter += 1; + + if (loc.line != 0) single_line = false; } else { single_line = false; column_counter = 0; @@ -2655,6 +2653,8 @@ fn copyFixingWhitespace(ais: anytype, slice: []const u8) @TypeOf(ais.*).Error!vo }; } +// Returns the number of nodes in `expr` that are on the same line as `rtoken`, +// or null if they all are on the same line. fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex) ?usize { const first_token = exprs[0].firstToken(); const first_loc = tree.tokenLocation(tree.token_locs[first_token].start, rtoken);