fmt: fix unexpected `comment`, expecting struct key for trailing arg syntax (#7658)
parent
fe7fa7077e
commit
2360762a42
|
@ -250,8 +250,8 @@ pub struct StructInit {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
is_short bool
|
is_short bool
|
||||||
pre_comments []Comment
|
|
||||||
pub mut:
|
pub mut:
|
||||||
|
pre_comments []Comment
|
||||||
typ table.Type
|
typ table.Type
|
||||||
fields []StructInitField
|
fields []StructInitField
|
||||||
embeds []StructInitEmbed
|
embeds []StructInitEmbed
|
||||||
|
|
|
@ -1932,14 +1932,18 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||||
} else {
|
} else {
|
||||||
use_short_args := f.use_short_fn_args
|
use_short_args := f.use_short_fn_args
|
||||||
f.use_short_fn_args = false
|
f.use_short_fn_args = false
|
||||||
mut multiline_short_args := false
|
mut multiline_short_args := it.pre_comments.len > 0
|
||||||
if !use_short_args {
|
if !use_short_args {
|
||||||
f.writeln('$name{')
|
f.writeln('$name{')
|
||||||
|
} else {
|
||||||
|
if multiline_short_args {
|
||||||
|
f.writeln('')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
init_start := f.out.len
|
init_start := f.out.len
|
||||||
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
|
|
||||||
f.indent++
|
f.indent++
|
||||||
short_args_loop: for {
|
short_args_loop: for {
|
||||||
|
f.comments(it.pre_comments, inline: true, has_nl: true, level: .keep)
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
f.write('$field.name: ')
|
f.write('$field.name: ')
|
||||||
f.prefix_expr_cast_expr(field.expr)
|
f.prefix_expr_cast_expr(field.expr)
|
||||||
|
|
|
@ -20,6 +20,15 @@ fn main() {
|
||||||
x: 0
|
x: 0
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
ui.row(
|
||||||
|
// stretch: true
|
||||||
|
margin: {
|
||||||
|
top: 10
|
||||||
|
left: 10
|
||||||
|
right: 10
|
||||||
|
bottom: 10
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bar_func(bar Bar) {
|
fn bar_func(bar Bar) {
|
||||||
|
|
|
@ -14,6 +14,10 @@ fn main() {
|
||||||
bar_func(x: 'some string', b: Baz{
|
bar_func(x: 'some string', b: Baz{
|
||||||
x: 0
|
x: 0
|
||||||
})
|
})
|
||||||
|
ui.row({
|
||||||
|
//stretch: true
|
||||||
|
margin: {top:10,left:10,right:10,bottom:10}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bar_func(bar Bar) {
|
fn bar_func(bar Bar) {
|
||||||
|
|
|
@ -18,8 +18,10 @@ fn main() {
|
||||||
})
|
})
|
||||||
bar_func(x: 'bar', y: 2, z: 3, a: 4)
|
bar_func(x: 'bar', y: 2, z: 3, a: 4)
|
||||||
func_from_other_file(val: 'something')
|
func_from_other_file(val: 'something')
|
||||||
|
bar_func(
|
||||||
// pre comment
|
// pre comment
|
||||||
bar_func(x: 'struct has a pre comment')
|
x: 'struct has a pre comment'
|
||||||
|
)
|
||||||
bar_func(
|
bar_func(
|
||||||
x: 'first field'
|
x: 'first field'
|
||||||
// comment between fields
|
// comment between fields
|
||||||
|
|
|
@ -125,7 +125,11 @@ pub fn (mut p Parser) call_args() []ast.CallArg {
|
||||||
}
|
}
|
||||||
mut comments := p.eat_comments()
|
mut comments := p.eat_comments()
|
||||||
arg_start_pos := p.tok.position()
|
arg_start_pos := p.tok.position()
|
||||||
e := p.expr(0)
|
mut e := p.expr(0)
|
||||||
|
if mut e is ast.StructInit {
|
||||||
|
e.pre_comments << comments
|
||||||
|
comments = []ast.Comment{}
|
||||||
|
}
|
||||||
pos := arg_start_pos.extend(p.prev_tok.position())
|
pos := arg_start_pos.extend(p.prev_tok.position())
|
||||||
comments << p.eat_comments()
|
comments << p.eat_comments()
|
||||||
args << ast.CallArg{
|
args << ast.CallArg{
|
||||||
|
|
|
@ -197,7 +197,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
|
||||||
// it should be a struct
|
// it should be a struct
|
||||||
if p.peek_tok.kind == .pipe {
|
if p.peek_tok.kind == .pipe {
|
||||||
node = p.assoc()
|
node = p.assoc()
|
||||||
} else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr {
|
} else if p.peek_tok.kind == .colon || p.tok.kind in [.rcbr, .comment] {
|
||||||
node = p.struct_init(true) // short_syntax: true
|
node = p.struct_init(true) // short_syntax: true
|
||||||
} else if p.tok.kind == .name {
|
} else if p.tok.kind == .name {
|
||||||
p.next()
|
p.next()
|
||||||
|
|
Loading…
Reference in New Issue