authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 18:32:47+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 18:32:47+01:00
log5306b1a9ab5cb0730fcc21da095db39b0197e99b
treebd7ba4faa7a0c25726d0ce19da12c8b33777873c
parent1f62e87031cbb85387934313a7a9e7091d424b84
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: container doc comments


5 files changed, 187 insertions(+), 164 deletions(-)

lib/std/zig/ast.zig+47-35
......@@ -525,17 +525,17 @@ pub const Tree = struct {
525525 },
526526
527527 .container_decl,
528 .container_decl_comma,
528 .container_decl_trailing,
529529 .container_decl_two,
530 .container_decl_two_comma,
530 .container_decl_two_trailing,
531531 .container_decl_arg,
532 .container_decl_arg_comma,
532 .container_decl_arg_trailing,
533533 .tagged_union,
534 .tagged_union_comma,
534 .tagged_union_trailing,
535535 .tagged_union_two,
536 .tagged_union_two_comma,
536 .tagged_union_two_trailing,
537537 .tagged_union_enum_tag,
538 .tagged_union_enum_tag_comma,
538 .tagged_union_enum_tag_trailing,
539539 => {
540540 const main_token = main_tokens[n];
541541 switch (token_tags[main_token - 1]) {
......@@ -606,6 +606,7 @@ pub const Tree = struct {
606606 const datas = tree.nodes.items(.data);
607607 const main_tokens = tree.nodes.items(.main_token);
608608 const token_starts = tree.tokens.items(.start);
609 const token_tags = tree.tokens.items(.tag);
609610 var n = node;
610611 var end_offset: TokenIndex = 0;
611612 while (true) switch (tags[n]) {
......@@ -738,9 +739,9 @@ pub const Tree = struct {
738739 },
739740 .call_comma,
740741 .async_call_comma,
741 .tagged_union_enum_tag_comma,
742 .tagged_union_enum_tag_trailing,
742743 => {
743 end_offset += 2; // for the comma + rparen/rbrace
744 end_offset += 2; // for the comma/semicolon + rparen/rbrace
744745 const params = tree.extraData(datas[n].rhs, Node.SubRange);
745746 assert(params.end > params.start);
746747 n = tree.extra_data[params.end - 1]; // last parameter
......@@ -779,7 +780,7 @@ pub const Tree = struct {
779780 },
780781 .array_init_comma,
781782 .struct_init_comma,
782 .container_decl_arg_comma,
783 .container_decl_arg_trailing,
783784 .switch_comma,
784785 => {
785786 const members = tree.extraData(datas[n].rhs, Node.SubRange);
......@@ -801,8 +802,8 @@ pub const Tree = struct {
801802 .array_init_dot_comma,
802803 .struct_init_dot_comma,
803804 .block_semicolon,
804 .container_decl_comma,
805 .tagged_union_comma,
805 .container_decl_trailing,
806 .tagged_union_trailing,
806807 .builtin_call_comma,
807808 => {
808809 assert(datas[n].rhs - datas[n].lhs > 0);
......@@ -838,10 +839,17 @@ pub const Tree = struct {
838839 .block_two,
839840 .struct_init_dot_two,
840841 => end_offset += 1, // rbrace
841 .builtin_call_two,
842 .container_decl_two,
843 => end_offset += 2, // lparen/lbrace + rparen/rbrace
844 .tagged_union_two => end_offset += 5, // (enum) {}
842 .builtin_call_two => end_offset += 2, // lparen/lbrace + rparen/rbrace
843 .container_decl_two => {
844 var i: u32 = 2; // lbrace + rbrace
845 while (token_tags[main_tokens[n] + i] == .container_doc_comment) i += 1;
846 end_offset += i;
847 },
848 .tagged_union_two => {
849 var i: u32 = 5; // (enum) {}
850 while (token_tags[main_tokens[n] + i] == .container_doc_comment) i += 1;
851 end_offset += i;
852 },
845853 else => unreachable,
846854 }
847855 return main_tokens[n] + end_offset;
......@@ -851,8 +859,8 @@ pub const Tree = struct {
851859 .builtin_call_two_comma,
852860 .block_two_semicolon,
853861 .struct_init_dot_two_comma,
854 .container_decl_two_comma,
855 .tagged_union_two_comma,
862 .container_decl_two_trailing,
863 .tagged_union_two_trailing,
856864 => {
857865 end_offset += 2; // for the comma/semicolon + rbrace/rparen
858866 if (datas[n].rhs != 0) {
......@@ -1531,7 +1539,7 @@ pub const Tree = struct {
15311539
15321540 pub fn containerDeclTwo(tree: Tree, buffer: *[2]Node.Index, node: Node.Index) full.ContainerDecl {
15331541 assert(tree.nodes.items(.tag)[node] == .container_decl_two or
1534 tree.nodes.items(.tag)[node] == .container_decl_two_comma);
1542 tree.nodes.items(.tag)[node] == .container_decl_two_trailing);
15351543 const data = tree.nodes.items(.data)[node];
15361544 buffer.* = .{ data.lhs, data.rhs };
15371545 const members = if (data.rhs != 0)
......@@ -1550,7 +1558,7 @@ pub const Tree = struct {
15501558
15511559 pub fn containerDecl(tree: Tree, node: Node.Index) full.ContainerDecl {
15521560 assert(tree.nodes.items(.tag)[node] == .container_decl or
1553 tree.nodes.items(.tag)[node] == .container_decl_comma);
1561 tree.nodes.items(.tag)[node] == .container_decl_trailing);
15541562 const data = tree.nodes.items(.data)[node];
15551563 return tree.fullContainerDecl(.{
15561564 .main_token = tree.nodes.items(.main_token)[node],
......@@ -1562,7 +1570,7 @@ pub const Tree = struct {
15621570
15631571 pub fn containerDeclArg(tree: Tree, node: Node.Index) full.ContainerDecl {
15641572 assert(tree.nodes.items(.tag)[node] == .container_decl_arg or
1565 tree.nodes.items(.tag)[node] == .container_decl_arg_comma);
1573 tree.nodes.items(.tag)[node] == .container_decl_arg_trailing);
15661574 const data = tree.nodes.items(.data)[node];
15671575 const members_range = tree.extraData(data.rhs, Node.SubRange);
15681576 return tree.fullContainerDecl(.{
......@@ -1575,7 +1583,7 @@ pub const Tree = struct {
15751583
15761584 pub fn taggedUnionTwo(tree: Tree, buffer: *[2]Node.Index, node: Node.Index) full.ContainerDecl {
15771585 assert(tree.nodes.items(.tag)[node] == .tagged_union_two or
1578 tree.nodes.items(.tag)[node] == .tagged_union_two_comma);
1586 tree.nodes.items(.tag)[node] == .tagged_union_two_trailing);
15791587 const data = tree.nodes.items(.data)[node];
15801588 buffer.* = .{ data.lhs, data.rhs };
15811589 const members = if (data.rhs != 0)
......@@ -1595,7 +1603,7 @@ pub const Tree = struct {
15951603
15961604 pub fn taggedUnion(tree: Tree, node: Node.Index) full.ContainerDecl {
15971605 assert(tree.nodes.items(.tag)[node] == .tagged_union or
1598 tree.nodes.items(.tag)[node] == .tagged_union_comma);
1606 tree.nodes.items(.tag)[node] == .tagged_union_trailing);
15991607 const data = tree.nodes.items(.data)[node];
16001608 const main_token = tree.nodes.items(.main_token)[node];
16011609 return tree.fullContainerDecl(.{
......@@ -1608,7 +1616,7 @@ pub const Tree = struct {
16081616
16091617 pub fn taggedUnionEnumTag(tree: Tree, node: Node.Index) full.ContainerDecl {
16101618 assert(tree.nodes.items(.tag)[node] == .tagged_union_enum_tag or
1611 tree.nodes.items(.tag)[node] == .tagged_union_enum_tag_comma);
1619 tree.nodes.items(.tag)[node] == .tagged_union_enum_tag_trailing);
16121620 const data = tree.nodes.items(.data)[node];
16131621 const members_range = tree.extraData(data.rhs, Node.SubRange);
16141622 const main_token = tree.nodes.items(.main_token)[node];
......@@ -2762,36 +2770,40 @@ pub const Node = struct {
27622770 /// `struct {}`, `union {}`, `opaque {}`, `enum {}`. `extra_data[lhs..rhs]`.
27632771 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.
27642772 container_decl,
2765 /// Same as ContainerDecl but there is known to be a trailing comma before the rbrace.
2766 container_decl_comma,
2773 /// Same as ContainerDecl but there is known to be a trailing comma
2774 /// or semicolon before the rbrace.
2775 container_decl_trailing,
27672776 /// `struct {lhs, rhs}`, `union {lhs, rhs}`, `opaque {lhs, rhs}`, `enum {lhs, rhs}`.
27682777 /// lhs or rhs can be omitted.
27692778 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.
27702779 container_decl_two,
27712780 /// Same as ContainerDeclTwo except there is known to be a trailing comma
2772 /// before the rbrace.
2773 container_decl_two_comma,
2781 /// or semicolon before the rbrace.
2782 container_decl_two_trailing,
27742783 /// `union(lhs)` / `enum(lhs)`. `SubRange[rhs]`.
27752784 container_decl_arg,
2776 /// Same as container_decl_arg but there is known to be a trailing comma before the rbrace.
2777 container_decl_arg_comma,
2785 /// Same as container_decl_arg but there is known to be a trailing
2786 /// comma or semicolon before the rbrace.
2787 container_decl_arg_trailing,
27782788 /// `union(enum) {}`. `sub_list[lhs..rhs]`.
27792789 /// Note that tagged unions with explicitly provided enums are represented
27802790 /// by `container_decl_arg`.
27812791 tagged_union,
2782 /// Same as tagged_union but there is known to be a trailing comma before the rbrace.
2783 tagged_union_comma,
2792 /// Same as tagged_union but there is known to be a trailing comma
2793 /// or semicolon before the rbrace.
2794 tagged_union_trailing,
27842795 /// `union(enum) {lhs, rhs}`. lhs or rhs may be omitted.
27852796 /// Note that tagged unions with explicitly provided enums are represented
27862797 /// by `container_decl_arg`.
27872798 tagged_union_two,
2788 /// Same as tagged_union_two but there is known to be a trailing comma before the rbrace.
2789 tagged_union_two_comma,
2799 /// Same as tagged_union_two but there is known to be a trailing comma
2800 /// or semicolon before the rbrace.
2801 tagged_union_two_trailing,
27902802 /// `union(enum(lhs)) {}`. `SubRange[rhs]`.
27912803 tagged_union_enum_tag,
27922804 /// Same as tagged_union_enum_tag but there is known to be a trailing comma
2793 /// before the rbrace.
2794 tagged_union_enum_tag_comma,
2805 /// or semicolon before the rbrace.
2806 tagged_union_enum_tag_trailing,
27952807 /// `a: lhs = rhs,`. lhs and rhs can be omitted.
27962808 /// main_token is the field name identifier.
27972809 /// lastToken() does not include the possible trailing comma.
lib/std/zig/parse.zig+27-27
......@@ -113,7 +113,7 @@ const Parser = struct {
113113 len: usize,
114114 lhs: Node.Index,
115115 rhs: Node.Index,
116 trailing_comma: bool,
116 trailing: bool,
117117
118118 fn toSpan(self: Members, p: *Parser) !Node.SubRange {
119119 if (self.len <= 2) {
......@@ -215,7 +215,7 @@ const Parser = struct {
215215 // Skip container doc comments.
216216 while (p.eatToken(.container_doc_comment)) |_| {}
217217
218 var trailing_comma = false;
218 var trailing = false;
219219 while (true) {
220220 const doc_comment = try p.eatDocComments();
221221
......@@ -228,7 +228,7 @@ const Parser = struct {
228228 }
229229 try list.append(test_decl_node);
230230 }
231 trailing_comma = false;
231 trailing = false;
232232 },
233233 .keyword_comptime => switch (p.token_tags[p.tok_i + 1]) {
234234 .identifier => {
......@@ -251,11 +251,11 @@ const Parser = struct {
251251 switch (p.token_tags[p.tok_i]) {
252252 .comma => {
253253 p.tok_i += 1;
254 trailing_comma = true;
254 trailing = true;
255255 continue;
256256 },
257257 .r_brace, .eof => {
258 trailing_comma = false;
258 trailing = false;
259259 break;
260260 },
261261 else => {},
......@@ -289,7 +289,7 @@ const Parser = struct {
289289 }
290290 try list.append(comptime_node);
291291 }
292 trailing_comma = false;
292 trailing = false;
293293 },
294294 else => {
295295 p.tok_i += 1;
......@@ -305,7 +305,7 @@ const Parser = struct {
305305 }
306306 try list.append(top_level_decl);
307307 }
308 trailing_comma = false;
308 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
309309 },
310310 .keyword_usingnamespace => {
311311 const node = try p.expectUsingNamespaceRecoverable();
......@@ -315,7 +315,7 @@ const Parser = struct {
315315 }
316316 try list.append(node);
317317 }
318 trailing_comma = false;
318 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
319319 },
320320 .keyword_const,
321321 .keyword_var,
......@@ -333,7 +333,7 @@ const Parser = struct {
333333 }
334334 try list.append(top_level_decl);
335335 }
336 trailing_comma = false;
336 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
337337 },
338338 .identifier => {
339339 const container_field = try p.expectContainerFieldRecoverable();
......@@ -354,11 +354,11 @@ const Parser = struct {
354354 switch (p.token_tags[p.tok_i]) {
355355 .comma => {
356356 p.tok_i += 1;
357 trailing_comma = true;
357 trailing = true;
358358 continue;
359359 },
360360 .r_brace, .eof => {
361 trailing_comma = false;
361 trailing = false;
362362 break;
363363 },
364364 else => {},
......@@ -391,19 +391,19 @@ const Parser = struct {
391391 .len = 0,
392392 .lhs = 0,
393393 .rhs = 0,
394 .trailing_comma = trailing_comma,
394 .trailing = trailing,
395395 },
396396 1 => return Members{
397397 .len = 1,
398398 .lhs = list.items[0],
399399 .rhs = 0,
400 .trailing_comma = trailing_comma,
400 .trailing = trailing,
401401 },
402402 2 => return Members{
403403 .len = 2,
404404 .lhs = list.items[0],
405405 .rhs = list.items[1],
406 .trailing_comma = trailing_comma,
406 .trailing = trailing,
407407 },
408408 else => {
409409 const span = try p.listToSpan(list.items);
......@@ -411,7 +411,7 @@ const Parser = struct {
411411 .len = list.items.len,
412412 .lhs = span.start,
413413 .rhs = span.end,
414 .trailing_comma = trailing_comma,
414 .trailing = trailing,
415415 };
416416 },
417417 }
......@@ -3575,8 +3575,8 @@ const Parser = struct {
35753575 const members_span = try members.toSpan(p);
35763576 _ = try p.expectToken(.r_brace);
35773577 return p.addNode(.{
3578 .tag = switch (members.trailing_comma) {
3579 true => .tagged_union_enum_tag_comma,
3578 .tag = switch (members.trailing) {
3579 true => .tagged_union_enum_tag_trailing,
35803580 false => .tagged_union_enum_tag,
35813581 },
35823582 .main_token = main_token,
......@@ -3593,8 +3593,8 @@ const Parser = struct {
35933593 _ = try p.expectToken(.r_brace);
35943594 if (members.len <= 2) {
35953595 return p.addNode(.{
3596 .tag = switch (members.trailing_comma) {
3597 true => .tagged_union_two_comma,
3596 .tag = switch (members.trailing) {
3597 true => .tagged_union_two_trailing,
35983598 false => .tagged_union_two,
35993599 },
36003600 .main_token = main_token,
......@@ -3606,8 +3606,8 @@ const Parser = struct {
36063606 } else {
36073607 const span = try members.toSpan(p);
36083608 return p.addNode(.{
3609 .tag = switch (members.trailing_comma) {
3610 true => .tagged_union_comma,
3609 .tag = switch (members.trailing) {
3610 true => .tagged_union_trailing,
36113611 false => .tagged_union,
36123612 },
36133613 .main_token = main_token,
......@@ -3638,8 +3638,8 @@ const Parser = struct {
36383638 if (arg_expr == 0) {
36393639 if (members.len <= 2) {
36403640 return p.addNode(.{
3641 .tag = switch (members.trailing_comma) {
3642 true => .container_decl_two_comma,
3641 .tag = switch (members.trailing) {
3642 true => .container_decl_two_trailing,
36433643 false => .container_decl_two,
36443644 },
36453645 .main_token = main_token,
......@@ -3651,8 +3651,8 @@ const Parser = struct {
36513651 } else {
36523652 const span = try members.toSpan(p);
36533653 return p.addNode(.{
3654 .tag = switch (members.trailing_comma) {
3655 true => .container_decl_comma,
3654 .tag = switch (members.trailing) {
3655 true => .container_decl_trailing,
36563656 false => .container_decl,
36573657 },
36583658 .main_token = main_token,
......@@ -3665,8 +3665,8 @@ const Parser = struct {
36653665 } else {
36663666 const span = try members.toSpan(p);
36673667 return p.addNode(.{
3668 .tag = switch (members.trailing_comma) {
3669 true => .container_decl_arg_comma,
3668 .tag = switch (members.trailing) {
3669 true => .container_decl_arg_trailing,
36703670 false => .container_decl_arg,
36713671 },
36723672 .main_token = main_token,
lib/std/zig/parser_test.zig+55-55
......@@ -3577,61 +3577,61 @@ test "zig fmt: file ends with struct field" {
35773577// );
35783578//}
35793579
3580//test "zig fmt: top level doc comments" {
3581// try testCanonical(
3582// \\//! tld 1
3583// \\//! tld 2
3584// \\//! tld 3
3585// \\
3586// \\// comment
3587// \\
3588// \\/// A doc
3589// \\const A = struct {
3590// \\ //! A tld 1
3591// \\ //! A tld 2
3592// \\ //! A tld 3
3593// \\};
3594// \\
3595// \\/// B doc
3596// \\const B = struct {
3597// \\ //! B tld 1
3598// \\ //! B tld 2
3599// \\ //! B tld 3
3600// \\
3601// \\ /// b doc
3602// \\ b: u32,
3603// \\};
3604// \\
3605// \\/// C doc
3606// \\const C = struct {
3607// \\ //! C tld 1
3608// \\ //! C tld 2
3609// \\ //! C tld 3
3610// \\
3611// \\ /// c1 doc
3612// \\ c1: u32,
3613// \\
3614// \\ //! C tld 4
3615// \\ //! C tld 5
3616// \\ //! C tld 6
3617// \\
3618// \\ /// c2 doc
3619// \\ c2: u32,
3620// \\};
3621// \\
3622// );
3623// try testCanonical(
3624// \\//! Top-level documentation.
3625// \\
3626// \\/// This is A
3627// \\pub const A = usize;
3628// \\
3629// );
3630// try testCanonical(
3631// \\//! Nothing here
3632// \\
3633// );
3634//}
3580test "zig fmt: container doc comments" {
3581 try testCanonical(
3582 \\//! tld 1
3583 \\//! tld 2
3584 \\//! tld 3
3585 \\
3586 \\// comment
3587 \\
3588 \\/// A doc
3589 \\const A = struct {
3590 \\ //! A tld 1
3591 \\ //! A tld 2
3592 \\ //! A tld 3
3593 \\};
3594 \\
3595 \\/// B doc
3596 \\const B = struct {
3597 \\ //! B tld 1
3598 \\ //! B tld 2
3599 \\ //! B tld 3
3600 \\
3601 \\ /// B doc
3602 \\ b: u32,
3603 \\};
3604 \\
3605 \\/// C doc
3606 \\const C = union(enum) { // comment
3607 \\ //! C tld 1
3608 \\ //! C tld 2
3609 \\ //! C tld 3
3610 \\};
3611 \\
3612 \\/// D doc
3613 \\const D = union(Foo) {
3614 \\ //! D tld 1
3615 \\ //! D tld 2
3616 \\ //! D tld 3
3617 \\
3618 \\ /// D doc
3619 \\ b: u32,
3620 \\};
3621 \\
3622 );
3623 try testCanonical(
3624 \\//! Top-level documentation.
3625 \\
3626 \\/// This is A
3627 \\pub const A = usize;
3628 \\
3629 );
3630 try testCanonical(
3631 \\//! Nothing here
3632 \\
3633 );
3634}
36353635
36363636test "zig fmt: extern without container keyword returns error" {
36373637 try testError(
lib/std/zig/render.zig+40-29
......@@ -30,6 +30,10 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {
3030 const comment_end_loc = tree.tokens.items(.start)[0];
3131 _ = try renderComments(ais, tree, 0, comment_end_loc);
3232
33 if (tree.tokens.items(.tag)[0] == .container_doc_comment) {
34 try renderContainerDocComments(ais, tree, 0);
35 }
36
3337 try renderMembers(buffer.allocator, ais, tree, tree.rootDecls());
3438
3539 if (ais.disabled_offset) |disabled_offset| {
......@@ -506,28 +510,28 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
506510 },
507511
508512 .container_decl,
509 .container_decl_comma,
510 => return renderContainerDecl(gpa, ais, tree, tree.containerDecl(node), space),
513 .container_decl_trailing,
514 => return renderContainerDecl(gpa, ais, tree, node, tree.containerDecl(node), space),
511515
512 .container_decl_two, .container_decl_two_comma => {
516 .container_decl_two, .container_decl_two_trailing => {
513517 var buffer: [2]ast.Node.Index = undefined;
514 return renderContainerDecl(gpa, ais, tree, tree.containerDeclTwo(&buffer, node), space);
518 return renderContainerDecl(gpa, ais, tree, node, tree.containerDeclTwo(&buffer, node), space);
515519 },
516520 .container_decl_arg,
517 .container_decl_arg_comma,
518 => return renderContainerDecl(gpa, ais, tree, tree.containerDeclArg(node), space),
521 .container_decl_arg_trailing,
522 => return renderContainerDecl(gpa, ais, tree, node, tree.containerDeclArg(node), space),
519523
520524 .tagged_union,
521 .tagged_union_comma,
522 => return renderContainerDecl(gpa, ais, tree, tree.taggedUnion(node), space),
525 .tagged_union_trailing,
526 => return renderContainerDecl(gpa, ais, tree, node, tree.taggedUnion(node), space),
523527
524 .tagged_union_two, .tagged_union_two_comma => {
528 .tagged_union_two, .tagged_union_two_trailing => {
525529 var buffer: [2]ast.Node.Index = undefined;
526 return renderContainerDecl(gpa, ais, tree, tree.taggedUnionTwo(&buffer, node), space);
530 return renderContainerDecl(gpa, ais, tree, node, tree.taggedUnionTwo(&buffer, node), space);
527531 },
528532 .tagged_union_enum_tag,
529 .tagged_union_enum_tag_comma,
530 => return renderContainerDecl(gpa, ais, tree, tree.taggedUnionEnumTag(node), space),
533 .tagged_union_enum_tag_trailing,
534 => return renderContainerDecl(gpa, ais, tree, node, tree.taggedUnionEnumTag(node), space),
531535
532536 .error_set_decl => {
533537 const error_token = main_tokens[node];
......@@ -1662,7 +1666,6 @@ fn renderArrayInit(
16621666 ais.pushIndentNextLine();
16631667 try renderToken(ais, tree, array_init.ast.lbrace, .newline);
16641668
1665
16661669 var expr_index: usize = 0;
16671670 while (rowSize(tree, array_init.ast.elements[expr_index..], rbrace)) |row_size| {
16681671 const row_exprs = array_init.ast.elements[expr_index..];
......@@ -1806,6 +1809,7 @@ fn renderContainerDecl(
18061809 gpa: *Allocator,
18071810 ais: *Ais,
18081811 tree: ast.Tree,
1812 container_decl_node: ast.Node.Index,
18091813 container_decl: ast.full.ContainerDecl,
18101814 space: Space,
18111815) Error!void {
......@@ -1844,25 +1848,20 @@ fn renderContainerDecl(
18441848 lbrace = container_decl.ast.main_token + 1;
18451849 }
18461850
1851 const rbrace = tree.lastToken(container_decl_node);
18471852 if (container_decl.ast.members.len == 0) {
1848 try renderToken(ais, tree, lbrace, Space.none); // lbrace
1849 return renderToken(ais, tree, lbrace + 1, space); // rbrace
1853 ais.pushIndentNextLine();
1854 if (token_tags[lbrace + 1] == .container_doc_comment) {
1855 try renderToken(ais, tree, lbrace, .newline); // lbrace
1856 try renderContainerDocComments(ais, tree, lbrace + 1);
1857 } else {
1858 try renderToken(ais, tree, lbrace, .none); // lbrace
1859 }
1860 ais.popIndent();
1861 return renderToken(ais, tree, rbrace, space); // rbrace
18501862 }
18511863
1852 const last_member = container_decl.ast.members[container_decl.ast.members.len - 1];
1853 const last_member_token = tree.lastToken(last_member);
1854 const rbrace = switch (token_tags[last_member_token + 1]) {
1855 .doc_comment => last_member_token + 2,
1856 .comma, .semicolon => switch (token_tags[last_member_token + 2]) {
1857 .doc_comment => last_member_token + 3,
1858 .r_brace => last_member_token + 2,
1859 else => unreachable,
1860 },
1861 .r_brace => last_member_token + 1,
1862 else => unreachable,
1863 };
1864 const src_has_trailing_comma = token_tags[last_member_token + 1] == .comma;
1865
1864 const src_has_trailing_comma = token_tags[rbrace - 1] == .comma;
18661865 if (!src_has_trailing_comma) one_line: {
18671866 // We can only print all the members in-line if all the members are fields.
18681867 for (container_decl.ast.members) |member| {
......@@ -1879,6 +1878,9 @@ fn renderContainerDecl(
18791878 // One member per line.
18801879 ais.pushIndentNextLine();
18811880 try renderToken(ais, tree, lbrace, .newline); // lbrace
1881 if (token_tags[lbrace + 1] == .container_doc_comment) {
1882 try renderContainerDocComments(ais, tree, lbrace + 1);
1883 }
18821884 try renderMembers(gpa, ais, tree, container_decl.ast.members);
18831885 ais.popIndent();
18841886
......@@ -2272,6 +2274,15 @@ fn renderDocComments(ais: *Ais, tree: ast.Tree, end_token: ast.TokenIndex) Error
22722274 }
22732275}
22742276
2277/// start_token is first container doc comment token.
2278fn renderContainerDocComments(ais: *Ais, tree: ast.Tree, start_token: ast.TokenIndex) Error!void {
2279 const token_tags = tree.tokens.items(.tag);
2280 var tok = start_token;
2281 while (token_tags[tok] == .container_doc_comment) : (tok += 1) {
2282 try renderToken(ais, tree, tok, .newline);
2283 }
2284}
2285
22752286fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {
22762287 var ret = tree.tokenSlice(token_index);
22772288 if (tree.tokens.items(.tag)[token_index] == .multiline_string_literal_line) {
src/astgen.zig+18-18
......@@ -215,17 +215,17 @@ fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.I
215215 .anyframe_literal,
216216 .error_set_decl,
217217 .container_decl,
218 .container_decl_comma,
218 .container_decl_trailing,
219219 .container_decl_two,
220 .container_decl_two_comma,
220 .container_decl_two_trailing,
221221 .container_decl_arg,
222 .container_decl_arg_comma,
222 .container_decl_arg_trailing,
223223 .tagged_union,
224 .tagged_union_comma,
224 .tagged_union_trailing,
225225 .tagged_union_two,
226 .tagged_union_two_comma,
226 .tagged_union_two_trailing,
227227 .tagged_union_enum_tag,
228 .tagged_union_enum_tag_comma,
228 .tagged_union_enum_tag_trailing,
229229 .@"comptime",
230230 .@"nosuspend",
231231 .error_value,
......@@ -577,25 +577,25 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
577577 .ptr_type_bit_range => return ptrType(mod, scope, rl, tree.ptrTypeBitRange(node)),
578578
579579 .container_decl,
580 .container_decl_comma,
580 .container_decl_trailing,
581581 => return containerDecl(mod, scope, rl, tree.containerDecl(node)),
582 .container_decl_two, .container_decl_two_comma => {
582 .container_decl_two, .container_decl_two_trailing => {
583583 var buffer: [2]ast.Node.Index = undefined;
584584 return containerDecl(mod, scope, rl, tree.containerDeclTwo(&buffer, node));
585585 },
586586 .container_decl_arg,
587 .container_decl_arg_comma,
587 .container_decl_arg_trailing,
588588 => return containerDecl(mod, scope, rl, tree.containerDeclArg(node)),
589589
590590 .tagged_union,
591 .tagged_union_comma,
591 .tagged_union_trailing,
592592 => return containerDecl(mod, scope, rl, tree.taggedUnion(node)),
593 .tagged_union_two, .tagged_union_two_comma => {
593 .tagged_union_two, .tagged_union_two_trailing => {
594594 var buffer: [2]ast.Node.Index = undefined;
595595 return containerDecl(mod, scope, rl, tree.taggedUnionTwo(&buffer, node));
596596 },
597597 .tagged_union_enum_tag,
598 .tagged_union_enum_tag_comma,
598 .tagged_union_enum_tag_trailing,
599599 => return containerDecl(mod, scope, rl, tree.taggedUnionEnumTag(node)),
600600
601601 .@"break" => return breakExpr(mod, scope, rl, node),
......@@ -3715,17 +3715,17 @@ fn nodeMayNeedMemoryLocation(scope: *Scope, start_node: ast.Node.Index) bool {
37153715 .identifier,
37163716 .error_set_decl,
37173717 .container_decl,
3718 .container_decl_comma,
3718 .container_decl_trailing,
37193719 .container_decl_two,
3720 .container_decl_two_comma,
3720 .container_decl_two_trailing,
37213721 .container_decl_arg,
3722 .container_decl_arg_comma,
3722 .container_decl_arg_trailing,
37233723 .tagged_union,
3724 .tagged_union_comma,
3724 .tagged_union_trailing,
37253725 .tagged_union_two,
3726 .tagged_union_two_comma,
3726 .tagged_union_two_trailing,
37273727 .tagged_union_enum_tag,
3728 .tagged_union_enum_tag_comma,
3728 .tagged_union_enum_tag_trailing,
37293729 .@"asm",
37303730 .asm_simple,
37313731 .add,