authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 19:16:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 19:16:46-04:00
log4cc1008c2d2b438b9847944898ad2d7cfbbdab8b
tree28ae3a1c5ad85493392f9999f8bf642056641e50
parent3e61c45f8936f6211ae9b61b2b0358c560344f7b

zig fmt: error set decls


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

std/zig/parser.zig+18-3
......@@ -4110,14 +4110,30 @@ pub const Parser = struct {
41104110 },
41114111 ast.Node.Id.ErrorSetDecl => {
41124112 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{");
41144131
41154132 try stack.append(RenderState { .Text = "}"});
41164133 try stack.append(RenderState.PrintIndent);
41174134 try stack.append(RenderState { .Indent = indent });
41184135 try stack.append(RenderState { .Text = "\n"});
41194136
4120 const decls = err_set_decl.decls.toSliceConst();
41214137 var i = decls.len;
41224138 while (i != 0) {
41234139 i -= 1;
......@@ -4142,7 +4158,6 @@ pub const Parser = struct {
41424158 });
41434159 }
41444160 try stack.append(RenderState { .Indent = indent + indent_delta});
4145 try stack.append(RenderState { .Text = "{"});
41464161 },
41474162 ast.Node.Id.MultilineStringLiteral => {
41484163 const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);
std/zig/parser_test.zig+32-14
......@@ -1,5 +1,35 @@
1test "zig fmt: union(enum(u32)) with assigned enum values" {
1test "zig fmt: error set declaration" {
22 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
31test "zig fmt: union(enum(u32)) with assigned enum values" {
32 try testCanonical(
333 \\const MultipleChoice = union(enum(u32)) {
434 \\ A = 20,
535 \\ B = 40,
......@@ -23,7 +53,7 @@ test "zig fmt: labeled suspend" {
2353
2454test "zig fmt: comments before error set decl" {
2555 try testCanonical(
26 \\const UnexpectedError = error {
56 \\const UnexpectedError = error{
2757 \\ /// The Operating System returned an undocumented error code.
2858 \\ Unexpected,
2959 \\ // another
......@@ -601,18 +631,6 @@ test "zig fmt: union declaration" {
601631 );
602632}
603633
604test "zig fmt: error set declaration" {
605 try testCanonical(
606 \\const E = error {
607 \\ A,
608 \\ B,
609 \\
610 \\ C,
611 \\};
612 \\
613 );
614}
615
616634test "zig fmt: arrays" {
617635 try testCanonical(
618636 \\test "test array" {