fmt: heavily refactor and improve/fix logic for structs that use short args syntax (#7653)
parent
ef6011b94c
commit
97bfabf194
|
@ -1931,22 +1931,19 @@ 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
|
||||||
if !use_short_args {
|
if !use_short_args {
|
||||||
f.writeln('$name{')
|
f.writeln('$name{')
|
||||||
}
|
}
|
||||||
|
init_start := f.out.len
|
||||||
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
|
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
|
||||||
f.indent++
|
f.indent++
|
||||||
mut short_args_multiline := false
|
short_args_loop: for {
|
||||||
mut field_start_positions := []int{}
|
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
field_start_positions << f.out.len
|
|
||||||
f.write('$field.name: ')
|
f.write('$field.name: ')
|
||||||
f.prefix_expr_cast_expr(field.expr)
|
f.prefix_expr_cast_expr(field.expr)
|
||||||
if field.expr is ast.StructInit {
|
|
||||||
short_args_multiline = true
|
|
||||||
}
|
|
||||||
f.comments(field.comments, inline: true, has_nl: false, level: .indent)
|
f.comments(field.comments, inline: true, has_nl: false, level: .indent)
|
||||||
if use_short_args {
|
if use_short_args && !multiline_short_args {
|
||||||
if i < it.fields.len - 1 {
|
if i < it.fields.len - 1 {
|
||||||
f.write(', ')
|
f.write(', ')
|
||||||
}
|
}
|
||||||
|
@ -1954,18 +1951,18 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
f.comments(field.next_comments, inline: false, has_nl: true, level: .keep)
|
f.comments(field.next_comments, inline: false, has_nl: true, level: .keep)
|
||||||
}
|
if use_short_args && !multiline_short_args &&
|
||||||
if use_short_args {
|
(field.comments.len > 0 ||
|
||||||
if f.line_len > max_len[3] || short_args_multiline {
|
field.next_comments.len > 0 || !expr_is_single_line(field.expr) || f.line_len > max_len.last()) {
|
||||||
mut fields := []string{}
|
multiline_short_args = true
|
||||||
for pos in field_start_positions.reverse() {
|
f.out.go_back_to(init_start)
|
||||||
fields << f.out.cut_last(f.out.len - pos).trim_suffix(', ')
|
f.line_len = init_start
|
||||||
}
|
f.remove_new_line()
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
for field in fields.reverse() {
|
continue short_args_loop
|
||||||
f.writeln(field)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
f.indent--
|
f.indent--
|
||||||
if !use_short_args {
|
if !use_short_args {
|
||||||
|
|
|
@ -18,6 +18,21 @@ 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')
|
||||||
|
// pre comment
|
||||||
|
bar_func(x: 'struct has a pre comment')
|
||||||
|
bar_func(
|
||||||
|
x: 'first field'
|
||||||
|
// comment between fields
|
||||||
|
y: 100
|
||||||
|
)
|
||||||
|
bar_func(
|
||||||
|
x: 'Look! A comment to my right.' // comment after field
|
||||||
|
)
|
||||||
|
func_from_other_file(
|
||||||
|
xyz: AnotherStruct{
|
||||||
|
f: 'here'
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bar_func(bar Bar) {
|
fn bar_func(bar Bar) {
|
||||||
|
|
|
@ -459,12 +459,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
method.comments = mcomments
|
method.comments = mcomments
|
||||||
methods << method
|
methods << method
|
||||||
// println('register method $name')
|
// println('register method $name')
|
||||||
ts.register_method(
|
ts.register_method(name: name, params: args, return_type: method.return_type, is_pub: true)
|
||||||
name: name
|
|
||||||
params: args
|
|
||||||
return_type: method.return_type
|
|
||||||
is_pub: true
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
p.top_level_statement_end()
|
p.top_level_statement_end()
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
|
|
|
@ -519,12 +519,7 @@ pub fn (mut t Table) register_builtin_type_symbols() {
|
||||||
t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin')
|
t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin')
|
||||||
t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin')
|
t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin')
|
||||||
t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin')
|
t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin')
|
||||||
t.register_type_symbol(
|
t.register_type_symbol(kind: .any_float, name: 'any_float', cname: 'any_float', mod: 'builtin')
|
||||||
kind: .any_float
|
|
||||||
name: 'any_float'
|
|
||||||
cname: 'any_float'
|
|
||||||
mod: 'builtin'
|
|
||||||
)
|
|
||||||
t.register_type_symbol(kind: .any_int, name: 'any_int', cname: 'any_int', mod: 'builtin')
|
t.register_type_symbol(kind: .any_int, name: 'any_int', cname: 'any_int', mod: 'builtin')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue