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:
@@ -213,6 +213,7 @@ pub struct StructInit {
|
|||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
is_short bool
|
is_short bool
|
||||||
|
pre_comments []Comment
|
||||||
pub mut:
|
pub mut:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
fields []StructInitField
|
fields []StructInitField
|
||||||
|
|||||||
@@ -1806,6 +1806,11 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
|||||||
} else {
|
} else {
|
||||||
f.writeln('$name{')
|
f.writeln('$name{')
|
||||||
}
|
}
|
||||||
|
f.comments(it.pre_comments, {
|
||||||
|
inline: true
|
||||||
|
has_nl: true
|
||||||
|
level: .indent
|
||||||
|
})
|
||||||
f.indent++
|
f.indent++
|
||||||
for field in it.fields {
|
for field in it.fields {
|
||||||
f.write('$field.name: ')
|
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 {
|
if !short_syntax {
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
}
|
}
|
||||||
|
pre_comments := p.eat_comments()
|
||||||
mut fields := []ast.StructInitField{}
|
mut fields := []ast.StructInitField{}
|
||||||
mut i := 0
|
mut i := 0
|
||||||
no_keys := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c}
|
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
|
len: last_pos.pos - first_pos.pos + last_pos.len
|
||||||
}
|
}
|
||||||
is_short: no_keys
|
is_short: no_keys
|
||||||
|
pre_comments: pre_comments
|
||||||
}
|
}
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user