diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-05-28 21:28:32 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-05-28 21:28:32 -0400 |
| commit | 71badebd08a1e5da9326fc7126c4de0fba4ae3d0 (patch) | |
| tree | 41d485179551ec61344789793d32a01853bdf9ec | |
| parent | 354ab1c5c8ec62c485fbc297817a0e70ab625a71 (diff) | |
| download | zig-71badebd08a1e5da9326fc7126c4de0fba4ae3d0.tar.gz zig-71badebd08a1e5da9326fc7126c4de0fba4ae3d0.zip | |
zig fmt: respect line breaks after infix operators
| -rw-r--r-- | std/zig/parser_test.zig | 17 | ||||
| -rw-r--r-- | std/zig/render.zig | 12 |
2 files changed, 28 insertions, 1 deletions
diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index edf1b002b0..f722c7f08f 100644 --- a/std/zig/parser_test.zig +++ b/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 {} diff --git a/std/zig/render.zig b/std/zig/render.zig index b8481b1348..56f3d24b8e 100644 --- a/std/zig/render.zig +++ b/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| { |
