vfmt: support inline comments before fields in struct initializations
parent
4bf1c2fdcc
commit
e80487b35c
|
@ -211,11 +211,12 @@ pub mut:
|
||||||
|
|
||||||
pub struct StructInit {
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// import statement
|
// import statement
|
||||||
|
|
|
@ -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: ')
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue