| author | |
| committer | |
| log | 5ecf8bddaeebf26ac6cd174f5f79e02ca243e501 |
| tree | d4243fd724718867680c6b98e2731bb5257760f9 |
| parent | f16f25047c511cc5f468da57292a968555c4b791 |
| signature |
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 #76182 files changed, 48 insertions(+), 2 deletions(-)
lib/std/zig/parser_test.zig+25| ... | @@ -4,6 +4,31 @@ | ... | @@ -4,6 +4,31 @@ |
| 4 | // The MIT license requires this copyright notice to be included in all copies | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | // and substantial portions of the software. | 5 | // and substantial portions of the software. |
| 6 | 6 | ||
| 7 | test "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 | |||
| 7 | // TODO Remove this after zig 0.9.0 is released. | 32 | // TODO Remove this after zig 0.9.0 is released. |
| 8 | test "zig fmt: rewrite inline functions as callconv(.Inline)" { | 33 | test "zig fmt: rewrite inline functions as callconv(.Inline)" { |
| 9 | try testTransform( | 34 | try testTransform( |
lib/std/zig/render.zig+23-2| ... | @@ -1159,8 +1159,29 @@ fn renderContainerField( | ... | @@ -1159,8 +1159,29 @@ fn renderContainerField( |
| 1159 | try renderToken(ais, tree, rparen_token, .space); // ) | 1159 | try renderToken(ais, tree, rparen_token, .space); // ) |
| 1160 | } | 1160 | } |
| 1161 | const eq_token = tree.firstToken(field.ast.value_expr) - 1; | 1161 | const eq_token = tree.firstToken(field.ast.value_expr) - 1; |
| 1162 | try renderToken(ais, tree, eq_token, .space); // = | 1162 | const eq_space: Space = if (tree.tokensOnSameLine(eq_token, eq_token + 1)) .space else .newline; |
| 1163 | return renderExpressionComma(gpa, ais, tree, field.ast.value_expr, space); // value | 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 | } | ||
| 1164 | } | 1185 | } |
| 1165 | 1186 | ||
| 1166 | fn renderBuiltinCall( | 1187 | fn renderBuiltinCall( |