Skip to content

Commit 1aacedf

Browse files
zig fmt: Fix regression in ArrayInitializers
1 parent 40b6e86 commit 1aacedf

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

lib/std/zig/parser_test.zig

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,9 +3516,13 @@ test "zig fmt: multiline string literals should play nice with array initializer
35163516
\\ .{(
35173517
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
35183518
\\ )},
3519-
\\ .{ "xxxxxxx", "xxx", (
3520-
\\ \\ xxx
3521-
\\ ), "xxx", "xxx" },
3519+
\\ .{
3520+
\\ "xxxxxxx", "xxx",
3521+
\\ (
3522+
\\ \\ xxx
3523+
\\ ),
3524+
\\ "xxx", "xxx",
3525+
\\ },
35223526
\\ .{ "xxxxxxx", "xxx", "xxx", "xxx" }, .{ "xxxxxxx", "xxx", "xxx", "xxx" },
35233527
\\ "aaaaaaa", "bbbbbb", "ccccc", // -
35243528
\\ "dddd", ("eee"), ("fff"),
@@ -3601,6 +3605,37 @@ test "zig fmt: single argument trailing commas in @builtins()" {
36013605
);
36023606
}
36033607

3608+
test "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+
3623+
test "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+
36043639
const std = @import("std");
36053640
const mem = std.mem;
36063641
const warn = std.debug.warn;

lib/std/zig/render.zig

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -733,14 +733,14 @@ fn renderExpression(
733733
}
734734

735735
// scan to find row size
736-
if (rowSize(tree, exprs, rtoken, false) != null) {
736+
if (rowSize(tree, exprs, rtoken) != null) {
737737
{
738738
ais.pushIndentNextLine();
739739
defer ais.popIndent();
740740
try renderToken(tree, ais, lbrace, Space.Newline);
741741

742742
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| {
744744
const row_exprs = exprs[expr_index..];
745745
// A place to store the width of each expression and its column's maximum
746746
var widths = try allocator.alloc(usize, row_exprs.len + row_size);
@@ -757,14 +757,14 @@ fn renderExpression(
757757
// Find next row with trailing comment (if any) to end the current section
758758
var section_end = sec_end: {
759759
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);
761761
for (row_exprs) |expr, i| {
762762
// Ignore comment on first line of this section
763763
if (i == 0 or tree.tokensOnSameLine(row_exprs[0].firstToken(), expr.lastToken())) continue;
764764
// Track start of line containing comment
765765
if (!tree.tokensOnSameLine(row_exprs[this_line_first_expr].firstToken(), expr.lastToken())) {
766766
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);
768768
}
769769

770770
const maybe_comma = expr.lastToken() + 1;
@@ -860,7 +860,7 @@ fn renderExpression(
860860
continue;
861861
}
862862
}
863-
if (single_line) {
863+
if (single_line and row_size != 1) {
864864
try renderToken(tree, ais, comma, Space.Space); // ,
865865
continue;
866866
}
@@ -1087,7 +1087,8 @@ fn renderExpression(
10871087
const params = call.params();
10881088
for (params) |param_node, i| {
10891089
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) {
10911092
ais.pushIndentOneShot();
10921093
}
10931094

@@ -2629,7 +2630,16 @@ fn copyFixingWhitespace(ais: anytype, slice: []const u8) @TypeOf(ais.*).Error!vo
26292630
};
26302631
}
26312632

2632-
fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex, force: bool) ?usize {
2633+
fn 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+
26332643
var count: usize = 1;
26342644
for (exprs) |expr, i| {
26352645
if (i + 1 < exprs.len) {
@@ -2638,21 +2648,6 @@ fn rowSize(tree: *ast.Tree, exprs: []*ast.Node, rtoken: ast.TokenIndex, force: b
26382648
if (loc.line != 0) return count;
26392649
count += 1;
26402650
} 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-
}
26562651
return count;
26572652
}
26582653
}

0 commit comments

Comments
 (0)