Skip to content

Commit

Permalink
spaces around slice operator if operands are infix
Browse files Browse the repository at this point in the history
See #1003
  • Loading branch information
andrewrk committed May 30, 2018
1 parent 2c96f19 commit 93b51b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions std/zig/parser_test.zig
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 93b51b0

Please sign in to comment.