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" {...@@ -1801,19 +1801,21 @@ test "zig fmt: array literal with hint" {
1801 );1801 );
1802}1802}
18031803
1804test "zig fmt: array literal veritical column alignment" {1804test "zig fmt: array literal vertical column alignment" {
1805 try testTransform(1805 try testTransform(
1806 \\const a = []u8{1806 \\const a = []u8{
1807 \\ 1000, 200,1807 \\ 1000, 200,
1808 \\ 30, 4,1808 \\ 30, 4,
1809 \\ 50000, 601809 \\ 50000, 60,
1810 \\};1810 \\};
1811 \\const a = []u8{0, 1, 2, 3, 40,1811 \\const a = []u8{0, 1, 2, 3, 40,
1812 \\ 4,5,600,7,1812 \\ 4,5,600,7,
1813 \\ 80,1813 \\ 80,
1814 \\ 9, 10, 11, 0, 13, 14, 15};1814 \\ 9, 10, 11, 0, 13, 14, 15,};
1815 \\const a = [12]u8{1815 \\const a = [12]u8{
1816 \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };1816 \\ 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, };
1817 \\1819 \\
1818 ,1820 ,
1819 \\const a = []u8{1821 \\const a = []u8{
...@@ -1827,8 +1829,20 @@ test "zig fmt: array literal veritical column alignment" {...@@ -1827,8 +1829,20 @@ test "zig fmt: array literal veritical column alignment" {
1827 \\ 9, 10, 11, 0, 13,1829 \\ 9, 10, 11, 0, 13,
1828 \\ 14, 15,1830 \\ 14, 15,
1829 \\};1831 \\};
1832 \\const a = [12]u8{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1830 \\const a = [12]u8{1833 \\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,
1832 \\};1846 \\};
1833 \\1847 \\
1834 );1848 );
...@@ -2031,10 +2045,7 @@ test "zig fmt: add trailing comma to array literal" {...@@ -2031,10 +2045,7 @@ test "zig fmt: add trailing comma to array literal" {
2031 \\ return []u16{2045 \\ return []u16{
2032 \\ 'm', 's', 'y', 's', '-', // hi2046 \\ 'm', 's', 'y', 's', '-', // hi
2033 \\ };2047 \\ };
2034 \\ return []u16{2048 \\ return []u16{ 'm', 's', 'y', 's', '-' };
2035 \\ 'm', 's', 'y', 's',
2036 \\ '-',
2037 \\ };
2038 \\ return []u16{ 'm', 's', 'y', 's', '-' };2049 \\ return []u16{ 'm', 's', 'y', 's', '-' };
2039 \\}2050 \\}
2040 \\2051 \\
...@@ -4666,6 +4677,53 @@ test "zig fmt: insert trailing comma if there are comments between switch values...@@ -4666,6 +4677,53 @@ test "zig fmt: insert trailing comma if there are comments between switch values
4666 );4677 );
4667}4678}
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
4669test "zig fmt: error for invalid bit range" {4727test "zig fmt: error for invalid bit range" {
4670 try testError(4728 try testError(
4671 \\var x: []align(0:0:0)u8 = bar;4729 \\var x: []align(0:0:0)u8 = bar;
lib/std/zig/render.zig+18-2
...@@ -1632,9 +1632,10 @@ fn renderArrayInit(...@@ -1632,9 +1632,10 @@ fn renderArrayInit(
1632 }1632 }
1633 }1633 }
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) {
1638 // Render all on one line, no trailing comma.1639 // Render all on one line, no trailing comma.
1639 if (array_init.ast.elements.len == 1) {1640 if (array_init.ast.elements.len == 1) {
1640 // If there is only one element, we don't use spaces1641 // 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...@@ -2252,6 +2253,21 @@ fn hasComment(tree: ast.Tree, start_token: ast.TokenIndex, end_token: ast.TokenI
2252 return mem.indexOf(u8, tree.source[start..end], "//") != null;2253 return mem.indexOf(u8, tree.source[start..end], "//") != null;
2253}2254}
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
2255/// Assumes that start is the first byte past the previous token and2271/// Assumes that start is the first byte past the previous token and
2256/// that end is the last byte before the next token.2272/// that end is the last byte before the next token.
2257fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {2273fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {