Skip to content

Commit 93b51b0

Browse files
committed
spaces around slice operator if operands are infix
See #1003
1 parent 2c96f19 commit 93b51b0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

std/zig/parser_test.zig

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
test "zig fmt: spaces around slice operator" {
2+
try testCanonical(
3+
\\var a = b[c..d];
4+
\\var a = b[c + 1 .. d];
5+
\\var a = b[c .. d + 1];
6+
\\
7+
);
8+
}
9+
110
test "zig fmt: async call in if condition" {
211
try testCanonical(
312
\\comptime {

std/zig/render.zig

+6-2
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,13 @@ fn renderExpression(
530530
const lbracket = tree.prevToken(range.start.firstToken());
531531
const dotdot = tree.nextToken(range.start.lastToken());
532532

533+
const spaces_around_op = range.start.id == ast.Node.Id.InfixOp or
534+
(if (range.end) |end| end.id == ast.Node.Id.InfixOp else false);
535+
const op_space = if (spaces_around_op) Space.Space else Space.None;
536+
533537
try renderToken(tree, stream, lbracket, indent, start_col, Space.None); // [
534-
try renderExpression(allocator, stream, tree, indent, start_col, range.start, Space.None);
535-
try renderToken(tree, stream, dotdot, indent, start_col, Space.None); // ..
538+
try renderExpression(allocator, stream, tree, indent, start_col, range.start, op_space);
539+
try renderToken(tree, stream, dotdot, indent, start_col, op_space); // ..
536540
if (range.end) |end| {
537541
try renderExpression(allocator, stream, tree, indent, start_col, end, Space.None);
538542
}

0 commit comments

Comments
 (0)