mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fmt: implement comments inside sumtype declaration (#18189)
This commit is contained in:
parent
ddb8e09fec
commit
2bb24ee739
@ -804,7 +804,6 @@ fn (t Tree) sum_type_decl(node ast.SumTypeDecl) &Node {
|
|||||||
obj.add('pos', t.pos(node.pos))
|
obj.add('pos', t.pos(node.pos))
|
||||||
obj.add_terse('typ', t.type_node(node.typ))
|
obj.add_terse('typ', t.type_node(node.typ))
|
||||||
obj.add_terse('generic_types', t.array_node_type(node.generic_types))
|
obj.add_terse('generic_types', t.array_node_type(node.generic_types))
|
||||||
obj.add('comments', t.array_node_comment(node.comments))
|
|
||||||
obj.add_terse('variants', t.array_node_type_expr(node.variants))
|
obj.add_terse('variants', t.array_node_type_expr(node.variants))
|
||||||
obj.add('name_pos', t.pos(node.name_pos))
|
obj.add('name_pos', t.pos(node.name_pos))
|
||||||
return obj
|
return obj
|
||||||
|
@ -115,6 +115,7 @@ pub:
|
|||||||
pos token.Pos
|
pos token.Pos
|
||||||
pub mut:
|
pub mut:
|
||||||
typ Type
|
typ Type
|
||||||
|
end_comments []Comment // comments that after current type node
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ComptimeTypeKind {
|
pub enum ComptimeTypeKind {
|
||||||
@ -1335,7 +1336,6 @@ pub:
|
|||||||
is_pub bool
|
is_pub bool
|
||||||
pos token.Pos
|
pos token.Pos
|
||||||
name_pos token.Pos
|
name_pos token.Pos
|
||||||
comments []Comment
|
|
||||||
typ Type
|
typ Type
|
||||||
generic_types []Type
|
generic_types []Type
|
||||||
attrs []Attr // attributes of type declaration
|
attrs []Attr // attributes of type declaration
|
||||||
|
@ -1469,6 +1469,11 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
|
|||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Variant {
|
||||||
|
name string
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
|
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
|
||||||
f.attrs(node.attrs)
|
f.attrs(node.attrs)
|
||||||
start_pos := f.out.len
|
start_pos := f.out.len
|
||||||
@ -1479,33 +1484,42 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
|
|||||||
f.write_generic_types(node.generic_types)
|
f.write_generic_types(node.generic_types)
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
|
|
||||||
mut sum_type_names := []string{cap: node.variants.len}
|
mut variants := []Variant{cap: node.variants.len}
|
||||||
for variant in node.variants {
|
for i, variant in node.variants {
|
||||||
sum_type_names << f.table.type_to_str_using_aliases(variant.typ, f.mod2alias)
|
variants << Variant{f.table.type_to_str_using_aliases(variant.typ, f.mod2alias), i}
|
||||||
f.mark_types_import_as_used(variant.typ)
|
f.mark_types_import_as_used(variant.typ)
|
||||||
}
|
}
|
||||||
sum_type_names.sort()
|
variants.sort(a.name < b.name)
|
||||||
|
|
||||||
mut separator := ' | '
|
mut separator := ' | '
|
||||||
|
mut is_multiline := false
|
||||||
// if line length is too long, put each type on its own line
|
// if line length is too long, put each type on its own line
|
||||||
mut line_length := f.out.len - start_pos
|
mut line_length := f.out.len - start_pos
|
||||||
for sum_type_name in sum_type_names {
|
for variant in variants {
|
||||||
// 3 = length of ' = ' or ' | '
|
// 3 = length of ' = ' or ' | '
|
||||||
line_length += 3 + sum_type_name.len
|
line_length += 3 + variant.name.len
|
||||||
if line_length > fmt.max_len.last() {
|
if line_length > fmt.max_len.last() || (variant.id != node.variants.len - 1
|
||||||
|
&& node.variants[variant.id].end_comments.len > 0) {
|
||||||
separator = '\n\t| '
|
separator = '\n\t| '
|
||||||
|
is_multiline = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, name in sum_type_names {
|
for i, variant in variants {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
f.write(separator)
|
f.write(separator)
|
||||||
}
|
}
|
||||||
f.write(name)
|
f.write(variant.name)
|
||||||
|
if node.variants[variant.id].end_comments.len > 0 && is_multiline {
|
||||||
|
f.comments(node.variants[variant.id].end_comments, has_nl: false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !is_multiline {
|
||||||
|
f.comments(node.variants.last().end_comments,
|
||||||
|
has_nl: false
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.comments(node.comments, has_nl: false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//=== Specific Expr methods ===//
|
//=== Specific Expr methods ===//
|
||||||
|
@ -5,6 +5,20 @@ pub type PublicBar = Bar | Foo | FooBar
|
|||||||
type Uint = u16 | u32 | u64 | u8 // This should stay on the same line
|
type Uint = u16 | u32 | u64 | u8 // This should stay on the same line
|
||||||
type Float = f32 | f64
|
type Float = f32 | f64
|
||||||
|
|
||||||
|
// Callback represents all the possible callbacks
|
||||||
|
pub type Callback = ConnectedCallback // connected callback
|
||||||
|
| DisconnectedCallback // disconnected callback
|
||||||
|
| LoggedOffCallback // logged off callback
|
||||||
|
| LoggedOnCallback // logged on callback
|
||||||
|
|
||||||
|
struct ConnectedCallback {}
|
||||||
|
|
||||||
|
struct DisconnectedCallback {}
|
||||||
|
|
||||||
|
struct LoggedOnCallback {}
|
||||||
|
|
||||||
|
struct LoggedOffCallback {}
|
||||||
|
|
||||||
// Alias type
|
// Alias type
|
||||||
type MyInt = int
|
type MyInt = int
|
||||||
|
|
||||||
|
@ -5,6 +5,17 @@ pub type PublicBar = Bar | Foo | FooBar
|
|||||||
type Uint = u16 | u32 | u64 | u8 // This should stay on the same line
|
type Uint = u16 | u32 | u64 | u8 // This should stay on the same line
|
||||||
type Float = f32 | f64
|
type Float = f32 | f64
|
||||||
|
|
||||||
|
// Callback represents all the possible callbacks
|
||||||
|
pub type Callback = ConnectedCallback // connected callback
|
||||||
|
| LoggedOnCallback // logged on callback
|
||||||
|
| DisconnectedCallback // disconnected callback
|
||||||
|
| LoggedOffCallback // logged off callback
|
||||||
|
|
||||||
|
struct ConnectedCallback{}
|
||||||
|
struct DisconnectedCallback{}
|
||||||
|
struct LoggedOnCallback{}
|
||||||
|
struct LoggedOffCallback{}
|
||||||
|
|
||||||
// Alias type
|
// Alias type
|
||||||
type MyInt = int
|
type MyInt = int
|
||||||
|
|
||||||
|
@ -371,6 +371,7 @@ pub fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode {
|
|||||||
for {
|
for {
|
||||||
type_start_pos := p.tok.pos()
|
type_start_pos := p.tok.pos()
|
||||||
typ := p.parse_type()
|
typ := p.parse_type()
|
||||||
|
end_comments := p.eat_comments(same_line: true)
|
||||||
// TODO: needs to be its own var, otherwise TCC fails because of a known stack error
|
// TODO: needs to be its own var, otherwise TCC fails because of a known stack error
|
||||||
prev_tok := p.prev_tok
|
prev_tok := p.prev_tok
|
||||||
type_end_pos := prev_tok.pos()
|
type_end_pos := prev_tok.pos()
|
||||||
@ -378,6 +379,7 @@ pub fn (mut p Parser) parse_sum_type_variants() []ast.TypeNode {
|
|||||||
types << ast.TypeNode{
|
types << ast.TypeNode{
|
||||||
typ: typ
|
typ: typ
|
||||||
pos: type_pos
|
pos: type_pos
|
||||||
|
end_comments: end_comments
|
||||||
}
|
}
|
||||||
if p.tok.kind != .pipe {
|
if p.tok.kind != .pipe {
|
||||||
break
|
break
|
||||||
|
@ -4099,7 +4099,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
|||||||
name_pos)
|
name_pos)
|
||||||
return ast.SumTypeDecl{}
|
return ast.SumTypeDecl{}
|
||||||
}
|
}
|
||||||
comments = p.eat_comments(same_line: true)
|
|
||||||
return ast.SumTypeDecl{
|
return ast.SumTypeDecl{
|
||||||
name: name
|
name: name
|
||||||
typ: typ
|
typ: typ
|
||||||
@ -4109,7 +4108,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
|||||||
attrs: p.attrs
|
attrs: p.attrs
|
||||||
pos: decl_pos
|
pos: decl_pos
|
||||||
name_pos: name_pos
|
name_pos: name_pos
|
||||||
comments: comments
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// type MyType = int
|
// type MyType = int
|
||||||
|
Loading…
x
Reference in New Issue
Block a user