authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-22 18:17:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-22 18:17:26-07:00
loga6038f03753a967082487020a0b834c1c4305503
tree4e91bad8e5b0bca74ec47b802e6ad925a25379b8
parentfec51ad7c5fe5f141f8f0c53a1287c5a00613429

zig fmt: function params should align nicely


2 files changed, 98 insertions(+), 99 deletions(-)

lib/std/zig/parser_test.zig+90-92
...@@ -3726,22 +3726,22 @@ test "zig fmt: C var args" {...@@ -3726,22 +3726,22 @@ test "zig fmt: C var args" {
3726 );3726 );
3727}3727}
37283728
3729//test "zig fmt: Only indent multiline string literals in function calls" {3729test "zig fmt: Only indent multiline string literals in function calls" {
3730// try testCanonical(3730 try testCanonical(
3731// \\test "zig fmt:" {3731 \\test "zig fmt:" {
3732// \\ try testTransform(3732 \\ try testTransform(
3733// \\ \\const X = struct {3733 \\ \\const X = struct {
3734// \\ \\ foo: i32, bar: i8 };3734 \\ \\ foo: i32, bar: i8 };
3735// \\ ,3735 \\ ,
3736// \\ \\const X = struct {3736 \\ \\const X = struct {
3737// \\ \\ foo: i32, bar: i83737 \\ \\ foo: i32, bar: i8
3738// \\ \\};3738 \\ \\};
3739// \\ \\3739 \\ \\
3740// \\ );3740 \\ );
3741// \\}3741 \\}
3742// \\3742 \\
3743// );3743 );
3744//}3744}
37453745
3746test "zig fmt: Don't add extra newline after if" {3746test "zig fmt: Don't add extra newline after if" {
3747 try testCanonical(3747 try testCanonical(
...@@ -3806,68 +3806,66 @@ test "zig fmt: comments in ternary ifs" {...@@ -3806,68 +3806,66 @@ test "zig fmt: comments in ternary ifs" {
3806// \\3806// \\
3807// );3807// );
3808//}3808//}
3809//
3810//test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
3811// try testCanonical(
3812// \\fn foo() void {
3813// \\ z.display_message_dialog(
3814// \\ *const [323:0]u8,
3815// \\ \\Message Text
3816// \\ \\------------
3817// \\ \\xxxxxxxxxxxx
3818// \\ \\xxxxxxxxxxxx
3819// \\ ,
3820// \\ g.GtkMessageType.GTK_MESSAGE_WARNING,
3821// \\ null,
3822// \\ );
3823// \\
3824// \\ z.display_message_dialog(*const [323:0]u8,
3825// \\ \\Message Text
3826// \\ \\------------
3827// \\ \\xxxxxxxxxxxx
3828// \\ \\xxxxxxxxxxxx
3829// \\ , g.GtkMessageType.GTK_MESSAGE_WARNING, null);
3830// \\}
3831// \\
3832// );
3833//}
38343809
3835//test "zig fmt: Control flow statement as body of blockless if" {3810test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
3836// try testCanonical(3811 try testCanonical(
3837// \\pub fn main() void {3812 \\fn foo() void {
3838// \\ const zoom_node = if (focused_node == layout_first)3813 \\ z.display_message_dialog(
3839// \\ if (it.next()) {3814 \\ *const [323:0]u8,
3840// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;3815 \\ \\Message Text
3841// \\ } else null3816 \\ \\------------
3842// \\ else3817 \\ \\xxxxxxxxxxxx
3843// \\ focused_node;3818 \\ \\xxxxxxxxxxxx
3844// \\3819 \\ ,
3845// \\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {3820 \\ g.GtkMessageType.GTK_MESSAGE_WARNING,
3846// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;3821 \\ null,
3847// \\ } else null else3822 \\ );
3848// \\ focused_node;3823 \\
3849// \\3824 \\ z.display_message_dialog(*const [323:0]u8,
3850// \\ const zoom_node = if (focused_node == layout_first)3825 \\ \\Message Text
3851// \\ if (it.next()) {3826 \\ \\------------
3852// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;3827 \\ \\xxxxxxxxxxxx
3853// \\ } else null;3828 \\ \\xxxxxxxxxxxx
3854// \\3829 \\ , g.GtkMessageType.GTK_MESSAGE_WARNING, null);
3855// \\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {3830 \\}
3856// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;3831 \\
3857// \\ };3832 );
3858// \\3833}
3859// \\ const zoom_node = if (focused_node == layout_first) for (nodes) |node| {3834
3860// \\ break node;3835test "zig fmt: Control flow statement as body of blockless if" {
3861// \\ };3836 try testCanonical(
3862// \\3837 \\pub fn main() void {
3863// \\ const zoom_node = if (focused_node == layout_first) switch (nodes) {3838 \\ const zoom_node = if (focused_node == layout_first)
3864// \\ 0 => 0,3839 \\ if (it.next()) {
3865// \\ } else3840 \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3866// \\ focused_node;3841 \\ } else null
3867// \\}3842 \\ else
3868// \\3843 \\ focused_node;
3869// );3844 \\
3870//}3845 \\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
3846 \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3847 \\ } else null else focused_node;
3848 \\
3849 \\ const zoom_node = if (focused_node == layout_first)
3850 \\ if (it.next()) {
3851 \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3852 \\ } else null;
3853 \\
3854 \\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
3855 \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3856 \\ };
3857 \\
3858 \\ const zoom_node = if (focused_node == layout_first) for (nodes) |node| {
3859 \\ break node;
3860 \\ };
3861 \\
3862 \\ const zoom_node = if (focused_node == layout_first) switch (nodes) {
3863 \\ 0 => 0,
3864 \\ } else focused_node;
3865 \\}
3866 \\
3867 );
3868}
38713869
3872test "zig fmt: regression test for #5722" {3870test "zig fmt: regression test for #5722" {
3873 try testCanonical(3871 try testCanonical(
...@@ -4058,21 +4056,21 @@ test "zig fmt: trailing comma should force multiline 1 column" {...@@ -4058,21 +4056,21 @@ test "zig fmt: trailing comma should force multiline 1 column" {
4058 );4056 );
4059}4057}
40604058
4061//test "zig fmt: function params should align nicely" {4059test "zig fmt: function params should align nicely" {
4062// try testCanonical(4060 try testCanonical(
4063// \\pub fn foo() void {4061 \\pub fn foo() void {
4064// \\ cases.addRuntimeSafety("slicing operator with sentinel",4062 \\ cases.addRuntimeSafety("slicing operator with sentinel",
4065// \\ \\const std = @import("std");4063 \\ \\const std = @import("std");
4066// \\ ++ check_panic_msg ++4064 \\ ++ check_panic_msg ++
4067// \\ \\pub fn main() void {4065 \\ \\pub fn main() void {
4068// \\ \\ var buf = [4]u8{'a','b','c',0};4066 \\ \\ var buf = [4]u8{'a','b','c',0};
4069// \\ \\ const slice = buf[0..:0];4067 \\ \\ const slice = buf[0..:0];
4070// \\ \\}4068 \\ \\}
4071// \\ );4069 \\ );
4072// \\}4070 \\}
4073// \\4071 \\
4074// );4072 );
4075//}4073}
40764074
4077test "zig fmt: error for invalid bit range" {4075test "zig fmt: error for invalid bit range" {
4078 try testError(4076 try testError(
lib/std/zig/render.zig+8-7
...@@ -197,11 +197,10 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac...@@ -197,11 +197,10 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
197 while (locked_indents > 0) : (locked_indents -= 1) ais.popIndent();197 while (locked_indents > 0) : (locked_indents -= 1) ais.popIndent();
198198
199 switch (space) {199 switch (space) {
200 .none => {},200 .none, .space, .newline => {},
201 .semicolon => if (token_tags[i] == .semicolon) try renderToken(ais, tree, i, .newline),201 .semicolon => if (token_tags[i] == .semicolon) try renderToken(ais, tree, i, .newline),
202 .comma => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .newline),202 .comma => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .newline),
203 .comma_space => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .space),203 .comma_space => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .space),
204 else => unreachable,
205 }204 }
206 },205 },
207206
...@@ -358,8 +357,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac...@@ -358,8 +357,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
358 ais.pushIndent();357 ais.pushIndent();
359 try renderToken(ais, tree, op_token, .newline);358 try renderToken(ais, tree, op_token, .newline);
360 ais.popIndent();359 ais.popIndent();
361 ais.pushIndentOneShot();
362 }360 }
361 ais.pushIndentOneShot();
363 return renderExpression(ais, tree, infix.rhs, space);362 return renderExpression(ais, tree, infix.rhs, space);
364 },363 },
365364
...@@ -1868,7 +1867,6 @@ fn renderCall(...@@ -1868,7 +1867,6 @@ fn renderCall(
1868 space: Space,1867 space: Space,
1869) Error!void {1868) Error!void {
1870 const token_tags = tree.tokens.items(.tag);1869 const token_tags = tree.tokens.items(.tag);
1871 const node_tags = tree.nodes.items(.tag);
1872 const main_tokens = tree.nodes.items(.main_token);1870 const main_tokens = tree.nodes.items(.main_token);
18731871
1874 if (call.async_token) |async_token| {1872 if (call.async_token) |async_token| {
...@@ -1895,7 +1893,8 @@ fn renderCall(...@@ -1895,7 +1893,8 @@ fn renderCall(
1895 try renderExpression(ais, tree, param_node, .none);1893 try renderExpression(ais, tree, param_node, .none);
18961894
1897 // Unindent the comma for multiline string literals.1895 // Unindent the comma for multiline string literals.
1898 const is_multiline_string = node_tags[param_node] == .multiline_string_literal;1896 const is_multiline_string =
1897 token_tags[tree.firstToken(param_node)] == .multiline_string_literal_line;
1899 if (is_multiline_string) ais.popIndent();1898 if (is_multiline_string) ais.popIndent();
19001899
1901 const comma = tree.lastToken(param_node) + 1;1900 const comma = tree.lastToken(param_node) + 1;
...@@ -1915,7 +1914,8 @@ fn renderCall(...@@ -1915,7 +1914,8 @@ fn renderCall(
1915 try renderToken(ais, tree, lparen, .none); // (1914 try renderToken(ais, tree, lparen, .none); // (
19161915
1917 for (params) |param_node, i| {1916 for (params) |param_node, i| {
1918 const this_multiline_string = node_tags[param_node] == .multiline_string_literal;1917 const this_multiline_string =
1918 token_tags[tree.firstToken(param_node)] == .multiline_string_literal_line;
1919 if (this_multiline_string) {1919 if (this_multiline_string) {
1920 ais.pushIndentOneShot();1920 ais.pushIndentOneShot();
1921 }1921 }
...@@ -1923,7 +1923,8 @@ fn renderCall(...@@ -1923,7 +1923,8 @@ fn renderCall(
19231923
1924 if (i + 1 < params.len) {1924 if (i + 1 < params.len) {
1925 const comma = tree.lastToken(param_node) + 1;1925 const comma = tree.lastToken(param_node) + 1;
1926 const next_multiline_string = node_tags[params[i + 1]] == .multiline_string_literal;1926 const next_multiline_string =
1927 token_tags[tree.firstToken(params[i + 1])] == .multiline_string_literal_line;
1927 const comma_space: Space = if (next_multiline_string) .none else .space;1928 const comma_space: Space = if (next_multiline_string) .none else .space;
1928 try renderToken(ais, tree, comma, comma_space);1929 try renderToken(ais, tree, comma, comma_space);
1929 }1930 }