zig fmt: fix extra whitespace in StructInit with multiline strings

68d2f68ed introduced special handling for StructInit fields
containing multiline strings to prevent inserting whitespace after =.
However, this logic didn't handle cases without a trailing comma,
which resulted in unwanted trailing whitespace.
This commit is contained in:
skewb1k 2025-11-03 03:29:16 +03:00 committed by Veikka Tuominen
parent a6d444c271
commit 26db54d69b
4 changed files with 102 additions and 91 deletions

View file

@ -2056,7 +2056,8 @@ fn renderStructInit(
const init_token = tree.firstToken(field_init);
try renderToken(r, init_token - 3, .none); // .
try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
try renderToken(r, init_token - 1, .space); // =
const space_after_equal: Space = if (tree.nodeTag(field_init) == .multiline_string_literal) .none else .space;
try renderToken(r, init_token - 1, space_after_equal); // =
try renderExpressionFixup(r, field_init, .comma_space);
}
}

View file

@ -5719,6 +5719,16 @@ test "zig fmt: no space before newline before multiline string" {
\\ ,
\\ };
\\ _ = s2;
\\ const s3 = .{ .text =
\\ \\hello
\\ \\world
\\ , .comment = "test" };
\\ _ = s3;
\\ const s4 = .{ .comment = "test", .text =
\\ \\hello
\\ \\world
\\ };
\\ _ = s4;
\\}
\\
);