commit f04015c080b8e4e97166d6ac9356f818e766fc48 (tree)
parent a912c7d75f61fb127ff8552e7a10ffe4672ac1aa
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Sun, 29 Apr 2018 21:47:54 -0400
zig fmt: comments before switch prong
Diffstat:
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/std/zig/parser.zig b/std/zig/parser.zig
@@ -1536,10 +1536,11 @@ pub const Parser = struct {
continue;
}
+ const comments = try self.eatComments(arena);
const node = try arena.construct(ast.Node.SwitchCase {
.base = ast.Node {
.id = ast.Node.Id.SwitchCase,
- .before_comments = null,
+ .before_comments = comments,
.same_line_comment = null,
},
.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 { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
try stack.append(State { .SwitchCaseFirstItem = &node.items });
+
continue;
},
@@ -4123,7 +4125,9 @@ pub const Parser = struct {
ast.Node.Id.SwitchCase => {
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 { .Expression = switch_case.expr });
if (switch_case.payload) |payload| {
diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig
@@ -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" {
try testCanonical(
\\test "" {