authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-12 21:23:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-12 21:23:18-04:00
logd4572d1140bb22aaeb587a66d3eee7bbdf162b1c
tree8ae3ac8f31b6ab03adc203f63e1ecefe599afc4f
parent373b3586a1b1d6a3989220a8aac0d1c30f9fdb3a

zig fmt: container init fields each on own line

See #911

1 files changed, 66 insertions(+), 25 deletions(-)

std/zig/parser.zig+66-25
...@@ -3579,33 +3579,45 @@ pub const Parser = struct {...@@ -3579,33 +3579,45 @@ pub const Parser = struct {
3579 try stack.append(RenderState { .Expression = suffix_op.lhs });3579 try stack.append(RenderState { .Expression = suffix_op.lhs });
3580 },3580 },
3581 ast.NodeSuffixOp.SuffixOp.StructInitializer => |field_inits| {3581 ast.NodeSuffixOp.SuffixOp.StructInitializer => |field_inits| {
3582 try stack.append(RenderState { .Text = " }"});3582 if (field_inits.len == 0) {
3583 try stack.append(RenderState { .Text = "{}" });
3584 try stack.append(RenderState { .Expression = suffix_op.lhs });
3585 continue;
3586 }
3587 try stack.append(RenderState { .Text = "}"});
3588 try stack.append(RenderState.PrintIndent);
3589 try stack.append(RenderState { .Indent = indent });
3583 var i = field_inits.len;3590 var i = field_inits.len;
3584 while (i != 0) {3591 while (i != 0) {
3585 i -= 1;3592 i -= 1;
3586 const field_init = field_inits.at(i);3593 const field_init = field_inits.at(i);
3594 try stack.append(RenderState { .Text = ",\n" });
3587 try stack.append(RenderState { .FieldInitializer = field_init });3595 try stack.append(RenderState { .FieldInitializer = field_init });
3588 try stack.append(RenderState { .Text = " " });3596 try stack.append(RenderState.PrintIndent);
3589 if (i != 0) {
3590 try stack.append(RenderState { .Text = "," });
3591 }
3592 }3597 }
3593 try stack.append(RenderState { .Text = "{"});3598 try stack.append(RenderState { .Indent = indent + indent_delta });
3599 try stack.append(RenderState { .Text = " {\n"});
3594 try stack.append(RenderState { .Expression = suffix_op.lhs });3600 try stack.append(RenderState { .Expression = suffix_op.lhs });
3595 },3601 },
3596 ast.NodeSuffixOp.SuffixOp.ArrayInitializer => |exprs| {3602 ast.NodeSuffixOp.SuffixOp.ArrayInitializer => |exprs| {
3597 try stack.append(RenderState { .Text = " }"});3603 if (exprs.len == 0) {
3604 try stack.append(RenderState { .Text = "{}" });
3605 try stack.append(RenderState { .Expression = suffix_op.lhs });
3606 continue;
3607 }
3608 try stack.append(RenderState { .Text = "}"});
3609 try stack.append(RenderState.PrintIndent);
3610 try stack.append(RenderState { .Indent = indent });
3598 var i = exprs.len;3611 var i = exprs.len;
3599 while (i != 0) {3612 while (i != 0) {
3600 i -= 1;3613 i -= 1;
3601 const expr = exprs.at(i);3614 const expr = exprs.at(i);
3615 try stack.append(RenderState { .Text = ",\n" });
3602 try stack.append(RenderState { .Expression = expr });3616 try stack.append(RenderState { .Expression = expr });
3603 try stack.append(RenderState { .Text = " " });3617 try stack.append(RenderState.PrintIndent);
3604 if (i != 0) {
3605 try stack.append(RenderState { .Text = "," });
3606 }
3607 }3618 }
3608 try stack.append(RenderState { .Text = "{"});3619 try stack.append(RenderState { .Indent = indent + indent_delta });
3620 try stack.append(RenderState { .Text = " {\n"});
3609 try stack.append(RenderState { .Expression = suffix_op.lhs });3621 try stack.append(RenderState { .Expression = suffix_op.lhs });
3610 },3622 },
3611 }3623 }
...@@ -4562,10 +4574,10 @@ test "zig fmt: precedence" {...@@ -4562,10 +4574,10 @@ test "zig fmt: precedence" {
4562 \\ (a!b)();4574 \\ (a!b)();
4563 \\ !a!b;4575 \\ !a!b;
4564 \\ !(a!b);4576 \\ !(a!b);
4565 \\ !a{ };4577 \\ !a{};
4566 \\ !(a{ });4578 \\ !(a{});
4567 \\ a + b{ };4579 \\ a + b{};
4568 \\ (a + b){ };4580 \\ (a + b){};
4569 \\ a << b + c;4581 \\ a << b + c;
4570 \\ (a << b) + c;4582 \\ (a << b) + c;
4571 \\ a & b << c;4583 \\ a & b << c;
...@@ -4805,9 +4817,15 @@ test "zig fmt: error set declaration" {...@@ -4805,9 +4817,15 @@ test "zig fmt: error set declaration" {
4805test "zig fmt: arrays" {4817test "zig fmt: arrays" {
4806 try testCanonical(4818 try testCanonical(
4807 \\test "test array" {4819 \\test "test array" {
4808 \\ const a: [2]u8 = [2]u8{ 1, 2 };4820 \\ const a: [2]u8 = [2]u8 {
4809 \\ const a: [2]u8 = []u8{ 1, 2 };4821 \\ 1,
4810 \\ const a: [0]u8 = []u8{ };4822 \\ 2,
4823 \\ };
4824 \\ const a: [2]u8 = []u8 {
4825 \\ 1,
4826 \\ 2,
4827 \\ };
4828 \\ const a: [0]u8 = []u8{};
4811 \\}4829 \\}
4812 \\4830 \\
4813 );4831 );
...@@ -4815,10 +4833,18 @@ test "zig fmt: arrays" {...@@ -4815,10 +4833,18 @@ test "zig fmt: arrays" {
48154833
4816test "zig fmt: container initializers" {4834test "zig fmt: container initializers" {
4817 try testCanonical(4835 try testCanonical(
4818 \\const a1 = []u8{ };4836 \\const a1 = []u8{};
4819 \\const a2 = []u8{ 1, 2, 3, 4 };4837 \\const a2 = []u8 {
4820 \\const s1 = S{ };4838 \\ 1,
4821 \\const s2 = S{ .a = 1, .b = 2 };4839 \\ 2,
4840 \\ 3,
4841 \\ 4,
4842 \\};
4843 \\const s1 = S{};
4844 \\const s2 = S {
4845 \\ .a = 1,
4846 \\ .b = 2,
4847 \\};
4822 \\4848 \\
4823 );4849 );
4824}4850}
...@@ -4883,7 +4909,9 @@ test "zig fmt: switch" {...@@ -4883,7 +4909,9 @@ test "zig fmt: switch" {
4883 \\ Float: f644909 \\ Float: f64
4884 \\ };4910 \\ };
4885 \\4911 \\
4886 \\ const u = Union{ .Int = 0 };4912 \\ const u = Union {
4913 \\ .Int = 0,
4914 \\ };
4887 \\ switch (u) {4915 \\ switch (u) {
4888 \\ Union.Int => |int| {},4916 \\ Union.Int => |int| {},
4889 \\ Union.Float => |*float| unreachable4917 \\ Union.Float => |*float| unreachable
...@@ -4962,7 +4990,11 @@ test "zig fmt: while" {...@@ -4962,7 +4990,11 @@ test "zig fmt: while" {
4962test "zig fmt: for" {4990test "zig fmt: for" {
4963 try testCanonical(4991 try testCanonical(
4964 \\test "for" {4992 \\test "for" {
4965 \\ const a = []u8{ 1, 2, 3 };4993 \\ const a = []u8 {
4994 \\ 1,
4995 \\ 2,
4996 \\ 3,
4997 \\ };
4966 \\ for (a) |v| {4998 \\ for (a) |v| {
4967 \\ continue;4999 \\ continue;
4968 \\ }5000 \\ }
...@@ -5192,3 +5224,12 @@ test "zig fmt: error return" {...@@ -5192,3 +5224,12 @@ test "zig fmt: error return" {
5192 \\5224 \\
5193 );5225 );
5194}5226}
5227
5228test "zig fmt: struct literals with fields on each line" {
5229 try testCanonical(
5230 \\var self = BufSet {
5231 \\ .hash_map = BufSetHashMap.init(a),
5232 \\};
5233 \\
5234 );
5235}