table, vfmt: fix typ_to_string + some fixes to vfmt

pull/4945/head
Ned Palacios 2020-05-18 21:37:06 +08:00 committed by GitHub
parent ebdfe9a9c5
commit d94d436273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -448,7 +448,8 @@ fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.write(strings.repeat(` `, max - field.name.len))
f.write(f.type_to_str(field.typ))
if field.has_default_expr {
f.write(' = ${field.default_expr.str()}')
f.write(' = ')
f.expr(field.default_expr)
}
// f.write('// $field.pos.line_nr')
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
@ -1053,7 +1054,7 @@ fn (mut f Fmt) struct_init(it ast.StructInit) {
if it.fields.len == 0 {
// `Foo{}` on one line if there are no fields
f.write('$name{}')
} else if it.fields.len == 0 {
} else if it.is_short {
// `Foo{1,2,3}` (short syntax )
// if name != '' {
f.write('$name{')

View File

@ -624,8 +624,12 @@ pub:
pub fn (table &Table) type_to_str(t Type) string {
sym := table.get_type_symbol(t)
mut res := sym.name
if sym.kind == .multi_return {
mut res := '('
res = '('
if t.flag_is(.optional) {
res = '?' + res
}
mr_info := sym.info as MultiReturn
for i, typ in mr_info.types {
res += table.type_to_str(typ)
@ -636,10 +640,9 @@ pub fn (table &Table) type_to_str(t Type) string {
res += ')'
return res
}
mut res := sym.name
if sym.kind == .array {
if sym.kind == .array || 'array_' in res {
res = res.replace('array_', '[]')
} else if sym.kind == .map {
} else if sym.kind == .map || 'map_string_' in res {
res = res.replace('map_string_', 'map[string]')
}
// mod.submod.submod2.Type => submod2.Type