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" {...@@ -396,6 +396,25 @@ test "zig fmt: container declaration, multiline string, add trailing comma" {
396 );396 );
397}397}
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
399test "zig fmt: remove empty lines at start/end of container decl" {418test "zig fmt: remove empty lines at start/end of container decl" {
400 try testTransform(419 try testTransform(
401 \\const X = struct {420 \\const X = struct {
lib/std/zig/render.zig+12-3
...@@ -1930,15 +1930,24 @@ fn renderContainerDecl(...@@ -1930,15 +1930,24 @@ fn renderContainerDecl(
19301930
1931 const src_has_trailing_comma = token_tags[rbrace - 1] == .comma;1931 const src_has_trailing_comma = token_tags[rbrace - 1] == .comma;
1932 if (!src_has_trailing_comma) one_line: {1932 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,1933 // We print all the members in-line unless one of the following conditions are true:
1934 // and all the members are fields.1934
1935 // 1. The container has comments or multiline strings.
1935 if (hasComment(tree, lbrace, rbrace) or hasMultilineString(tree, lbrace, rbrace)) {1936 if (hasComment(tree, lbrace, rbrace) or hasMultilineString(tree, lbrace, rbrace)) {
1936 break :one_line;1937 break :one_line;
1937 }1938 }
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.
1938 for (container_decl.ast.members) |member| {1946 for (container_decl.ast.members) |member| {
1939 if (!node_tags[member].isContainerField()) break :one_line;1947 if (!node_tags[member].isContainerField()) break :one_line;
1940 }1948 }
1941 // All the declarations on the same line.1949
1950 // Print all the declarations on the same line.
1942 try renderToken(ais, tree, lbrace, .space); // lbrace1951 try renderToken(ais, tree, lbrace, .space); // lbrace
1943 for (container_decl.ast.members) |member| {1952 for (container_decl.ast.members) |member| {
1944 try renderMember(gpa, ais, tree, member, .space);1953 try renderMember(gpa, ais, tree, member, .space);