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" {
39863986 );
39873987}
39883988
3989//test "zig fmt: multiline string literals should play nice with array initializers" {
3990// try testCanonical(
3991// \\fn main() void {
3992// \\ var a = .{.{.{.{.{.{.{.{
3993// \\ 0,
3994// \\ }}}}}}}};
3995// \\ myFunc(.{
3996// \\ "aaaaaaa", "bbbbbb", "ccccc",
3997// \\ "dddd", ("eee"), ("fff"),
3998// \\ ("gggg"),
3999// \\ // Line comment
4000// \\ \\Multiline String Literals can be quite long
4001// \\ ,
4002// \\ \\Multiline String Literals can be quite long
4003// \\ \\Multiline String Literals can be quite long
4004// \\ ,
4005// \\ \\Multiline String Literals can be quite long
4006// \\ \\Multiline String Literals can be quite long
4007// \\ \\Multiline String Literals can be quite long
4008// \\ \\Multiline String Literals can be quite long
4009// \\ ,
4010// \\ (
4011// \\ \\Multiline String Literals can be quite long
4012// \\ ),
4013// \\ .{
4014// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4015// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4016// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4017// \\ },
4018// \\ .{(
4019// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4020// \\ )},
4021// \\ .{
4022// \\ "xxxxxxx", "xxx",
4023// \\ (
4024// \\ \\ xxx
4025// \\ ),
4026// \\ "xxx", "xxx",
4027// \\ },
4028// \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" }, .{ "xxxxxxx", "xxx", "xxx", "xxx" },
4029// \\ "aaaaaaa", "bbbbbb", "ccccc", // -
4030// \\ "dddd", ("eee"), ("fff"),
4031// \\ .{
4032// \\ "xxx", "xxx",
4033// \\ (
4034// \\ \\ xxx
4035// \\ ),
4036// \\ "xxxxxxxxxxxxxx", "xxx",
4037// \\ },
4038// \\ .{
4039// \\ (
4040// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4041// \\ ),
4042// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4043// \\ },
4044// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4045// \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4046// \\ });
4047// \\}
4048// \\
4049// );
4050//}
3989test "zig fmt: multiline string literals should play nice with array initializers" {
3990 try testCanonical(
3991 \\fn main() void {
3992 \\ var a = .{.{.{.{.{.{.{.{
3993 \\ 0,
3994 \\ }}}}}}}};
3995 \\ myFunc(.{
3996 \\ "aaaaaaa", "bbbbbb", "ccccc",
3997 \\ "dddd", ("eee"), ("fff"),
3998 \\ ("gggg"),
3999 \\ // Line comment
4000 \\ \\Multiline String Literals can be quite long
4001 \\ ,
4002 \\ \\Multiline String Literals can be quite long
4003 \\ \\Multiline String Literals can be quite long
4004 \\ ,
4005 \\ \\Multiline String Literals can be quite long
4006 \\ \\Multiline String Literals can be quite long
4007 \\ \\Multiline String Literals can be quite long
4008 \\ \\Multiline String Literals can be quite long
4009 \\ ,
4010 \\ (
4011 \\ \\Multiline String Literals can be quite long
4012 \\ ),
4013 \\ .{
4014 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4015 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4016 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4017 \\ },
4018 \\ .{(
4019 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4020 \\ )},
4021 \\ .{
4022 \\ "xxxxxxx", "xxx",
4023 \\ (
4024 \\ \\ xxx
4025 \\ ),
4026 \\ "xxx",
4027 \\ "xxx",
4028 \\ },
4029 \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" },
4030 \\ .{ "xxxxxxx", "xxx", "xxx", "xxx" },
4031 \\ "aaaaaaa", "bbbbbb", "ccccc", // -
4032 \\ "dddd", ("eee"), ("fff"),
4033 \\ .{
4034 \\ "xxx", "xxx",
4035 \\ (
4036 \\ \\ xxx
4037 \\ ),
4038 \\ "xxxxxxxxxxxxxx",
4039 \\ "xxx",
4040 \\ },
4041 \\ .{
4042 \\ (
4043 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4044 \\ ),
4045 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4046 \\ },
4047 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4048 \\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4049 \\ });
4050 \\}
4051 \\
4052 );
4053}
40514054
40524055test "zig fmt: use of comments and multiline string literals may force the parameters over multiple lines" {
40534056 try testCanonical(
lib/std/zig/render.zig+26
......@@ -1686,6 +1686,19 @@ fn renderArrayInit(
16861686 const trailing_comma = token_tags[last_elem_token + 1] == .comma;
16871687 const rbrace = if (trailing_comma) last_elem_token + 2 else last_elem_token + 1;
16881688 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
16891702 const contains_newlines = !tree.tokensOnSameLine(array_init.ast.lbrace, rbrace);
16901703
16911704 if (!trailing_comma and !contains_newlines) {
......@@ -2353,6 +2366,19 @@ fn hasSameLineComment(tree: ast.Tree, token_index: ast.TokenIndex) bool {
23532366 return false;
23542367}
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
23562382fn writeFixingWhitespace(writer: std.ArrayList(u8).Writer, slice: []const u8) Error!void {
23572383 for (slice) |byte| switch (byte) {
23582384 '\t' => try writer.writeAll(" " ** 4),