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" {...@@ -992,12 +992,10 @@ test "zig fmt: empty block with only comment" {
992}992}
993993
994test "zig fmt: no trailing comma on struct decl" {994test "zig fmt: no trailing comma on struct decl" {
995 try testTransform(995 try testCanonical(
996 \\const RoundParam = struct {996 \\const RoundParam = struct {
997 \\ k: usize, s: u32, t: u32997 \\ k: usize, s: u32, t: u32
998 \\};998 \\};
999 ,
1000 \\const RoundParam = struct { k: usize, s: u32, t: u32 };
1001 \\999 \\
1002 );1000 );
1003}1001}
lib/std/zig/render.zig+19
...@@ -1179,6 +1179,12 @@ fn renderExpression(...@@ -1179,6 +1179,12 @@ fn renderExpression(
1179 break :blk tree.tokens.at(maybe_comma).id == .Comma;1179 break :blk tree.tokens.at(maybe_comma).id == .Comma;
1180 };1180 };
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
1182 // We can only print all the elements in-line if all the1188 // We can only print all the elements in-line if all the
1183 // declarations inside are fields1189 // declarations inside are fields
1184 const src_has_only_fields = blk: {1190 const src_has_only_fields = blk: {
...@@ -1205,6 +1211,19 @@ fn renderExpression(...@@ -1205,6 +1211,19 @@ fn renderExpression(
1205 }1211 }
12061212
1207 try stream.writeByteNTimes(' ', indent);1213 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 }
1208 } else {1227 } else {
1209 // All the declarations on the same line1228 // All the declarations on the same line
1210 try renderToken(tree, stream, container_decl.lbrace_token, indent, start_col, .Space); // {1229 try renderToken(tree, stream, container_decl.lbrace_token, indent, start_col, .Space); // {