authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-03-16 09:22:16+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-03-16 09:22:16+01:00
log5ecf8bddaeebf26ac6cd174f5f79e02ca243e501
treed4243fd724718867680c6b98e2731bb5257760f9
parentf16f25047c511cc5f468da57292a968555c4b791
signature Signed by PGP key 4AEE18F83AFDEB23

zig fmt: Respect line breaks in struct default value decls

Bring this in line with how variable declarations are handled. Open a new indentation level for the initialization expression to handle nested expressions like blocks. Closes #7618

2 files changed, 48 insertions(+), 2 deletions(-)

lib/std/zig/parser_test.zig+25
......@@ -4,6 +4,31 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66
7test "zig fmt: respect line breaks in struct field value declaration" {
8 try testCanonical(
9 \\const Foo = struct {
10 \\ bar: u32 =
11 \\ 42,
12 \\ bar: u32 =
13 \\ // a comment
14 \\ 42,
15 \\ bar: u32 =
16 \\ 42,
17 \\ // a comment
18 \\ bar: []const u8 =
19 \\ \\ foo
20 \\ \\ bar
21 \\ \\ baz
22 \\ ,
23 \\ bar: u32 =
24 \\ blk: {
25 \\ break :blk 42;
26 \\ },
27 \\};
28 \\
29 );
30}
31
732// TODO Remove this after zig 0.9.0 is released.
833test "zig fmt: rewrite inline functions as callconv(.Inline)" {
934 try testTransform(
lib/std/zig/render.zig+23-2
......@@ -1159,8 +1159,29 @@ fn renderContainerField(
11591159 try renderToken(ais, tree, rparen_token, .space); // )
11601160 }
11611161 const eq_token = tree.firstToken(field.ast.value_expr) - 1;
1162 try renderToken(ais, tree, eq_token, .space); // =
1163 return renderExpressionComma(gpa, ais, tree, field.ast.value_expr, space); // value
1162 const eq_space: Space = if (tree.tokensOnSameLine(eq_token, eq_token + 1)) .space else .newline;
1163 {
1164 ais.pushIndent();
1165 try renderToken(ais, tree, eq_token, eq_space); // =
1166 ais.popIndent();
1167 }
1168
1169 if (eq_space == .space)
1170 return renderExpressionComma(gpa, ais, tree, field.ast.value_expr, space); // value
1171
1172 const token_tags = tree.tokens.items(.tag);
1173 const maybe_comma = tree.lastToken(field.ast.value_expr) + 1;
1174
1175 if (token_tags[maybe_comma] == .comma) {
1176 ais.pushIndent();
1177 try renderExpression(gpa, ais, tree, field.ast.value_expr, .none); // value
1178 ais.popIndent();
1179 try renderToken(ais, tree, maybe_comma, space);
1180 } else {
1181 ais.pushIndent();
1182 try renderExpression(gpa, ais, tree, field.ast.value_expr, space); // value
1183 ais.popIndent();
1184 }
11641185}
11651186
11661187fn renderBuiltinCall(