authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-01 14:43:31+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-01 14:43:31+01:00
logf34abbf2602c60a562988631de6a3dcbefbbb4cd
tree57c907fc2b2eff1b1f90e723e7ec6add90415e18
parent3640c682a2ed8a0f554224936a3c634215543ffe

fmt: Handle declarations in line with the opening brace


2 files changed, 20 insertions(+), 3 deletions(-)

lib/std/zig/parser_test.zig+1-3
......@@ -992,12 +992,10 @@ test "zig fmt: empty block with only comment" {
992992}
993993
994994test "zig fmt: no trailing comma on struct decl" {
995 try testTransform(
995 try testCanonical(
996996 \\const RoundParam = struct {
997997 \\ k: usize, s: u32, t: u32
998998 \\};
999 ,
1000 \\const RoundParam = struct { k: usize, s: u32, t: u32 };
1001999 \\
10021000 );
10031001}
lib/std/zig/render.zig+19
......@@ -1179,6 +1179,12 @@ fn renderExpression(
11791179 break :blk tree.tokens.at(maybe_comma).id == .Comma;
11801180 };
11811181
1182 // Check if the first declaration and the { are on the same line
1183 const src_has_newline = !tree.tokensOnSameLine(
1184 container_decl.fields_and_decls.at(0).*.firstToken(),
1185 container_decl.rbrace_token,
1186 );
1187
11821188 // We can only print all the elements in-line if all the
11831189 // declarations inside are fields
11841190 const src_has_only_fields = blk: {
......@@ -1205,6 +1211,19 @@ fn renderExpression(
12051211 }
12061212
12071213 try stream.writeByteNTimes(' ', indent);
1214 } else if (src_has_newline) {
1215 // All the declarations on the same line, but place the items on
1216 // their own line
1217 try renderToken(tree, stream, container_decl.lbrace_token, indent, start_col, .Newline); // {
1218
1219 const new_indent = indent + indent_delta;
1220 try stream.writeByteNTimes(' ', new_indent);
1221
1222 var it = container_decl.fields_and_decls.iterator(0);
1223 while (it.next()) |decl| {
1224 const space_after_decl: Space = if (it.peek() == null) .Newline else .Space;
1225 try renderContainerDecl(allocator, stream, tree, new_indent, start_col, decl.*, space_after_decl);
1226 }
12081227 } else {
12091228 // All the declarations on the same line
12101229 try renderToken(tree, stream, container_decl.lbrace_token, indent, start_col, .Space); // {