authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-23 17:00:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-23 17:00:33-07:00
logbb89c619edbae8b02c826a2d334e2736c876c07d
tree20fa89ae025bab977a4ae3f2bb6b002d2790f6f4
parent6f4a1bafcf9cc1120881dabc462b46696481720e

zig fmt: multiline string literals + array init


2 files changed, 91 insertions(+), 62 deletions(-)

lib/std/zig/parser_test.zig+65-62
...@@ -3986,68 +3986,71 @@ test "zig fmt: allow trailing line comments to do manual array formatting" {...@@ -3986,68 +3986,71 @@ test "zig fmt: allow trailing line comments to do manual array formatting" {
3986 );3986 );
3987}3987}
39883988
3989//test "zig fmt: multiline string literals should play nice with array initializers" {3989test "zig fmt: multiline string literals should play nice with array initializers" {
3990// try testCanonical(3990 try testCanonical(
3991// \\fn main() void {3991 \\fn main() void {
3992// \\ var a = .{.{.{.{.{.{.{.{3992 \\ var a = .{.{.{.{.{.{.{.{
3993// \\ 0,3993 \\ 0,
3994// \\ }}}}}}}};3994 \\ }}}}}}}};
3995// \\ myFunc(.{3995 \\ myFunc(.{
3996// \\ "aaaaaaa", "bbbbbb", "ccccc",3996 \\ "aaaaaaa", "bbbbbb", "ccccc",
3997// \\ "dddd", ("eee"), ("fff"),3997 \\ "dddd", ("eee"), ("fff"),
3998// \\ ("gggg"),3998 \\ ("gggg"),
3999// \\ // Line comment3999 \\ // Line comment
4000// \\ \\Multiline String Literals can be quite long4000 \\ \\Multiline String Literals can be quite long
4001// \\ ,4001 \\ ,
4002// \\ \\Multiline String Literals can be quite long4002 \\ \\Multiline String Literals can be quite long
4003// \\ \\Multiline String Literals can be quite long4003 \\ \\Multiline String Literals can be quite long
4004// \\ ,4004 \\ ,
4005// \\ \\Multiline String Literals can be quite long4005 \\ \\Multiline String Literals can be quite long
4006// \\ \\Multiline String Literals can be quite long4006 \\ \\Multiline String Literals can be quite long
4007// \\ \\Multiline String Literals can be quite long4007 \\ \\Multiline String Literals can be quite long
4008// \\ \\Multiline String Literals can be quite long4008 \\ \\Multiline String Literals can be quite long
4009// \\ ,4009 \\ ,
4010// \\ (4010 \\ (
4011// \\ \\Multiline String Literals can be quite long4011 \\ \\Multiline String Literals can be quite long
4012// \\ ),4012 \\ ),
4013// \\ .{4013 \\ .{
4014// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4014 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4015// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4015 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4016// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4016 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4017// \\ },4017 \\ },
4018// \\ .{(4018 \\ .{(
4019// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4019 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4020// \\ )},4020 \\ )},
4021// \\ .{4021 \\ .{
4022// \\ "xxxxxxx", "xxx",4022 \\ "xxxxxxx", "xxx",
4023// \\ (4023 \\ (
4024// \\ \\ xxx4024 \\ \\ xxx
4025// \\ ),4025 \\ ),
4026// \\ "xxx", "xxx",4026 \\ "xxx",
4027// \\ },4027 \\ "xxx",
4028// \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" }, .{ "xxxxxxx", "xxx", "xxx", "xxx" },4028 \\ },
4029// \\ "aaaaaaa", "bbbbbb", "ccccc", // -4029 \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" },
4030// \\ "dddd", ("eee"), ("fff"),4030 \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" },
4031// \\ .{4031 \\ "aaaaaaa", "bbbbbb", "ccccc", // -
4032// \\ "xxx", "xxx",4032 \\ "dddd", ("eee"), ("fff"),
4033// \\ (4033 \\ .{
4034// \\ \\ xxx4034 \\ "xxx", "xxx",
4035// \\ ),4035 \\ (
4036// \\ "xxxxxxxxxxxxxx", "xxx",4036 \\ \\ xxx
4037// \\ },4037 \\ ),
4038// \\ .{4038 \\ "xxxxxxxxxxxxxx",
4039// \\ (4039 \\ "xxx",
4040// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4040 \\ },
4041// \\ ),4041 \\ .{
4042// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4042 \\ (
4043// \\ },4043 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4044// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4044 \\ ),
4045// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4045 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4046// \\ });4046 \\ },
4047// \\}4047 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4048// \\4048 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4049// );4049 \\ });
4050//}4050 \\}
4051 \\
4052 );
4053}
40514054
4052test "zig fmt: use of comments and multiline string literals may force the parameters over multiple lines" {4055test "zig fmt: use of comments and multiline string literals may force the parameters over multiple lines" {
4053 try testCanonical(4056 try testCanonical(
lib/std/zig/render.zig+26
...@@ -1686,6 +1686,19 @@ fn renderArrayInit(...@@ -1686,6 +1686,19 @@ fn renderArrayInit(
1686 const trailing_comma = token_tags[last_elem_token + 1] == .comma;1686 const trailing_comma = token_tags[last_elem_token + 1] == .comma;
1687 const rbrace = if (trailing_comma) last_elem_token + 2 else last_elem_token + 1;1687 const rbrace = if (trailing_comma) last_elem_token + 2 else last_elem_token + 1;
1688 assert(token_tags[rbrace] == .r_brace);1688 assert(token_tags[rbrace] == .r_brace);
1689
1690 if (array_init.ast.elements.len == 1) {
1691 const only_elem = array_init.ast.elements[0];
1692 const first_token = tree.firstToken(only_elem);
1693 if (token_tags[first_token] != .multiline_string_literal_line and
1694 !anythingBetween(tree, last_elem_token, rbrace))
1695 {
1696 try renderToken(ais, tree, array_init.ast.lbrace, .none);
1697 try renderExpression(gpa, ais, tree, only_elem, .none);
1698 return renderToken(ais, tree, rbrace, space);
1699 }
1700 }
1701
1689 const contains_newlines = !tree.tokensOnSameLine(array_init.ast.lbrace, rbrace);1702 const contains_newlines = !tree.tokensOnSameLine(array_init.ast.lbrace, rbrace);
16901703
1691 if (!trailing_comma and !contains_newlines) {1704 if (!trailing_comma and !contains_newlines) {
...@@ -2353,6 +2366,19 @@ fn hasSameLineComment(tree: ast.Tree, token_index: ast.TokenIndex) bool {...@@ -2353,6 +2366,19 @@ fn hasSameLineComment(tree: ast.Tree, token_index: ast.TokenIndex) bool {
2353 return false;2366 return false;
2354}2367}
23552368
2369/// Returns `true` if and only if there are any tokens or line comments between
2370/// start_token and end_token.
2371fn anythingBetween(tree: ast.Tree, start_token: ast.TokenIndex, end_token: ast.TokenIndex) bool {
2372 if (start_token + 1 != end_token) return true;
2373 const token_starts = tree.tokens.items(.start);
2374 const between_source = tree.source[token_starts[start_token]..token_starts[start_token + 1]];
2375 for (between_source) |byte| switch (byte) {
2376 '/' => return true,
2377 else => continue,
2378 };
2379 return false;
2380}
2381
2356fn writeFixingWhitespace(writer: std.ArrayList(u8).Writer, slice: []const u8) Error!void {2382fn writeFixingWhitespace(writer: std.ArrayList(u8).Writer, slice: []const u8) Error!void {
2357 for (slice) |byte| switch (byte) {2383 for (slice) |byte| switch (byte) {
2358 '\t' => try writer.writeAll(" " ** 4),2384 '\t' => try writer.writeAll(" " ** 4),