zig fmt: Respect trailing commas in error set declarations
The logic is not perfect as it doesn't take into account the presence of doc comments, but it's an improvement over the status quo.master
parent
63a4dbc30d
commit
11a4ce42c1
|
@ -1509,6 +1509,8 @@ test "zig fmt: error set declaration" {
|
||||||
\\const Error = error{OutOfMemory};
|
\\const Error = error{OutOfMemory};
|
||||||
\\const Error = error{};
|
\\const Error = error{};
|
||||||
\\
|
\\
|
||||||
|
\\const Error = error{ OutOfMemory, OutOfTime };
|
||||||
|
\\
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1268,25 +1268,51 @@ fn renderExpression(
|
||||||
}
|
}
|
||||||
|
|
||||||
try renderToken(tree, stream, err_set_decl.error_token, indent, start_col, Space.None); // error
|
try renderToken(tree, stream, err_set_decl.error_token, indent, start_col, Space.None); // error
|
||||||
try renderToken(tree, stream, lbrace, indent, start_col, Space.Newline); // {
|
|
||||||
const new_indent = indent + indent_delta;
|
|
||||||
|
|
||||||
var it = err_set_decl.decls.iterator(0);
|
const src_has_trailing_comma = blk: {
|
||||||
while (it.next()) |node| {
|
const maybe_comma = tree.prevToken(err_set_decl.rbrace_token);
|
||||||
try stream.writeByteNTimes(' ', new_indent);
|
break :blk tree.tokens.at(maybe_comma).id == .Comma;
|
||||||
|
};
|
||||||
|
|
||||||
if (it.peek()) |next_node| {
|
if (src_has_trailing_comma) {
|
||||||
try renderExpression(allocator, stream, tree, new_indent, start_col, node.*, Space.None);
|
try renderToken(tree, stream, lbrace, indent, start_col, Space.Newline); // {
|
||||||
try renderToken(tree, stream, tree.nextToken(node.*.lastToken()), new_indent, start_col, Space.Newline); // ,
|
const new_indent = indent + indent_delta;
|
||||||
|
|
||||||
try renderExtraNewline(tree, stream, start_col, next_node.*);
|
var it = err_set_decl.decls.iterator(0);
|
||||||
} else {
|
while (it.next()) |node| {
|
||||||
try renderExpression(allocator, stream, tree, new_indent, start_col, node.*, Space.Comma);
|
try stream.writeByteNTimes(' ', new_indent);
|
||||||
|
|
||||||
|
if (it.peek()) |next_node| {
|
||||||
|
try renderExpression(allocator, stream, tree, new_indent, start_col, node.*, Space.None);
|
||||||
|
try renderToken(tree, stream, tree.nextToken(node.*.lastToken()), new_indent, start_col, Space.Newline); // ,
|
||||||
|
|
||||||
|
try renderExtraNewline(tree, stream, start_col, next_node.*);
|
||||||
|
} else {
|
||||||
|
try renderExpression(allocator, stream, tree, new_indent, start_col, node.*, Space.Comma);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try stream.writeByteNTimes(' ', indent);
|
try stream.writeByteNTimes(' ', indent);
|
||||||
return renderToken(tree, stream, err_set_decl.rbrace_token, indent, start_col, space); // }
|
return renderToken(tree, stream, err_set_decl.rbrace_token, indent, start_col, space); // }
|
||||||
|
} else {
|
||||||
|
try renderToken(tree, stream, lbrace, indent, start_col, Space.Space); // {
|
||||||
|
|
||||||
|
var it = err_set_decl.decls.iterator(0);
|
||||||
|
while (it.next()) |node| {
|
||||||
|
if (it.peek()) |next_node| {
|
||||||
|
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.None);
|
||||||
|
|
||||||
|
const comma_token = tree.nextToken(node.*.lastToken());
|
||||||
|
assert(tree.tokens.at(comma_token).id == .Comma);
|
||||||
|
try renderToken(tree, stream, comma_token, indent, start_col, Space.Space); // ,
|
||||||
|
try renderExtraNewline(tree, stream, start_col, next_node.*);
|
||||||
|
} else {
|
||||||
|
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.Space);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return renderToken(tree, stream, err_set_decl.rbrace_token, indent, start_col, space); // }
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
.ErrorTag => {
|
.ErrorTag => {
|
||||||
|
@ -1589,8 +1615,7 @@ fn renderExpression(
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var it = switch_case.items.iterator(0);
|
var it = switch_case.items.iterator(0);
|
||||||
while (true) {
|
while (it.next()) |node| {
|
||||||
const node = it.next().?;
|
|
||||||
if (it.peek()) |next_node| {
|
if (it.peek()) |next_node| {
|
||||||
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.None);
|
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.None);
|
||||||
|
|
||||||
|
@ -1601,7 +1626,6 @@ fn renderExpression(
|
||||||
} else {
|
} else {
|
||||||
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.Comma);
|
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.Comma);
|
||||||
try stream.writeByteNTimes(' ', indent);
|
try stream.writeByteNTimes(' ', indent);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue