Skip to content

Commit 2bb24ee

Browse files
authored
fmt: implement comments inside sumtype declaration (#18189)
1 parent ddb8e09 commit 2bb24ee

File tree

7 files changed

+54
-16
lines changed

7 files changed

+54
-16
lines changed

cmd/tools/vast/vast.v

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,6 @@ fn (t Tree) sum_type_decl(node ast.SumTypeDecl) &Node {
804804
obj.add('pos', t.pos(node.pos))
805805
obj.add_terse('typ', t.type_node(node.typ))
806806
obj.add_terse('generic_types', t.array_node_type(node.generic_types))
807-
obj.add('comments', t.array_node_comment(node.comments))
808807
obj.add_terse('variants', t.array_node_type_expr(node.variants))
809808
obj.add('name_pos', t.pos(node.name_pos))
810809
return obj

vlib/v/ast/ast.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ pub struct TypeNode {
114114
pub:
115115
pos token.Pos
116116
pub mut:
117-
typ Type
117+
typ Type
118+
end_comments []Comment // comments that after current type node
118119
}
119120

120121
pub enum ComptimeTypeKind {
@@ -1335,7 +1336,6 @@ pub:
13351336
is_pub bool
13361337
pos token.Pos
13371338
name_pos token.Pos
1338-
comments []Comment
13391339
typ Type
13401340
generic_types []Type
13411341
attrs []Attr // attributes of type declaration

vlib/v/fmt/fmt.v

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,11 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
14691469
f.writeln('')
14701470
}
14711471

1472+
struct Variant {
1473+
name string
1474+
id int
1475+
}
1476+
14721477
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
14731478
f.attrs(node.attrs)
14741479
start_pos := f.out.len
@@ -1479,33 +1484,42 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
14791484
f.write_generic_types(node.generic_types)
14801485
f.write(' = ')
14811486

1482-
mut sum_type_names := []string{cap: node.variants.len}
1483-
for variant in node.variants {
1484-
sum_type_names << f.table.type_to_str_using_aliases(variant.typ, f.mod2alias)
1487+
mut variants := []Variant{cap: node.variants.len}
1488+
for i, variant in node.variants {
1489+
variants << Variant{f.table.type_to_str_using_aliases(variant.typ, f.mod2alias), i}
14851490
f.mark_types_import_as_used(variant.typ)
14861491
}
1487-
sum_type_names.sort()
1492+
variants.sort(a.name < b.name)
14881493

14891494
mut separator := ' | '
1495+
mut is_multiline := false
14901496
// if line length is too long, put each type on its own line
14911497
mut line_length := f.out.len - start_pos
1492-
for sum_type_name in sum_type_names {
1498+
for variant in variants {
14931499
// 3 = length of ' = ' or ' | '
1494-
line_length += 3 + sum_type_name.len
1495-
if line_length > fmt.max_len.last() {
1500+
line_length += 3 + variant.name.len
1501+
if line_length > fmt.max_len.last() || (variant.id != node.variants.len - 1
1502+
&& node.variants[variant.id].end_comments.len > 0) {
14961503
separator = '\n\t| '
1504+
is_multiline = true
14971505
break
14981506
}
14991507
}
15001508

1501-
for i, name in sum_type_names {
1509+
for i, variant in variants {
15021510
if i > 0 {
15031511
f.write(separator)
15041512
}
1505-
f.write(name)
1513+
f.write(variant.name)
1514+
if node.variants[variant.id].end_comments.len > 0 && is_multiline {
1515+
f.comments(node.variants[variant.id].end_comments, has_nl: false)
1516+
}
1517+
}
1518+
if !is_multiline {
1519+
f.comments(node.variants.last().end_comments,
1520+
has_nl: false
1521+
)
15061522
}
1507-
1508-
f.comments(node.comments, has_nl: false)
15091523
}
15101524

15111525
//=== Specific Expr methods ===//

vlib/v/fmt/tests/types_expected.vv

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ pub type PublicBar = Bar | Foo | FooBar
55
type Uint = u16 | u32 | u64 | u8 // This should stay on the same line
66
type Float = f32 | f64
77

8+
// Callback represents all the possible callbacks
9+
pub type Callback = ConnectedCallback // connected callback
10+
| DisconnectedCallback // disconnected callback
11+
| LoggedOffCallback // logged off callback
12+
| LoggedOnCallback // logged on callback
13+
14+
struct ConnectedCallback {}
15+
16+
struct DisconnectedCallback {}
17+
18+
struct LoggedOnCallback {}
19+
20+
struct LoggedOffCallback {}
21+
822
// Alias type
923
type MyInt = int
1024

vlib/v/fmt/tests/types_input.vv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ pub type PublicBar = Bar | Foo | FooBar
55
type Uint = u16 | u32 | u64 | u8 // This should stay on the same line
66
type Float = f32 | f64
77

8+
// Callback represents all the possible callbacks
9+
pub type Callback = ConnectedCallback // connected callback
10+
| LoggedOnCallback // logged on callback
11+
| DisconnectedCallback // disconnected callback
12+
| LoggedOffCallback // logged off callback
13+
14+
struct ConnectedCallback{}
15+
struct DisconnectedCallback{}
16+
struct LoggedOnCallback{}
17+
struct LoggedOffCallback{}
18+
819
// Alias type
920
type MyInt = int
1021

vlib/v/parser/parse_type.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,15 @@ pub fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode {
371371
for {
372372
type_start_pos := p.tok.pos()
373373
typ := p.parse_type()
374+
end_comments := p.eat_comments(same_line: true)
374375
// TODO: needs to be its own var, otherwise TCC fails because of a known stack error
375376
prev_tok := p.prev_tok
376377
type_end_pos := prev_tok.pos()
377378
type_pos := type_start_pos.extend(type_end_pos)
378379
types << ast.TypeNode{
379380
typ: typ
380381
pos: type_pos
382+
end_comments: end_comments
381383
}
382384
if p.tok.kind != .pipe {
383385
break

vlib/v/parser/parser.v

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,7 +4099,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
40994099
name_pos)
41004100
return ast.SumTypeDecl{}
41014101
}
4102-
comments = p.eat_comments(same_line: true)
41034102
return ast.SumTypeDecl{
41044103
name: name
41054104
typ: typ
@@ -4109,7 +4108,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
41094108
attrs: p.attrs
41104109
pos: decl_pos
41114110
name_pos: name_pos
4112-
comments: comments
41134111
}
41144112
}
41154113
// type MyType = int

0 commit comments

Comments
 (0)