Skip to content

Commit b082cd4

Browse files
committed
zig fmt: field access does not cause spaces for slicing
See #1003
1 parent 84b1842 commit b082cd4

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

std/zig/parser_test.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ test "zig fmt: spaces around slice operator" {
22
try testCanonical(
33
\\var a = b[c..d];
44
\\var a = b[c + 1 .. d];
5+
\\var a = b[c + 1 ..];
56
\\var a = b[c .. d + 1];
7+
\\var a = b[c.a..d.e];
68
\\
79
);
810
}

std/zig/render.zig

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,14 @@ 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;
533+
const after_start_space_bool = nodeCausesSliceOpSpace(range.start) or
534+
(if (range.end) |end| nodeCausesSliceOpSpace(end) else false);
535+
const after_start_space = if (after_start_space_bool) Space.Space else Space.None;
536+
const after_op_space = if (range.end != null) after_start_space else Space.None;
536537

537538
try renderToken(tree, stream, lbracket, 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); // ..
539+
try renderExpression(allocator, stream, tree, indent, start_col, range.start, after_start_space);
540+
try renderToken(tree, stream, dotdot, indent, start_col, after_op_space); // ..
540541
if (range.end) |end| {
541542
try renderExpression(allocator, stream, tree, indent, start_col, end, Space.None);
542543
}
@@ -1959,3 +1960,11 @@ fn nodeIsBlock(base: &const ast.Node) bool {
19591960
else => false,
19601961
};
19611962
}
1963+
1964+
fn nodeCausesSliceOpSpace(base: &ast.Node) bool {
1965+
const infix_op = base.cast(ast.Node.InfixOp) ?? return false;
1966+
return switch (infix_op.op) {
1967+
ast.Node.InfixOp.Op.Period => false,
1968+
else => true,
1969+
};
1970+
}

0 commit comments

Comments
 (0)