authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-04 18:35:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-04 18:35:43-04:00
log4d6d2f1cd2a46e95b998b6bbc1effc72b2f4923c
treec7a3c8be0680d0f1e80f205bdcd90c682380a2cd
parent0fc8885a8df6a44455535ba50c160bca90e8d998

zig fmt: same-line comment after non-block if expression


2 files changed, 38 insertions(+), 9 deletions(-)

std/zig/parser.zig+20-3
......@@ -1891,12 +1891,13 @@ pub const Parser = struct {
18911891 }
18921892 );
18931893
1894 stack.append(State {
1894 stack.append(State {.LookForSameLineCommentDirect = &node.base }) catch unreachable;
1895 try stack.append(State {
18951896 .ExpectTokenSave = ExpectTokenSave {
18961897 .id = Token.Id.Pipe,
18971898 .ptr = &node.rpipe,
18981899 }
1899 }) catch unreachable;
1900 });
19001901 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });
19011902 try stack.append(State {
19021903 .OptionalTokenSave = OptionalTokenSave {
......@@ -3122,6 +3123,7 @@ pub const Parser = struct {
31223123 stack.append(State { .Else = &node.@"else" }) catch unreachable;
31233124 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });
31243125 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
3126 try stack.append(State { .LookForSameLineComment = &node.condition });
31253127 try stack.append(State { .ExpectToken = Token.Id.RParen });
31263128 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });
31273129 try stack.append(State { .ExpectToken = Token.Id.LParen });
......@@ -3460,6 +3462,7 @@ pub const Parser = struct {
34603462 PrintIndent,
34613463 Indent: usize,
34623464 PrintSameLineComment: ?&Token,
3465 PrintLineComment: &Token,
34633466 };
34643467
34653468 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {
......@@ -4517,7 +4520,18 @@ pub const Parser = struct {
45174520 }
45184521 }
45194522
4520 try stack.append(RenderState { .Expression = if_node.body });
4523 if (if_node.condition.same_line_comment) |comment| {
4524 try stack.append(RenderState { .Indent = indent });
4525 try stack.append(RenderState { .Expression = if_node.body });
4526 try stack.append(RenderState.PrintIndent);
4527 try stack.append(RenderState { .Indent = indent + indent_delta });
4528 try stack.append(RenderState { .Text = "\n" });
4529 try stack.append(RenderState { .PrintLineComment = comment });
4530 } else {
4531 try stack.append(RenderState { .Expression = if_node.body });
4532 }
4533
4534
45214535 try stack.append(RenderState { .Text = " " });
45224536
45234537 if (if_node.payload) |payload| {
......@@ -4678,6 +4692,9 @@ pub const Parser = struct {
46784692 const comment_token = maybe_comment ?? break :blk;
46794693 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));
46804694 },
4695 RenderState.PrintLineComment => |comment_token| {
4696 try stream.write(self.tokenizer.getTokenSlice(comment_token));
4697 },
46814698 }
46824699 }
46834700 }
std/zig/parser_test.zig+18-6
......@@ -1,6 +1,14 @@
1// TODO
2//if (sr > n_uword_bits - 1) // d > r
3// return 0;
1test "zig fmt: same-line comment after non-block if expression" {
2 try testCanonical(
3 \\comptime {
4 \\ if (sr > n_uword_bits - 1) {
5 \\ // d > r
6 \\ return 0;
7 \\ }
8 \\}
9 \\
10 );
11}
412
513test "zig fmt: switch with empty body" {
614 try testCanonical(
......@@ -1108,15 +1116,15 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 {
11081116 return buffer.toOwnedSlice();
11091117}
11101118
1111fn testCanonical(source: []const u8) !void {
1119fn testTransform(source: []const u8, expected_source: []const u8) !void {
11121120 const needed_alloc_count = x: {
11131121 // Try it once with unlimited memory, make sure it works
11141122 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
11151123 var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize));
11161124 const result_source = try testParse(source, &failing_allocator.allocator);
1117 if (!mem.eql(u8, result_source, source)) {
1125 if (!mem.eql(u8, result_source, expected_source)) {
11181126 warn("\n====== expected this output: =========\n");
1119 warn("{}", source);
1127 warn("{}", expected_source);
11201128 warn("\n======== instead found this: =========\n");
11211129 warn("{}", result_source);
11221130 warn("\n======================================\n");
......@@ -1147,3 +1155,7 @@ fn testCanonical(source: []const u8) !void {
11471155 }
11481156}
11491157
1158fn testCanonical(source: []const u8) !void {
1159 return testTransform(source, source);
1160}
1161