zig fmt: comments before switch prong

This commit is contained in:
Andrew Kelley 2018-04-29 21:47:54 -04:00
parent a912c7d75f
commit f04015c080
2 changed files with 21 additions and 2 deletions

View File

@ -1536,10 +1536,11 @@ pub const Parser = struct {
continue; continue;
} }
const comments = try self.eatComments(arena);
const node = try arena.construct(ast.Node.SwitchCase { const node = try arena.construct(ast.Node.SwitchCase {
.base = ast.Node { .base = ast.Node {
.id = ast.Node.Id.SwitchCase, .id = ast.Node.Id.SwitchCase,
.before_comments = null, .before_comments = comments,
.same_line_comment = null, .same_line_comment = null,
}, },
.items = ArrayList(&ast.Node).init(arena), .items = ArrayList(&ast.Node).init(arena),
@ -1551,6 +1552,7 @@ pub const Parser = struct {
try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } }); try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } });
try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } }); try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
try stack.append(State { .SwitchCaseFirstItem = &node.items }); try stack.append(State { .SwitchCaseFirstItem = &node.items });
continue; continue;
}, },
@ -4123,7 +4125,9 @@ pub const Parser = struct {
ast.Node.Id.SwitchCase => { ast.Node.Id.SwitchCase => {
const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base); const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);
try stack.append(RenderState { .PrintSameLineComment = switch_case.base.same_line_comment }); try self.renderComments(stream, base, indent);
try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment });
try stack.append(RenderState { .Text = "," }); try stack.append(RenderState { .Text = "," });
try stack.append(RenderState { .Expression = switch_case.expr }); try stack.append(RenderState { .Expression = switch_case.expr });
if (switch_case.payload) |payload| { if (switch_case.payload) |payload| {

View File

@ -1,3 +1,18 @@
test "zig fmt: comments before switch prong" {
try testCanonical(
\\test "" {
\\ switch (err) {
\\ error.PathAlreadyExists => continue,
\\
\\ // comment 1
\\ // comment 2
\\ else => return err,
\\ }
\\}
\\
);
}
test "zig fmt: same-line comment after switch prong" { test "zig fmt: same-line comment after switch prong" {
try testCanonical( try testCanonical(
\\test "" { \\test "" {