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 {...@@ -1891,12 +1891,13 @@ pub const Parser = struct {
1891 }1891 }
1892 );1892 );
18931893
1894 stack.append(State {1894 stack.append(State {.LookForSameLineCommentDirect = &node.base }) catch unreachable;
1895 try stack.append(State {
1895 .ExpectTokenSave = ExpectTokenSave {1896 .ExpectTokenSave = ExpectTokenSave {
1896 .id = Token.Id.Pipe,1897 .id = Token.Id.Pipe,
1897 .ptr = &node.rpipe,1898 .ptr = &node.rpipe,
1898 }1899 }
1899 }) catch unreachable;1900 });
1900 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });1901 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });
1901 try stack.append(State {1902 try stack.append(State {
1902 .OptionalTokenSave = OptionalTokenSave {1903 .OptionalTokenSave = OptionalTokenSave {
...@@ -3122,6 +3123,7 @@ pub const Parser = struct {...@@ -3122,6 +3123,7 @@ pub const Parser = struct {
3122 stack.append(State { .Else = &node.@"else" }) catch unreachable;3123 stack.append(State { .Else = &node.@"else" }) catch unreachable;
3123 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });3124 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });
3124 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });3125 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
3126 try stack.append(State { .LookForSameLineComment = &node.condition });
3125 try stack.append(State { .ExpectToken = Token.Id.RParen });3127 try stack.append(State { .ExpectToken = Token.Id.RParen });
3126 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });3128 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });
3127 try stack.append(State { .ExpectToken = Token.Id.LParen });3129 try stack.append(State { .ExpectToken = Token.Id.LParen });
...@@ -3460,6 +3462,7 @@ pub const Parser = struct {...@@ -3460,6 +3462,7 @@ pub const Parser = struct {
3460 PrintIndent,3462 PrintIndent,
3461 Indent: usize,3463 Indent: usize,
3462 PrintSameLineComment: ?&Token,3464 PrintSameLineComment: ?&Token,
3465 PrintLineComment: &Token,
3463 };3466 };
34643467
3465 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {3468 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {
...@@ -4517,7 +4520,18 @@ pub const Parser = struct {...@@ -4517,7 +4520,18 @@ pub const Parser = struct {
4517 }4520 }
4518 }4521 }
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
4521 try stack.append(RenderState { .Text = " " });4535 try stack.append(RenderState { .Text = " " });
45224536
4523 if (if_node.payload) |payload| {4537 if (if_node.payload) |payload| {
...@@ -4678,6 +4692,9 @@ pub const Parser = struct {...@@ -4678,6 +4692,9 @@ pub const Parser = struct {
4678 const comment_token = maybe_comment ?? break :blk;4692 const comment_token = maybe_comment ?? break :blk;
4679 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));4693 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));
4680 },4694 },
4695 RenderState.PrintLineComment => |comment_token| {
4696 try stream.write(self.tokenizer.getTokenSlice(comment_token));
4697 },
4681 }4698 }
4682 }4699 }
4683 }4700 }
std/zig/parser_test.zig+18-6
...@@ -1,6 +1,14 @@...@@ -1,6 +1,14 @@
1// TODO1test "zig fmt: same-line comment after non-block if expression" {
2//if (sr > n_uword_bits - 1) // d > r2 try testCanonical(
3// return 0;3 \\comptime {
4 \\ if (sr > n_uword_bits - 1) {
5 \\ // d > r
6 \\ return 0;
7 \\ }
8 \\}
9 \\
10 );
11}
412
5test "zig fmt: switch with empty body" {13test "zig fmt: switch with empty body" {
6 try testCanonical(14 try testCanonical(
...@@ -1108,15 +1116,15 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 {...@@ -1108,15 +1116,15 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 {
1108 return buffer.toOwnedSlice();1116 return buffer.toOwnedSlice();
1109}1117}
11101118
1111fn testCanonical(source: []const u8) !void {1119fn testTransform(source: []const u8, expected_source: []const u8) !void {
1112 const needed_alloc_count = x: {1120 const needed_alloc_count = x: {
1113 // Try it once with unlimited memory, make sure it works1121 // Try it once with unlimited memory, make sure it works
1114 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);1122 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
1115 var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize));1123 var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize));
1116 const result_source = try testParse(source, &failing_allocator.allocator);1124 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)) {
1118 warn("\n====== expected this output: =========\n");1126 warn("\n====== expected this output: =========\n");
1119 warn("{}", source);1127 warn("{}", expected_source);
1120 warn("\n======== instead found this: =========\n");1128 warn("\n======== instead found this: =========\n");
1121 warn("{}", result_source);1129 warn("{}", result_source);
1122 warn("\n======================================\n");1130 warn("\n======================================\n");
...@@ -1147,3 +1155,7 @@ fn testCanonical(source: []const u8) !void {...@@ -1147,3 +1155,7 @@ fn testCanonical(source: []const u8) !void {
1147 }1155 }
1148}1156}
11491157
1158fn testCanonical(source: []const u8) !void {
1159 return testTransform(source, source);
1160}
1161