Commit 71badebd08

Andrew Kelley <superjoe30@gmail.com>
2018-05-29 03:28:32
zig fmt: respect line breaks after infix operators
1 parent 354ab1c
Changed files (2)
std/zig/parser_test.zig
@@ -1,3 +1,20 @@
+test "zig fmt: respect line breaks after infix operators" {
+    try testCanonical(
+        \\comptime {
+        \\    self.crc =
+        \\        lookup_tables[0][p[7]] ^
+        \\        lookup_tables[1][p[6]] ^
+        \\        lookup_tables[2][p[5]] ^
+        \\        lookup_tables[3][p[4]] ^
+        \\        lookup_tables[4][@truncate(u8, self.crc >> 24)] ^
+        \\        lookup_tables[5][@truncate(u8, self.crc >> 16)] ^
+        \\        lookup_tables[6][@truncate(u8, self.crc >> 8)] ^
+        \\        lookup_tables[7][@truncate(u8, self.crc >> 0)];
+        \\}
+        \\
+    );
+}
+
 test "zig fmt: fn decl with trailing comma" {
     try testTransform(
         \\fn foo(a: i32, b: i32,) void {}
std/zig/render.zig
@@ -254,7 +254,17 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
                 else => Space.Space,
             };
             try renderExpression(allocator, stream, tree, indent, infix_op_node.lhs, op_space);
-            try renderToken(tree, stream, infix_op_node.op_token, indent, op_space);
+
+            const after_op_space = blk: {
+                const loc = tree.tokenLocation(tree.tokens.at(infix_op_node.op_token).end,
+                    tree.nextToken(infix_op_node.op_token));
+                break :blk if (loc.line == 0) op_space else Space.Newline;
+            };
+
+            try renderToken(tree, stream, infix_op_node.op_token, indent, after_op_space);
+            if (after_op_space == Space.Newline) {
+                try stream.writeByteNTimes(' ', indent + indent_delta);
+            }
 
             switch (infix_op_node.op) {
                 ast.Node.InfixOp.Op.Catch => |maybe_payload| if (maybe_payload) |payload| {