aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-05-30 15:33:58 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-05-30 15:33:58 -0400
commit93b51b0e4023c05f1ef40371b8e72004cb55f046 (patch)
tree99d08b1618adb14daa3f5a1d0ff4c42da862ea2d
parent2c96f19fd3ed312e5cb0ac8006b73e73abf4a98d (diff)
downloadzig-93b51b0e4023c05f1ef40371b8e72004cb55f046.tar.gz
zig-93b51b0e4023c05f1ef40371b8e72004cb55f046.zip
spaces around slice operator if operands are infix
See #1003
-rw-r--r--std/zig/parser_test.zig9
-rw-r--r--std/zig/render.zig8
2 files changed, 15 insertions, 2 deletions
diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig
index eee6b12767..52bbaa8b4d 100644
--- a/std/zig/parser_test.zig
+++ b/std/zig/parser_test.zig
@@ -1,3 +1,12 @@
+test "zig fmt: spaces around slice operator" {
+ try testCanonical(
+ \\var a = b[c..d];
+ \\var a = b[c + 1 .. d];
+ \\var a = b[c .. d + 1];
+ \\
+ );
+}
+
test "zig fmt: async call in if condition" {
try testCanonical(
\\comptime {
diff --git a/std/zig/render.zig b/std/zig/render.zig
index 657fcae8bb..bd20346bec 100644
--- a/std/zig/render.zig
+++ b/std/zig/render.zig
@@ -530,9 +530,13 @@ fn renderExpression(
const lbracket = tree.prevToken(range.start.firstToken());
const dotdot = tree.nextToken(range.start.lastToken());
+ const spaces_around_op = range.start.id == ast.Node.Id.InfixOp or
+ (if (range.end) |end| end.id == ast.Node.Id.InfixOp else false);
+ const op_space = if (spaces_around_op) Space.Space else Space.None;
+
try renderToken(tree, stream, lbracket, indent, start_col, Space.None); // [
- try renderExpression(allocator, stream, tree, indent, start_col, range.start, Space.None);
- try renderToken(tree, stream, dotdot, indent, start_col, Space.None); // ..
+ try renderExpression(allocator, stream, tree, indent, start_col, range.start, op_space);
+ try renderToken(tree, stream, dotdot, indent, start_col, op_space); // ..
if (range.end) |end| {
try renderExpression(allocator, stream, tree, indent, start_col, end, Space.None);
}