aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2021-02-23 23:17:00 +0100
committerIsaac Freund <ifreund@ifreund.xyz>2021-02-23 23:17:38 +0100
commit4ee368c4b33f5cf286c575e94535deaf59659895 (patch)
treec8c95d0a2eba7adf9da0e7e50a8c25343183de6d /lib
parentca9259340d42d92c8e44cb814effae447ee64a24 (diff)
downloadzig-4ee368c4b33f5cf286c575e94535deaf59659895.tar.gz
zig-4ee368c4b33f5cf286c575e94535deaf59659895.zip
zig fmt: comments/line breaks in field access chain
Diffstat (limited to 'lib')
-rw-r--r--lib/std/zig/parser_test.zig116
-rw-r--r--lib/std/zig/render.zig24
2 files changed, 105 insertions, 35 deletions
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 3fd7fbdb74..9701cd4e29 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -3790,40 +3790,88 @@ test "zig fmt: comments in ternary ifs" {
);
}
-//test "zig fmt: test comments in field access chain" {
-// try testCanonical(
-// \\pub const str = struct {
-// \\ pub const Thing = more.more //
-// \\ .more() //
-// \\ .more().more() //
-// \\ .more() //
-// \\ // .more() //
-// \\ .more() //
-// \\ .more();
-// \\ data: Data,
-// \\};
-// \\
-// \\pub const str = struct {
-// \\ pub const Thing = more.more //
-// \\ .more() //
-// \\ // .more() //
-// \\ // .more() //
-// \\ // .more() //
-// \\ .more() //
-// \\ .more();
-// \\ data: Data,
-// \\};
-// \\
-// \\pub const str = struct {
-// \\ pub const Thing = more //
-// \\ .more //
-// \\ .more() //
-// \\ .more();
-// \\ data: Data,
-// \\};
-// \\
-// );
-//}
+test "zig fmt: test comments in field access chain" {
+ try testCanonical(
+ \\pub const str = struct {
+ \\ pub const Thing = more.more //
+ \\ .more() //
+ \\ .more().more() //
+ \\ .more() //
+ \\ // .more() //
+ \\ .more() //
+ \\ .more();
+ \\ data: Data,
+ \\};
+ \\
+ \\pub const str = struct {
+ \\ pub const Thing = more.more //
+ \\ .more() //
+ \\ // .more() //
+ \\ // .more() //
+ \\ // .more() //
+ \\ .more() //
+ \\ .more();
+ \\ data: Data,
+ \\};
+ \\
+ \\pub const str = struct {
+ \\ pub const Thing = more //
+ \\ .more //
+ \\ .more() //
+ \\ .more();
+ \\ data: Data,
+ \\};
+ \\
+ );
+}
+
+test "zig fmt: allow line break before field access" {
+ try testCanonical(
+ \\test {
+ \\ const w = foo.bar().zippy(zag).iguessthisisok();
+ \\
+ \\ const x = foo
+ \\ .bar()
+ \\ . // comment
+ \\ // comment
+ \\ swooop().zippy(zag)
+ \\ .iguessthisisok();
+ \\
+ \\ const y = view.output.root.server.input_manager.default_seat.wlr_seat.name;
+ \\
+ \\ const z = view.output.root.server
+ \\ .input_manager //
+ \\ .default_seat
+ \\ . // comment
+ \\ // another comment
+ \\ wlr_seat.name;
+ \\}
+ \\
+ );
+ try testTransform(
+ \\test {
+ \\ const x = foo.
+ \\ bar()
+ \\ .zippy(zag).iguessthisisok();
+ \\
+ \\ const z = view.output.root.server.
+ \\ input_manager.
+ \\ default_seat.wlr_seat.name;
+ \\}
+ \\
+ ,
+ \\test {
+ \\ const x = foo
+ \\ .bar()
+ \\ .zippy(zag).iguessthisisok();
+ \\
+ \\ const z = view.output.root.server
+ \\ .input_manager
+ \\ .default_seat.wlr_seat.name;
+ \\}
+ \\
+ );
+}
test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
try testCanonical(
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index 4f7cadb437..bd25868e12 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -298,9 +298,31 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
},
.field_access => {
+ const main_token = main_tokens[node];
const field_access = datas[node];
+
try renderExpression(gpa, ais, tree, field_access.lhs, .none);
- try renderToken(ais, tree, main_tokens[node], .none);
+
+ // Allow a line break between the lhs and the dot if the lhs and rhs
+ // are on different lines.
+ const lhs_last_token = tree.lastToken(field_access.lhs);
+ const same_line = tree.tokensOnSameLine(lhs_last_token, main_token + 1);
+ if (!same_line) {
+ if (!hasComment(tree, lhs_last_token, main_token)) try ais.insertNewline();
+ ais.pushIndentOneShot();
+ }
+
+ try renderToken(ais, tree, main_token, .none);
+
+ // This check ensures that zag() is indented in the following example:
+ // const x = foo
+ // .bar()
+ // . // comment
+ // zag();
+ if (!same_line and hasComment(tree, main_token, main_token + 1)) {
+ ais.pushIndentOneShot();
+ }
+
return renderToken(ais, tree, field_access.rhs, space);
},