| author | |
| committer | |
| log | 4cc1008c2d2b438b9847944898ad2d7cfbbdab8b |
| tree | 28ae3a1c5ad85493392f9999f8bf642056641e50 |
| parent | 3e61c45f8936f6211ae9b61b2b0358c560344f7b |
2 files changed, 50 insertions(+), 17 deletions(-)
std/zig/parser.zig+18-3| ... | @@ -4110,14 +4110,30 @@ pub const Parser = struct { | ... | @@ -4110,14 +4110,30 @@ pub const Parser = struct { |
| 4110 | }, | 4110 | }, |
| 4111 | ast.Node.Id.ErrorSetDecl => { | 4111 | ast.Node.Id.ErrorSetDecl => { |
| 4112 | const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base); | 4112 | const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base); |
| 4113 | try stream.print("error "); | 4113 | |
| 4114 | const decls = err_set_decl.decls.toSliceConst(); | ||
| 4115 | if (decls.len == 0) { | ||
| 4116 | try stream.write("error{}"); | ||
| 4117 | continue; | ||
| 4118 | } | ||
| 4119 | |||
| 4120 | if (decls.len == 1) blk: { | ||
| 4121 | const node = decls[0]; | ||
| 4122 | if (node.same_line_comment != null or node.doc_comments != null) break :blk; | ||
| 4123 | |||
| 4124 | try stream.write("error{"); | ||
| 4125 | try stack.append(RenderState { .Text = "}" }); | ||
| 4126 | try stack.append(RenderState { .Expression = node }); | ||
| 4127 | continue; | ||
| 4128 | } | ||
| 4129 | |||
| 4130 | try stream.write("error{"); | ||
| 4114 | 4131 | ||
| 4115 | try stack.append(RenderState { .Text = "}"}); | 4132 | try stack.append(RenderState { .Text = "}"}); |
| 4116 | try stack.append(RenderState.PrintIndent); | 4133 | try stack.append(RenderState.PrintIndent); |
| 4117 | try stack.append(RenderState { .Indent = indent }); | 4134 | try stack.append(RenderState { .Indent = indent }); |
| 4118 | try stack.append(RenderState { .Text = "\n"}); | 4135 | try stack.append(RenderState { .Text = "\n"}); |
| 4119 | 4136 | ||
| 4120 | const decls = err_set_decl.decls.toSliceConst(); | ||
| 4121 | var i = decls.len; | 4137 | var i = decls.len; |
| 4122 | while (i != 0) { | 4138 | while (i != 0) { |
| 4123 | i -= 1; | 4139 | i -= 1; |
| ... | @@ -4142,7 +4158,6 @@ pub const Parser = struct { | ... | @@ -4142,7 +4158,6 @@ pub const Parser = struct { |
| 4142 | }); | 4158 | }); |
| 4143 | } | 4159 | } |
| 4144 | try stack.append(RenderState { .Indent = indent + indent_delta}); | 4160 | try stack.append(RenderState { .Indent = indent + indent_delta}); |
| 4145 | try stack.append(RenderState { .Text = "{"}); | ||
| 4146 | }, | 4161 | }, |
| 4147 | ast.Node.Id.MultilineStringLiteral => { | 4162 | ast.Node.Id.MultilineStringLiteral => { |
| 4148 | const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base); | 4163 | const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base); |
std/zig/parser_test.zig+32-14| ... | @@ -1,5 +1,35 @@ | ... | @@ -1,5 +1,35 @@ |
| 1 | test "zig fmt: union(enum(u32)) with assigned enum values" { | 1 | test "zig fmt: error set declaration" { |
| 2 | try testCanonical( | 2 | try testCanonical( |
| 3 | \\const E = error{ | ||
| 4 | \\ A, | ||
| 5 | \\ B, | ||
| 6 | \\ | ||
| 7 | \\ C, | ||
| 8 | \\}; | ||
| 9 | \\ | ||
| 10 | \\const Error = error{ | ||
| 11 | \\ /// no more memory | ||
| 12 | \\ OutOfMemory, | ||
| 13 | \\}; | ||
| 14 | \\ | ||
| 15 | \\const Error = error{ | ||
| 16 | \\ /// no more memory | ||
| 17 | \\ OutOfMemory, | ||
| 18 | \\ | ||
| 19 | \\ /// another | ||
| 20 | \\ Another, | ||
| 21 | \\ | ||
| 22 | \\ // end | ||
| 23 | \\}; | ||
| 24 | \\ | ||
| 25 | \\const Error = error{OutOfMemory}; | ||
| 26 | \\const Error = error{}; | ||
| 27 | \\ | ||
| 28 | ); | ||
| 29 | } | ||
| 30 | |||
| 31 | test "zig fmt: union(enum(u32)) with assigned enum values" { | ||
| 32 | try testCanonical( | ||
| 3 | \\const MultipleChoice = union(enum(u32)) { | 33 | \\const MultipleChoice = union(enum(u32)) { |
| 4 | \\ A = 20, | 34 | \\ A = 20, |
| 5 | \\ B = 40, | 35 | \\ B = 40, |
| ... | @@ -23,7 +53,7 @@ test "zig fmt: labeled suspend" { | ... | @@ -23,7 +53,7 @@ test "zig fmt: labeled suspend" { |
| 23 | 53 | ||
| 24 | test "zig fmt: comments before error set decl" { | 54 | test "zig fmt: comments before error set decl" { |
| 25 | try testCanonical( | 55 | try testCanonical( |
| 26 | \\const UnexpectedError = error { | 56 | \\const UnexpectedError = error{ |
| 27 | \\ /// The Operating System returned an undocumented error code. | 57 | \\ /// The Operating System returned an undocumented error code. |
| 28 | \\ Unexpected, | 58 | \\ Unexpected, |
| 29 | \\ // another | 59 | \\ // another |
| ... | @@ -601,18 +631,6 @@ test "zig fmt: union declaration" { | ... | @@ -601,18 +631,6 @@ test "zig fmt: union declaration" { |
| 601 | ); | 631 | ); |
| 602 | } | 632 | } |
| 603 | 633 | ||
| 604 | test "zig fmt: error set declaration" { | ||
| 605 | try testCanonical( | ||
| 606 | \\const E = error { | ||
| 607 | \\ A, | ||
| 608 | \\ B, | ||
| 609 | \\ | ||
| 610 | \\ C, | ||
| 611 | \\}; | ||
| 612 | \\ | ||
| 613 | ); | ||
| 614 | } | ||
| 615 | |||
| 616 | test "zig fmt: arrays" { | 634 | test "zig fmt: arrays" { |
| 617 | try testCanonical( | 635 | try testCanonical( |
| 618 | \\test "test array" { | 636 | \\test "test array" { |