mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fmt: move struct init logic to a separate fn
This commit is contained in:
parent
148661ead6
commit
c5d295c378
@ -691,36 +691,7 @@ fn (mut f Fmt) expr(node ast.Expr) {
|
||||
f.write("'")
|
||||
}
|
||||
ast.StructInit {
|
||||
type_sym := f.table.get_type_symbol(it.typ)
|
||||
// f.write('<old name: $type_sym.name>')
|
||||
mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str?
|
||||
if name == 'void' {
|
||||
name = ''
|
||||
}
|
||||
if it.fields.len == 0 {
|
||||
// `Foo{}` on one line if there are no fields
|
||||
f.write('$name{}')
|
||||
} else if it.fields.len == 0 {
|
||||
// `Foo{1,2,3}` (short syntax )
|
||||
f.write('$name{')
|
||||
for i, field in it.fields {
|
||||
f.expr(field.expr)
|
||||
if i < it.fields.len - 1 {
|
||||
f.write(', ')
|
||||
}
|
||||
}
|
||||
f.write('}')
|
||||
} else {
|
||||
f.writeln('$name{')
|
||||
f.indent++
|
||||
for field in it.fields {
|
||||
f.write('$field.name: ')
|
||||
f.expr(field.expr)
|
||||
f.writeln('')
|
||||
}
|
||||
f.indent--
|
||||
f.write('}')
|
||||
}
|
||||
f.struct_init(it)
|
||||
}
|
||||
ast.Type {
|
||||
f.write(f.type_to_str(it.typ))
|
||||
@ -1026,7 +997,6 @@ fn (mut f Fmt) array_init(it ast.ArrayInit) {
|
||||
f.write('}')
|
||||
return
|
||||
}
|
||||
|
||||
f.write(f.type_to_str(it.typ))
|
||||
f.write('{')
|
||||
if it.has_cap {
|
||||
@ -1069,3 +1039,38 @@ fn (mut f Fmt) array_init(it ast.ArrayInit) {
|
||||
}
|
||||
f.write(']')
|
||||
}
|
||||
|
||||
fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||
type_sym := f.table.get_type_symbol(it.typ)
|
||||
// f.write('<old name: $type_sym.name>')
|
||||
mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str?
|
||||
if name == 'void' {
|
||||
name = ''
|
||||
}
|
||||
if it.fields.len == 0 {
|
||||
// `Foo{}` on one line if there are no fields
|
||||
f.write('$name{}')
|
||||
} else if it.fields.len == 0 {
|
||||
// `Foo{1,2,3}` (short syntax )
|
||||
// if name != '' {
|
||||
f.write('$name{')
|
||||
// }
|
||||
for i, field in it.fields {
|
||||
f.expr(field.expr)
|
||||
if i < it.fields.len - 1 {
|
||||
f.write(', ')
|
||||
}
|
||||
}
|
||||
f.write('}')
|
||||
} else {
|
||||
f.writeln('$name{')
|
||||
f.indent++
|
||||
for field in it.fields {
|
||||
f.write('$field.name: ')
|
||||
f.expr(field.expr)
|
||||
f.writeln('')
|
||||
}
|
||||
f.indent--
|
||||
f.write('}')
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user