Commit b082cd4580

Andrew Kelley <superjoe30@gmail.com>
2018-05-30 22:08:40
zig fmt: field access does not cause spaces for slicing
See #1003
1 parent 84b1842
Changed files (2)
std/zig/parser_test.zig
@@ -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];
         \\
     );
 }
std/zig/render.zig
@@ -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);
                     }
@@ -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,
+    };
+}