authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-01-28 23:10:14+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2022-01-29 12:19:31+01:00
loge51a44b3422a1fad0ec75078f3576f0fa447d986
tree189a51aecbc9f92bcd2b7c5c41dfd656d98ab367
parent225910f9341fbc725ff5e0d2c653e29bc2f21cb8

fmt: handle doc comments on struct members

Closes https://github.com/ziglang/zig/issues/10443.

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

lib/std/zig/parser_test.zig+19
......@@ -396,6 +396,25 @@ test "zig fmt: container declaration, multiline string, add trailing comma" {
396396 );
397397}
398398
399test "zig fmt: container declaration, doc comment on member, add trailing comma" {
400 try testTransform(
401 \\pub const Pos = struct {
402 \\ /// X-axis.
403 \\ x: u32,
404 \\ /// Y-axis.
405 \\ y: u32
406 \\};
407 ,
408 \\pub const Pos = struct {
409 \\ /// X-axis.
410 \\ x: u32,
411 \\ /// Y-axis.
412 \\ y: u32,
413 \\};
414 \\
415 );
416}
417
399418test "zig fmt: remove empty lines at start/end of container decl" {
400419 try testTransform(
401420 \\const X = struct {
lib/std/zig/render.zig+12-3
......@@ -1930,15 +1930,24 @@ fn renderContainerDecl(
19301930
19311931 const src_has_trailing_comma = token_tags[rbrace - 1] == .comma;
19321932 if (!src_has_trailing_comma) one_line: {
1933 // We can only print all the members in-line if there are no comments or multiline strings,
1934 // and all the members are fields.
1933 // We print all the members in-line unless one of the following conditions are true:
1934
1935 // 1. The container has comments or multiline strings.
19351936 if (hasComment(tree, lbrace, rbrace) or hasMultilineString(tree, lbrace, rbrace)) {
19361937 break :one_line;
19371938 }
1939
1940 // 2. A member of the container has a doc comment.
1941 for (token_tags[lbrace + 1 .. rbrace - 1]) |tag| {
1942 if (tag == .doc_comment) break :one_line;
1943 }
1944
1945 // 3. The container has non-field members.
19381946 for (container_decl.ast.members) |member| {
19391947 if (!node_tags[member].isContainerField()) break :one_line;
19401948 }
1941 // All the declarations on the same line.
1949
1950 // Print all the declarations on the same line.
19421951 try renderToken(ais, tree, lbrace, .space); // lbrace
19431952 for (container_decl.ast.members) |member| {
19441953 try renderMember(gpa, ais, tree, member, .space);