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{};
|
||||
\\
|
||||
\\const Error = error{ OutOfMemory, OutOfTime };
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1268,6 +1268,13 @@ fn renderExpression(
|
|||
}
|
||||
|
||||
try renderToken(tree, stream, err_set_decl.error_token, indent, start_col, Space.None); // error
|
||||
|
||||
const src_has_trailing_comma = blk: {
|
||||
const maybe_comma = tree.prevToken(err_set_decl.rbrace_token);
|
||||
break :blk tree.tokens.at(maybe_comma).id == .Comma;
|
||||
};
|
||||
|
||||
if (src_has_trailing_comma) {
|
||||
try renderToken(tree, stream, lbrace, indent, start_col, Space.Newline); // {
|
||||
const new_indent = indent + indent_delta;
|
||||
|
||||
|
@ -1287,6 +1294,25 @@ fn renderExpression(
|
|||
|
||||
try stream.writeByteNTimes(' ', indent);
|
||||
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 => {
|
||||
|
@ -1589,8 +1615,7 @@ fn renderExpression(
|
|||
}
|
||||
} else {
|
||||
var it = switch_case.items.iterator(0);
|
||||
while (true) {
|
||||
const node = it.next().?;
|
||||
while (it.next()) |node| {
|
||||
if (it.peek()) |next_node| {
|
||||
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.None);
|
||||
|
||||
|
@ -1601,7 +1626,6 @@ fn renderExpression(
|
|||
} else {
|
||||
try renderExpression(allocator, stream, tree, indent, start_col, node.*, Space.Comma);
|
||||
try stream.writeByteNTimes(' ', indent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue