authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 21:27:44-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-29 21:27:44-04:00
loga912c7d75f61fb127ff8552e7a10ffe4672ac1aa
treea506b13b193db14c26a15baf6f40b05559d15a7a
parentc53209a8a8cee014382a735b3baa7cd439106c6f

zig fmt: same-line comment after switch prong


2 files changed, 36 insertions(+), 17 deletions(-)

std/zig/parser.zig+24-17
...@@ -1505,11 +1505,11 @@ pub const Parser = struct {...@@ -1505,11 +1505,11 @@ pub const Parser = struct {
1505 if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| {1505 if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| {
1506 container_decl.rbrace_token = end;1506 container_decl.rbrace_token = end;
1507 continue;1507 continue;
1508 } else {
1509 try self.lookForSameLineComment(arena, container_decl.fields_and_decls.toSlice()[container_decl.fields_and_decls.len - 1]);
1510 try stack.append(State { .ContainerDecl = container_decl });
1511 continue;
1512 }1508 }
1509
1510 try self.lookForSameLineComment(arena, container_decl.fields_and_decls.toSlice()[container_decl.fields_and_decls.len - 1]);
1511 try stack.append(State { .ContainerDecl = container_decl });
1512 continue;
1513 },1513 },
1514 State.IdentifierListItemOrEnd => |list_state| {1514 State.IdentifierListItemOrEnd => |list_state| {
1515 if (self.eatToken(Token.Id.RBrace)) |rbrace| {1515 if (self.eatToken(Token.Id.RBrace)) |rbrace| {
...@@ -1536,30 +1536,36 @@ pub const Parser = struct {...@@ -1536,30 +1536,36 @@ pub const Parser = struct {
1536 continue;1536 continue;
1537 }1537 }
15381538
1539 const node = try self.createNode(arena, ast.Node.SwitchCase,1539 const node = try arena.construct(ast.Node.SwitchCase {
1540 ast.Node.SwitchCase {1540 .base = ast.Node {
1541 .base = undefined,1541 .id = ast.Node.Id.SwitchCase,
1542 .items = ArrayList(&ast.Node).init(arena),1542 .before_comments = null,
1543 .payload = null,1543 .same_line_comment = null,
1544 .expr = undefined,1544 },
1545 }1545 .items = ArrayList(&ast.Node).init(arena),
1546 );1546 .payload = null,
1547 .expr = undefined,
1548 });
1547 try list_state.list.append(node);1549 try list_state.list.append(node);
1548 stack.append(State { .SwitchCaseCommaOrEnd = list_state }) catch unreachable;1550 try stack.append(State { .SwitchCaseCommaOrEnd = list_state });
1549 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } });1551 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } });
1550 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });1552 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
1551 try stack.append(State { .SwitchCaseFirstItem = &node.items });1553 try stack.append(State { .SwitchCaseFirstItem = &node.items });
1552 continue;1554 continue;
1553 },1555 },
1556
1554 State.SwitchCaseCommaOrEnd => |list_state| {1557 State.SwitchCaseCommaOrEnd => |list_state| {
1555 if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| {1558 if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| {
1556 *list_state.ptr = end;1559 *list_state.ptr = end;
1557 continue;1560 continue;
1558 } else {
1559 stack.append(State { .SwitchCaseOrEnd = list_state }) catch unreachable;
1560 continue;
1561 }1561 }
1562
1563 const switch_case = list_state.list.toSlice()[list_state.list.len - 1];
1564 try self.lookForSameLineComment(arena, &switch_case.base);
1565 try stack.append(State { .SwitchCaseOrEnd = list_state });
1566 continue;
1562 },1567 },
1568
1563 State.SwitchCaseFirstItem => |case_items| {1569 State.SwitchCaseFirstItem => |case_items| {
1564 const token = self.getNextToken();1570 const token = self.getNextToken();
1565 if (token.id == Token.Id.Keyword_else) {1571 if (token.id == Token.Id.Keyword_else) {
...@@ -4095,7 +4101,6 @@ pub const Parser = struct {...@@ -4095,7 +4101,6 @@ pub const Parser = struct {
4095 while (i != 0) {4101 while (i != 0) {
4096 i -= 1;4102 i -= 1;
4097 const node = cases[i];4103 const node = cases[i];
4098 try stack.append(RenderState { .Text = ","});
4099 try stack.append(RenderState { .Expression = &node.base});4104 try stack.append(RenderState { .Expression = &node.base});
4100 try stack.append(RenderState.PrintIndent);4105 try stack.append(RenderState.PrintIndent);
4101 try stack.append(RenderState {4106 try stack.append(RenderState {
...@@ -4118,6 +4123,8 @@ pub const Parser = struct {...@@ -4118,6 +4123,8 @@ pub const Parser = struct {
4118 ast.Node.Id.SwitchCase => {4123 ast.Node.Id.SwitchCase => {
4119 const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);4124 const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);
41204125
4126 try stack.append(RenderState { .PrintSameLineComment = switch_case.base.same_line_comment });
4127 try stack.append(RenderState { .Text = "," });
4121 try stack.append(RenderState { .Expression = switch_case.expr });4128 try stack.append(RenderState { .Expression = switch_case.expr });
4122 if (switch_case.payload) |payload| {4129 if (switch_case.payload) |payload| {
4123 try stack.append(RenderState { .Text = " " });4130 try stack.append(RenderState { .Text = " " });
std/zig/parser_test.zig+12
...@@ -1,3 +1,15 @@...@@ -1,3 +1,15 @@
1test "zig fmt: same-line comment after switch prong" {
2 try testCanonical(
3 \\test "" {
4 \\ switch (err) {
5 \\ error.PathAlreadyExists => {}, // comment 2
6 \\ else => return err, // comment 1
7 \\ }
8 \\}
9 \\
10 );
11}
12
1test "zig fmt: comments before var decl in struct" {13test "zig fmt: comments before var decl in struct" {
2 try testCanonical(14 try testCanonical(
3 \\pub const vfs_cap_data = extern struct {15 \\pub const vfs_cap_data = extern struct {