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" {
37263726 );
37273727}
37283728
3729//test "zig fmt: Only indent multiline string literals in function calls" {
3730// try testCanonical(
3731// \\test "zig fmt:" {
3732// \\ try testTransform(
3733// \\ \\const X = struct {
3734// \\ \\ foo: i32, bar: i8 };
3735// \\ ,
3736// \\ \\const X = struct {
3737// \\ \\ foo: i32, bar: i8
3738// \\ \\};
3739// \\ \\
3740// \\ );
3741// \\}
3742// \\
3743// );
3744//}
3729test "zig fmt: Only indent multiline string literals in function calls" {
3730 try testCanonical(
3731 \\test "zig fmt:" {
3732 \\ try testTransform(
3733 \\ \\const X = struct {
3734 \\ \\ foo: i32, bar: i8 };
3735 \\ ,
3736 \\ \\const X = struct {
3737 \\ \\ foo: i32, bar: i8
3738 \\ \\};
3739 \\ \\
3740 \\ );
3741 \\}
3742 \\
3743 );
3744}
37453745
37463746test "zig fmt: Don't add extra newline after if" {
37473747 try testCanonical(
......@@ -3806,68 +3806,66 @@ test "zig fmt: comments in ternary ifs" {
38063806// \\
38073807// );
38083808//}
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" {
3836// try testCanonical(
3837// \\pub fn main() void {
3838// \\ const zoom_node = if (focused_node == layout_first)
3839// \\ if (it.next()) {
3840// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3841// \\ } else null
3842// \\ else
3843// \\ focused_node;
3844// \\
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
3848// \\ focused_node;
3849// \\
3850// \\ const zoom_node = if (focused_node == layout_first)
3851// \\ if (it.next()) {
3852// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3853// \\ } else null;
3854// \\
3855// \\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
3856// \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3857// \\ };
3858// \\
3859// \\ const zoom_node = if (focused_node == layout_first) for (nodes) |node| {
3860// \\ break node;
3861// \\ };
3862// \\
3863// \\ const zoom_node = if (focused_node == layout_first) switch (nodes) {
3864// \\ 0 => 0,
3865// \\ } else
3866// \\ focused_node;
3867// \\}
3868// \\
3869// );
3870//}
3810test "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}
3834
3835test "zig fmt: Control flow statement as body of blockless if" {
3836 try testCanonical(
3837 \\pub fn main() void {
3838 \\ const zoom_node = if (focused_node == layout_first)
3839 \\ if (it.next()) {
3840 \\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3841 \\ } else null
3842 \\ else
3843 \\ focused_node;
3844 \\
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
38723870test "zig fmt: regression test for #5722" {
38733871 try testCanonical(
......@@ -4058,21 +4056,21 @@ test "zig fmt: trailing comma should force multiline 1 column" {
40584056 );
40594057}
40604058
4061//test "zig fmt: function params should align nicely" {
4062// try testCanonical(
4063// \\pub fn foo() void {
4064// \\ cases.addRuntimeSafety("slicing operator with sentinel",
4065// \\ \\const std = @import("std");
4066// \\ ++ check_panic_msg ++
4067// \\ \\pub fn main() void {
4068// \\ \\ var buf = [4]u8{'a','b','c',0};
4069// \\ \\ const slice = buf[0..:0];
4070// \\ \\}
4071// \\ );
4072// \\}
4073// \\
4074// );
4075//}
4059test "zig fmt: function params should align nicely" {
4060 try testCanonical(
4061 \\pub fn foo() void {
4062 \\ cases.addRuntimeSafety("slicing operator with sentinel",
4063 \\ \\const std = @import("std");
4064 \\ ++ check_panic_msg ++
4065 \\ \\pub fn main() void {
4066 \\ \\ var buf = [4]u8{'a','b','c',0};
4067 \\ \\ const slice = buf[0..:0];
4068 \\ \\}
4069 \\ );
4070 \\}
4071 \\
4072 );
4073}
40764074
40774075test "zig fmt: error for invalid bit range" {
40784076 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
197197 while (locked_indents > 0) : (locked_indents -= 1) ais.popIndent();
198198
199199 switch (space) {
200 .none => {},
200 .none, .space, .newline => {},
201201 .semicolon => if (token_tags[i] == .semicolon) try renderToken(ais, tree, i, .newline),
202202 .comma => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .newline),
203203 .comma_space => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .space),
204 else => unreachable,
205204 }
206205 },
207206
......@@ -358,8 +357,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
358357 ais.pushIndent();
359358 try renderToken(ais, tree, op_token, .newline);
360359 ais.popIndent();
361 ais.pushIndentOneShot();
362360 }
361 ais.pushIndentOneShot();
363362 return renderExpression(ais, tree, infix.rhs, space);
364363 },
365364
......@@ -1868,7 +1867,6 @@ fn renderCall(
18681867 space: Space,
18691868) Error!void {
18701869 const token_tags = tree.tokens.items(.tag);
1871 const node_tags = tree.nodes.items(.tag);
18721870 const main_tokens = tree.nodes.items(.main_token);
18731871
18741872 if (call.async_token) |async_token| {
......@@ -1895,7 +1893,8 @@ fn renderCall(
18951893 try renderExpression(ais, tree, param_node, .none);
18961894
18971895 // 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;
18991898 if (is_multiline_string) ais.popIndent();
19001899
19011900 const comma = tree.lastToken(param_node) + 1;
......@@ -1915,7 +1914,8 @@ fn renderCall(
19151914 try renderToken(ais, tree, lparen, .none); // (
19161915
19171916 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;
19191919 if (this_multiline_string) {
19201920 ais.pushIndentOneShot();
19211921 }
......@@ -1923,7 +1923,8 @@ fn renderCall(
19231923
19241924 if (i + 1 < params.len) {
19251925 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;
19271928 const comma_space: Space = if (next_multiline_string) .none else .space;
19281929 try renderToken(ais, tree, comma, comma_space);
19291930 }