Commit eed49a2104

Andrew Kelley <superjoe30@gmail.com>
2018-05-01 00:30:47
zig fmt: aggregate type init with only 1 field
1 parent 1d06915
Changed files (2)
std/zig/parser.zig
@@ -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 });
std/zig/parser_test.zig
@@ -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,
         \\};
         \\
     );