mirror of
https://github.com/ziglang/zig.git
synced 2026-02-05 22:16:58 +00:00
fmt: Handle declarations in line with the opening brace
This commit is contained in:
parent
3640c682a2
commit
f34abbf260
@ -992,12 +992,10 @@ test "zig fmt: empty block with only comment" {
|
||||
}
|
||||
|
||||
test "zig fmt: no trailing comma on struct decl" {
|
||||
try testTransform(
|
||||
try testCanonical(
|
||||
\\const RoundParam = struct {
|
||||
\\ k: usize, s: u32, t: u32
|
||||
\\};
|
||||
,
|
||||
\\const RoundParam = struct { k: usize, s: u32, t: u32 };
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
@ -1179,6 +1179,12 @@ fn renderExpression(
|
||||
break :blk tree.tokens.at(maybe_comma).id == .Comma;
|
||||
};
|
||||
|
||||
// Check if the first declaration and the { are on the same line
|
||||
const src_has_newline = !tree.tokensOnSameLine(
|
||||
container_decl.fields_and_decls.at(0).*.firstToken(),
|
||||
container_decl.rbrace_token,
|
||||
);
|
||||
|
||||
// We can only print all the elements in-line if all the
|
||||
// declarations inside are fields
|
||||
const src_has_only_fields = blk: {
|
||||
@ -1205,6 +1211,19 @@ fn renderExpression(
|
||||
}
|
||||
|
||||
try stream.writeByteNTimes(' ', indent);
|
||||
} else if (src_has_newline) {
|
||||
// All the declarations on the same line, but place the items on
|
||||
// their own line
|
||||
try renderToken(tree, stream, container_decl.lbrace_token, indent, start_col, .Newline); // {
|
||||
|
||||
const new_indent = indent + indent_delta;
|
||||
try stream.writeByteNTimes(' ', new_indent);
|
||||
|
||||
var it = container_decl.fields_and_decls.iterator(0);
|
||||
while (it.next()) |decl| {
|
||||
const space_after_decl: Space = if (it.peek() == null) .Newline else .Space;
|
||||
try renderContainerDecl(allocator, stream, tree, new_indent, start_col, decl.*, space_after_decl);
|
||||
}
|
||||
} else {
|
||||
// All the declarations on the same line
|
||||
try renderToken(tree, stream, container_decl.lbrace_token, indent, start_col, .Space); // {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user