Skip to content

Commit

Permalink
zig fmt: field access does not cause spaces for slicing
Browse files Browse the repository at this point in the history
See #1003
  • Loading branch information
andrewrk committed May 30, 2018
1 parent 84b1842 commit b082cd4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ test "zig fmt: spaces around slice operator" {
try testCanonical(
\\var a = b[c..d];
\\var a = b[c + 1 .. d];
\\var a = b[c + 1 ..];
\\var a = b[c .. d + 1];
\\var a = b[c.a..d.e];
\\
);
}
Expand Down
19 changes: 14 additions & 5 deletions std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,14 @@ 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;
const after_start_space_bool = nodeCausesSliceOpSpace(range.start) or
(if (range.end) |end| nodeCausesSliceOpSpace(end) else false);
const after_start_space = if (after_start_space_bool) Space.Space else Space.None;
const after_op_space = if (range.end != null) after_start_space else Space.None;

try renderToken(tree, stream, lbracket, 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); // ..
try renderExpression(allocator, stream, tree, indent, start_col, range.start, after_start_space);
try renderToken(tree, stream, dotdot, indent, start_col, after_op_space); // ..
if (range.end) |end| {
try renderExpression(allocator, stream, tree, indent, start_col, end, Space.None);
}
Expand Down Expand Up @@ -1959,3 +1960,11 @@ fn nodeIsBlock(base: &const ast.Node) bool {
else => false,
};
}

fn nodeCausesSliceOpSpace(base: &ast.Node) bool {
const infix_op = base.cast(ast.Node.InfixOp) ?? return false;
return switch (infix_op.op) {
ast.Node.InfixOp.Op.Period => false,
else => true,
};
}

0 comments on commit b082cd4

Please sign in to comment.