From 71badebd08a1e5da9326fc7126c4de0fba4ae3d0 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 May 2018 21:28:32 -0400 Subject: zig fmt: respect line breaks after infix operators --- std/zig/parser_test.zig | 17 +++++++++++++++++ std/zig/render.zig | 12 +++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'std') 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| { -- cgit v1.2.3