authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 23:17:00+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 23:17:38+01:00
log4ee368c4b33f5cf286c575e94535deaf59659895
treec8c95d0a2eba7adf9da0e7e50a8c25343183de6d
parentca9259340d42d92c8e44cb814effae447ee64a24
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: comments/line breaks in field access chain


2 files changed, 105 insertions(+), 35 deletions(-)

lib/std/zig/parser_test.zig+82-34
......@@ -3790,40 +3790,88 @@ test "zig fmt: comments in ternary ifs" {
37903790 );
37913791}
37923792
3793//test "zig fmt: test comments in field access chain" {
3794// try testCanonical(
3795// \\pub const str = struct {
3796// \\ pub const Thing = more.more //
3797// \\ .more() //
3798// \\ .more().more() //
3799// \\ .more() //
3800// \\ // .more() //
3801// \\ .more() //
3802// \\ .more();
3803// \\ data: Data,
3804// \\};
3805// \\
3806// \\pub const str = struct {
3807// \\ pub const Thing = more.more //
3808// \\ .more() //
3809// \\ // .more() //
3810// \\ // .more() //
3811// \\ // .more() //
3812// \\ .more() //
3813// \\ .more();
3814// \\ data: Data,
3815// \\};
3816// \\
3817// \\pub const str = struct {
3818// \\ pub const Thing = more //
3819// \\ .more //
3820// \\ .more() //
3821// \\ .more();
3822// \\ data: Data,
3823// \\};
3824// \\
3825// );
3826//}
3793test "zig fmt: test comments in field access chain" {
3794 try testCanonical(
3795 \\pub const str = struct {
3796 \\ pub const Thing = more.more //
3797 \\ .more() //
3798 \\ .more().more() //
3799 \\ .more() //
3800 \\ // .more() //
3801 \\ .more() //
3802 \\ .more();
3803 \\ data: Data,
3804 \\};
3805 \\
3806 \\pub const str = struct {
3807 \\ pub const Thing = more.more //
3808 \\ .more() //
3809 \\ // .more() //
3810 \\ // .more() //
3811 \\ // .more() //
3812 \\ .more() //
3813 \\ .more();
3814 \\ data: Data,
3815 \\};
3816 \\
3817 \\pub const str = struct {
3818 \\ pub const Thing = more //
3819 \\ .more //
3820 \\ .more() //
3821 \\ .more();
3822 \\ data: Data,
3823 \\};
3824 \\
3825 );
3826}
3827
3828test "zig fmt: allow line break before field access" {
3829 try testCanonical(
3830 \\test {
3831 \\ const w = foo.bar().zippy(zag).iguessthisisok();
3832 \\
3833 \\ const x = foo
3834 \\ .bar()
3835 \\ . // comment
3836 \\ // comment
3837 \\ swooop().zippy(zag)
3838 \\ .iguessthisisok();
3839 \\
3840 \\ const y = view.output.root.server.input_manager.default_seat.wlr_seat.name;
3841 \\
3842 \\ const z = view.output.root.server
3843 \\ .input_manager //
3844 \\ .default_seat
3845 \\ . // comment
3846 \\ // another comment
3847 \\ wlr_seat.name;
3848 \\}
3849 \\
3850 );
3851 try testTransform(
3852 \\test {
3853 \\ const x = foo.
3854 \\ bar()
3855 \\ .zippy(zag).iguessthisisok();
3856 \\
3857 \\ const z = view.output.root.server.
3858 \\ input_manager.
3859 \\ default_seat.wlr_seat.name;
3860 \\}
3861 \\
3862 ,
3863 \\test {
3864 \\ const x = foo
3865 \\ .bar()
3866 \\ .zippy(zag).iguessthisisok();
3867 \\
3868 \\ const z = view.output.root.server
3869 \\ .input_manager
3870 \\ .default_seat.wlr_seat.name;
3871 \\}
3872 \\
3873 );
3874}
38273875
38283876test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
38293877 try testCanonical(
lib/std/zig/render.zig+23-1
......@@ -298,9 +298,31 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
298298 },
299299
300300 .field_access => {
301 const main_token = main_tokens[node];
301302 const field_access = datas[node];
303
302304 try renderExpression(gpa, ais, tree, field_access.lhs, .none);
303 try renderToken(ais, tree, main_tokens[node], .none);
305
306 // Allow a line break between the lhs and the dot if the lhs and rhs
307 // are on different lines.
308 const lhs_last_token = tree.lastToken(field_access.lhs);
309 const same_line = tree.tokensOnSameLine(lhs_last_token, main_token + 1);
310 if (!same_line) {
311 if (!hasComment(tree, lhs_last_token, main_token)) try ais.insertNewline();
312 ais.pushIndentOneShot();
313 }
314
315 try renderToken(ais, tree, main_token, .none);
316
317 // This check ensures that zag() is indented in the following example:
318 // const x = foo
319 // .bar()
320 // . // comment
321 // zag();
322 if (!same_line and hasComment(tree, main_token, main_token + 1)) {
323 ais.pushIndentOneShot();
324 }
325
304326 return renderToken(ais, tree, field_access.rhs, space);
305327 },
306328