mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: support inline comments before fields in struct initializations
This commit is contained in:
parent
4bf1c2fdcc
commit
e80487b35c
@ -211,11 +211,12 @@ pub mut:
|
||||
|
||||
pub struct StructInit {
|
||||
pub:
|
||||
pos token.Position
|
||||
is_short bool
|
||||
pos token.Position
|
||||
is_short bool
|
||||
pre_comments []Comment
|
||||
pub mut:
|
||||
typ table.Type
|
||||
fields []StructInitField
|
||||
typ table.Type
|
||||
fields []StructInitField
|
||||
}
|
||||
|
||||
// import statement
|
||||
|
@ -1806,6 +1806,11 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||
} else {
|
||||
f.writeln('$name{')
|
||||
}
|
||||
f.comments(it.pre_comments, {
|
||||
inline: true
|
||||
has_nl: true
|
||||
level: .indent
|
||||
})
|
||||
f.indent++
|
||||
for field in it.fields {
|
||||
f.write('$field.name: ')
|
||||
|
20
vlib/v/fmt/tests/struct_init_with_comments_keep.vv
Normal file
20
vlib/v/fmt/tests/struct_init_with_comments_keep.vv
Normal file
@ -0,0 +1,20 @@
|
||||
module abcde
|
||||
|
||||
pub struct Builder {
|
||||
pub mut:
|
||||
// TODO
|
||||
buf []byte
|
||||
str_calls int
|
||||
len int
|
||||
initial_size int = 1
|
||||
}
|
||||
|
||||
pub fn new_builder(initial_size int) Builder {
|
||||
return Builder{
|
||||
// buf: make(0, initial_size)
|
||||
buf: []byte{cap: initial_size}
|
||||
str_calls: 0 // after str_calls
|
||||
len: 0 // after len
|
||||
initial_size: initial_size // final
|
||||
}
|
||||
}
|
@ -299,6 +299,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||
if !short_syntax {
|
||||
p.check(.lcbr)
|
||||
}
|
||||
pre_comments := p.eat_comments()
|
||||
mut fields := []ast.StructInitField{}
|
||||
mut i := 0
|
||||
no_keys := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c}
|
||||
@ -354,6 +355,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||
len: last_pos.pos - first_pos.pos + last_pos.len
|
||||
}
|
||||
is_short: no_keys
|
||||
pre_comments: pre_comments
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user