vfmt: support inline comments before fields in struct initializations

pull/6757/head
Delyan Angelov 2020-11-05 18:49:52 +02:00
parent 4bf1c2fdcc
commit e80487b35c
4 changed files with 32 additions and 4 deletions

View File

@ -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

View File

@ -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: ')

View 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
}
}

View File

@ -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
}