authorgravatar for lewis.gaul@gmail.comLewis Gaul <lewis.gaul@gmail.com> 2021-04-10 15:39:26+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-10 16:39:26+02:00
logecf555c6931afdb32af4ad0f84311986e0fa3687
tree0a47d663fc779e6244471d7deef1ed899cf42875
parent0d92bd474f0aa93c79b532b6dc713790597fa293
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

zig fmt: render array init on one line if no trailing comma

Continue to insert a trailing comma if there is a comment or multiline string literal present.

2 files changed, 84 insertions(+), 10 deletions(-)

lib/std/zig/parser_test.zig+66-8
......@@ -1801,19 +1801,21 @@ test "zig fmt: array literal with hint" {
18011801 );
18021802}
18031803
1804test "zig fmt: array literal veritical column alignment" {
1804test "zig fmt: array literal vertical column alignment" {
18051805 try testTransform(
18061806 \\const a = []u8{
18071807 \\ 1000, 200,
18081808 \\ 30, 4,
1809 \\ 50000, 60
1809 \\ 50000, 60,
18101810 \\};
18111811 \\const a = []u8{0, 1, 2, 3, 40,
18121812 \\ 4,5,600,7,
18131813 \\ 80,
1814 \\ 9, 10, 11, 0, 13, 14, 15};
1814 \\ 9, 10, 11, 0, 13, 14, 15,};
18151815 \\const a = [12]u8{
18161816 \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1817 \\const a = [12]u8{
1818 \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, };
18171819 \\
18181820 ,
18191821 \\const a = []u8{
......@@ -1827,8 +1829,20 @@ test "zig fmt: array literal veritical column alignment" {
18271829 \\ 9, 10, 11, 0, 13,
18281830 \\ 14, 15,
18291831 \\};
1832 \\const a = [12]u8{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
18301833 \\const a = [12]u8{
1831 \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
1834 \\ 31,
1835 \\ 28,
1836 \\ 31,
1837 \\ 30,
1838 \\ 31,
1839 \\ 30,
1840 \\ 31,
1841 \\ 31,
1842 \\ 30,
1843 \\ 31,
1844 \\ 30,
1845 \\ 31,
18321846 \\};
18331847 \\
18341848 );
......@@ -2031,10 +2045,7 @@ test "zig fmt: add trailing comma to array literal" {
20312045 \\ return []u16{
20322046 \\ 'm', 's', 'y', 's', '-', // hi
20332047 \\ };
2034 \\ return []u16{
2035 \\ 'm', 's', 'y', 's',
2036 \\ '-',
2037 \\ };
2048 \\ return []u16{ 'm', 's', 'y', 's', '-' };
20382049 \\ return []u16{ 'm', 's', 'y', 's', '-' };
20392050 \\}
20402051 \\
......@@ -4666,6 +4677,53 @@ test "zig fmt: insert trailing comma if there are comments between switch values
46664677 );
46674678}
46684679
4680test "zig fmt: make single-line if no trailing comma" {
4681 try testTransform(
4682 \\test "function call no trailing comma" {
4683 \\ foo(
4684 \\ 1,
4685 \\ 2
4686 \\ );
4687 \\}
4688 \\
4689 ,
4690 \\test "function call no trailing comma" {
4691 \\ foo(1, 2);
4692 \\}
4693 \\
4694 );
4695
4696 try testTransform(
4697 \\test "struct no trailing comma" {
4698 \\ const a = .{
4699 \\ .foo = 1,
4700 \\ .bar = 2
4701 \\ };
4702 \\}
4703 \\
4704 ,
4705 \\test "struct no trailing comma" {
4706 \\ const a = .{ .foo = 1, .bar = 2 };
4707 \\}
4708 \\
4709 );
4710
4711 try testTransform(
4712 \\test "array no trailing comma" {
4713 \\ var stream = multiOutStream(.{
4714 \\ fbs1.outStream(),
4715 \\ fbs2.outStream()
4716 \\ });
4717 \\}
4718 \\
4719 ,
4720 \\test "array no trailing comma" {
4721 \\ var stream = multiOutStream(.{ fbs1.outStream(), fbs2.outStream() });
4722 \\}
4723 \\
4724 );
4725}
4726
46694727test "zig fmt: error for invalid bit range" {
46704728 try testError(
46714729 \\var x: []align(0:0:0)u8 = bar;
lib/std/zig/render.zig+18-2
......@@ -1632,9 +1632,10 @@ fn renderArrayInit(
16321632 }
16331633 }
16341634
1635 const contains_newlines = !tree.tokensOnSameLine(array_init.ast.lbrace, rbrace);
1635 const contains_comment = hasComment(tree, array_init.ast.lbrace, rbrace);
1636 const contains_multiline_string = hasMultilineString(tree, array_init.ast.lbrace, rbrace);
16361637
1637 if (!trailing_comma and !contains_newlines) {
1638 if (!trailing_comma and !contains_comment and !contains_multiline_string) {
16381639 // Render all on one line, no trailing comma.
16391640 if (array_init.ast.elements.len == 1) {
16401641 // If there is only one element, we don't use spaces
......@@ -2252,6 +2253,21 @@ fn hasComment(tree: ast.Tree, start_token: ast.TokenIndex, end_token: ast.TokenI
22522253 return mem.indexOf(u8, tree.source[start..end], "//") != null;
22532254}
22542255
2256/// Returns true if there exists a multiline string literal between the start
2257/// of token `start_token` and the start of token `end_token`.
2258fn hasMultilineString(tree: ast.Tree, start_token: ast.TokenIndex, end_token: ast.TokenIndex) bool {
2259 const token_tags = tree.tokens.items(.tag);
2260
2261 for (token_tags[start_token..end_token]) |tag| {
2262 switch (tag) {
2263 .multiline_string_literal_line => return true,
2264 else => continue,
2265 }
2266 }
2267
2268 return false;
2269}
2270
22552271/// Assumes that start is the first byte past the previous token and
22562272/// that end is the last byte before the next token.
22572273fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {