Skip to content

Commit

Permalink
zig fmt: put nested struct inits on newlines
Browse files Browse the repository at this point in the history
See #1003
  • Loading branch information
andrewrk committed May 26, 2018
1 parent c029f4b commit 85ca611
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions std/zig/parser_test.zig
@@ -1,3 +1,12 @@
test "zig fmt: nested struct literal with one item" {
try testCanonical(
\\const a = foo{
\\ .item = bar{ .a = b },
\\};
\\
);
}

test "zig fmt: switch cases trailing comma" {
try testTransform(
\\fn switch_cases(x: i32) void {
Expand Down
12 changes: 9 additions & 3 deletions std/zig/render.zig
Expand Up @@ -431,12 +431,18 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
return;
}

if (field_inits.len == 1) {
const field_init = field_inits.at(0).*;
if (field_inits.len == 1) blk: {
const field_init = ??field_inits.at(0).*.cast(ast.Node.FieldInitializer);

if (field_init.expr.cast(ast.Node.SuffixOp)) |nested_suffix_op| {
if (nested_suffix_op.op == ast.Node.SuffixOp.Op.StructInitializer) {
break :blk;
}
}

try renderExpression(allocator, stream, tree, indent, suffix_op.lhs, Space.None);
try renderToken(tree, stream, lbrace, indent, Space.Space);
try renderExpression(allocator, stream, tree, indent, field_init, Space.Space);
try renderExpression(allocator, stream, tree, indent, &field_init.base, Space.Space);
try renderToken(tree, stream, suffix_op.rtoken, indent, space);
return;
}
Expand Down

0 comments on commit 85ca611

Please sign in to comment.