authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 18:30:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 18:30:47-04:00
logeed49a210474af98f64961ed603feb6dfa60acde
tree94d1ddbfc5aac38476ed1edd74b27f19b9767921
parent1d06915f275ac59918c32206ad944fd10cc997d4

zig fmt: aggregate type init with only 1 field


2 files changed, 20 insertions(+), 3 deletions(-)

std/zig/parser.zig+9
...@@ -3872,6 +3872,15 @@ pub const Parser = struct {...@@ -3872,6 +3872,15 @@ pub const Parser = struct {
3872 try stack.append(RenderState { .Expression = suffix_op.lhs });3872 try stack.append(RenderState { .Expression = suffix_op.lhs });
3873 continue;3873 continue;
3874 }3874 }
3875 if (field_inits.len == 1) {
3876 const field_init = field_inits.at(0);
3877
3878 try stack.append(RenderState { .Text = "}" });
3879 try stack.append(RenderState { .FieldInitializer = field_init });
3880 try stack.append(RenderState { .Text = " {" });
3881 try stack.append(RenderState { .Expression = suffix_op.lhs });
3882 continue;
3883 }
3875 try stack.append(RenderState { .Text = "}"});3884 try stack.append(RenderState { .Text = "}"});
3876 try stack.append(RenderState.PrintIndent);3885 try stack.append(RenderState.PrintIndent);
3877 try stack.append(RenderState { .Indent = indent });3886 try stack.append(RenderState { .Indent = indent });
std/zig/parser_test.zig+11-3
...@@ -1,3 +1,12 @@...@@ -1,3 +1,12 @@
1test "zig fmt: aggregate type init with only 1 field" {
2 try testCanonical(
3 \\comptime {
4 \\ assert(bar(Payload {.A = 1234}) == -10);
5 \\}
6 \\
7 );
8}
9
1test "zig fmt: union(enum(u32)) with assigned enum values" {10test "zig fmt: union(enum(u32)) with assigned enum values" {
2 try testCanonical(11 try testCanonical(
3 \\const MultipleChoice = union(enum(u32)) {12 \\const MultipleChoice = union(enum(u32)) {
...@@ -709,9 +718,7 @@ test "zig fmt: switch" {...@@ -709,9 +718,7 @@ test "zig fmt: switch" {
709 \\ Float: f64,718 \\ Float: f64,
710 \\ };719 \\ };
711 \\720 \\
712 \\ const u = Union {721 \\ const u = Union {.Int = 0};
713 \\ .Int = 0,
714 \\ };
715 \\ switch (u) {722 \\ switch (u) {
716 \\ Union.Int => |int| {},723 \\ Union.Int => |int| {},
717 \\ Union.Float => |*float| unreachable,724 \\ Union.Float => |*float| unreachable,
...@@ -1029,6 +1036,7 @@ test "zig fmt: struct literals with fields on each line" {...@@ -1029,6 +1036,7 @@ test "zig fmt: struct literals with fields on each line" {
1029 try testCanonical(1036 try testCanonical(
1030 \\var self = BufSet {1037 \\var self = BufSet {
1031 \\ .hash_map = BufSetHashMap.init(a),1038 \\ .hash_map = BufSetHashMap.init(a),
1039 \\ .hash_map2 = xyz,
1032 \\};1040 \\};
1033 \\1041 \\
1034 );1042 );