1
0
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:
Alexander Medvednikov 2020-05-05 15:06:51 +02:00
parent 148661ead6
commit c5d295c378

View File

@ -691,36 +691,7 @@ fn (mut f Fmt) expr(node ast.Expr) {
f.write("'") f.write("'")
} }
ast.StructInit { ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ) f.struct_init(it)
// 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('}')
}
} }
ast.Type { ast.Type {
f.write(f.type_to_str(it.typ)) f.write(f.type_to_str(it.typ))
@ -1010,11 +981,11 @@ fn (mut f Fmt) array_init(it ast.ArrayInit) {
typ_sym := f.table.get_type_symbol(it.typ) typ_sym := f.table.get_type_symbol(it.typ)
if typ_sym.kind == .array && typ_sym.name.starts_with('array_map') { if typ_sym.kind == .array && typ_sym.name.starts_with('array_map') {
ainfo := typ_sym.info as table.Array ainfo := typ_sym.info as table.Array
map_typ_sym := f.table.get_type_symbol( ainfo.elem_type ) map_typ_sym := f.table.get_type_symbol(ainfo.elem_type)
minfo := map_typ_sym.info as table.Map minfo := map_typ_sym.info as table.Map
mk := f.table.get_type_symbol(minfo.key_type).name mk := f.table.get_type_symbol(minfo.key_type).name
mv := f.table.get_type_symbol(minfo.value_type).name mv := f.table.get_type_symbol(minfo.value_type).name
for _ in 0..ainfo.nr_dims { for _ in 0 .. ainfo.nr_dims {
f.write('[]') f.write('[]')
} }
f.write('map[${mk}]${mv}') f.write('map[${mk}]${mv}')
@ -1026,7 +997,6 @@ fn (mut f Fmt) array_init(it ast.ArrayInit) {
f.write('}') f.write('}')
return return
} }
f.write(f.type_to_str(it.typ)) f.write(f.type_to_str(it.typ))
f.write('{') f.write('{')
if it.has_cap { if it.has_cap {
@ -1069,3 +1039,38 @@ fn (mut f Fmt) array_init(it ast.ArrayInit) {
} }
f.write(']') 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('}')
}
}