aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-02-01 14:43:31 +0100
committerLemonBoy <thatlemon@gmail.com>2020-02-01 14:43:31 +0100
commitf34abbf2602c60a562988631de6a3dcbefbbb4cd (patch)
tree57c907fc2b2eff1b1f90e723e7ec6add90415e18
parent3640c682a2ed8a0f554224936a3c634215543ffe (diff)
downloadzig-f34abbf2602c60a562988631de6a3dcbefbbb4cd.tar.gz
zig-f34abbf2602c60a562988631de6a3dcbefbbb4cd.zip
fmt: Handle declarations in line with the opening brace
-rw-r--r--lib/std/zig/parser_test.zig4
-rw-r--r--lib/std/zig/render.zig19
2 files changed, 20 insertions, 3 deletions
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 9602b56c9f..50735b0ec8 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -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 };
\\
);
}
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index 1897ed1ae6..993b30b0ee 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -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); // {