aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLachlan Easton <lachlan@lakebythewoods.xyz>2020-08-30 10:35:18 +1000
committerLachlan Easton <lachlan@lakebythewoods.xyz>2020-09-09 21:54:42 +1000
commit283d441c19d5bafa01a7df24db277a6b08a86c00 (patch)
tree56830b0ac654af46a161323ff180648dbaaf12b0
parent7d487a41627658bfda37f1f06f8e453c2b576b9c (diff)
downloadzig-283d441c19d5bafa01a7df24db277a6b08a86c00.tar.gz
zig-283d441c19d5bafa01a7df24db277a6b08a86c00.zip
zig fmt: fix #3978, fix #2748
-rw-r--r--lib/std/zig/parser_test.zig53
-rw-r--r--lib/std/zig/render.zig15
2 files changed, 65 insertions, 3 deletions
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 36ceb400dc..1aec1c3567 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -3321,6 +3321,59 @@ test "zig fmt: Don't add extra newline after if" {
);
}
+test "zig fmt: comments in ternary ifs" {
+ try testCanonical(
+ \\const x = if (true) {
+ \\ 1;
+ \\} else if (false)
+ \\ // Comment
+ \\ 0;
+ \\const y = if (true)
+ \\ // Comment
+ \\ 1
+ \\else
+ \\ 0;
+ \\
+ \\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
+ \\
+ );
+}
+
+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,
+ \\};
+ \\
+ );
+}
+
const std = @import("std");
const mem = std.mem;
const warn = std.debug.warn;
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index 237ca07d2b..4432d08787 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -522,8 +522,12 @@ fn renderExpression(
break :blk if (loc.line == 0) op_space else Space.Newline;
};
- try renderToken(tree, ais, infix_op_node.op_token, after_op_space);
- ais.pushIndentOneShot();
+ {
+ try ais.pushIndent();
+ defer ais.popIndent();
+ try renderToken(tree, ais, infix_op_node.op_token, after_op_space);
+ }
+ try ais.pushIndentOneShot();
return renderExpression(allocator, ais, tree, infix_op_node.rhs, space);
},
@@ -1873,7 +1877,12 @@ fn renderExpression(
if (src_has_newline) {
const after_rparen_space = if (if_node.payload == null) Space.Newline else Space.Space;
- try renderToken(tree, ais, rparen, after_rparen_space); // )
+
+ {
+ try ais.pushIndent();
+ defer ais.popIndent();
+ try renderToken(tree, ais, rparen, after_rparen_space); // )
+ }
if (if_node.payload) |payload| {
try renderExpression(allocator, ais, tree, payload, Space.Newline);