zig fmt: aggregate type init with only 1 field

This commit is contained in:
Andrew Kelley 2018-04-30 18:30:47 -04:00
parent 1d06915f27
commit eed49a2104
2 changed files with 20 additions and 3 deletions

View File

@ -3872,6 +3872,15 @@ pub const Parser = struct {
try stack.append(RenderState { .Expression = suffix_op.lhs });
continue;
}
if (field_inits.len == 1) {
const field_init = field_inits.at(0);
try stack.append(RenderState { .Text = "}" });
try stack.append(RenderState { .FieldInitializer = field_init });
try stack.append(RenderState { .Text = " {" });
try stack.append(RenderState { .Expression = suffix_op.lhs });
continue;
}
try stack.append(RenderState { .Text = "}"});
try stack.append(RenderState.PrintIndent);
try stack.append(RenderState { .Indent = indent });

View File

@ -1,3 +1,12 @@
test "zig fmt: aggregate type init with only 1 field" {
try testCanonical(
\\comptime {
\\ assert(bar(Payload {.A = 1234}) == -10);
\\}
\\
);
}
test "zig fmt: union(enum(u32)) with assigned enum values" {
try testCanonical(
\\const MultipleChoice = union(enum(u32)) {
@ -709,9 +718,7 @@ test "zig fmt: switch" {
\\ Float: f64,
\\ };
\\
\\ const u = Union {
\\ .Int = 0,
\\ };
\\ const u = Union {.Int = 0};
\\ switch (u) {
\\ Union.Int => |int| {},
\\ Union.Float => |*float| unreachable,
@ -1029,6 +1036,7 @@ test "zig fmt: struct literals with fields on each line" {
try testCanonical(
\\var self = BufSet {
\\ .hash_map = BufSetHashMap.init(a),
\\ .hash_map2 = xyz,
\\};
\\
);