1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: extract type decls code into functions to cleanup (#9126)

This commit is contained in:
zakuro 2021-03-05 21:34:51 +09:00 committed by GitHub
parent 0f042124cb
commit 9ba312066e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -476,87 +476,93 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
} }
pub fn (mut f Fmt) type_decl(node ast.TypeDecl) { pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
mut comments := []ast.Comment{}
match node { match node {
ast.AliasTypeDecl { ast.AliasTypeDecl { f.alias_type_decl(node) }
if node.is_pub { ast.FnTypeDecl { f.fn_type_decl(node) }
f.write('pub ') ast.SumTypeDecl { f.sum_type_decl(node) }
} }
ptype := f.table.type_to_str(node.parent_type) }
f.write('type $node.name = $ptype')
comments << node.comments pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) {
if node.is_pub {
f.write('pub ')
}
ptype := f.table.type_to_str(node.parent_type)
f.write('type $node.name = $ptype')
f.comments(node.comments, has_nl: false)
f.writeln('\n')
}
pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
if node.is_pub {
f.write('pub ')
}
typ_sym := f.table.get_type_symbol(node.typ)
fn_typ_info := typ_sym.info as table.FnType
fn_info := fn_typ_info.func
fn_name := f.no_cur_mod(node.name)
f.write('type $fn_name = fn (')
for i, arg in fn_info.params {
if arg.is_mut {
f.write(arg.typ.share().str() + ' ')
} }
ast.FnTypeDecl { f.write(arg.name)
if node.is_pub { mut s := f.no_cur_mod(f.table.type_to_str(arg.typ))
f.write('pub ') if arg.is_mut {
if s.starts_with('&') {
s = s[1..]
} }
typ_sym := f.table.get_type_symbol(node.typ)
fn_typ_info := typ_sym.info as table.FnType
fn_info := fn_typ_info.func
fn_name := f.no_cur_mod(node.name)
f.write('type $fn_name = fn (')
for i, arg in fn_info.params {
if arg.is_mut {
f.write(arg.typ.share().str() + ' ')
}
f.write(arg.name)
mut s := f.no_cur_mod(f.table.type_to_str(arg.typ))
if arg.is_mut {
if s.starts_with('&') {
s = s[1..]
}
}
is_last_arg := i == fn_info.params.len - 1
should_add_type := true || is_last_arg
|| fn_info.params[i + 1].typ != arg.typ
|| (fn_info.is_variadic && i == fn_info.params.len - 2)
if should_add_type {
ns := if arg.name == '' { '' } else { ' ' }
if fn_info.is_variadic && is_last_arg {
f.write(ns + '...' + s)
} else {
f.write(ns + s)
}
}
if !is_last_arg {
f.write(', ')
}
}
f.write(')')
if fn_info.return_type.idx() != table.void_type_idx {
ret_str := f.no_cur_mod(f.table.type_to_str(fn_info.return_type))
f.write(' $ret_str')
} else if fn_info.return_type.has_flag(.optional) {
f.write(' ?')
}
comments << node.comments
} }
ast.SumTypeDecl { is_last_arg := i == fn_info.params.len - 1
if node.is_pub { should_add_type := true || is_last_arg
f.write('pub ') || fn_info.params[i + 1].typ != arg.typ
|| (fn_info.is_variadic && i == fn_info.params.len - 2)
if should_add_type {
ns := if arg.name == '' { '' } else { ' ' }
if fn_info.is_variadic && is_last_arg {
f.write(ns + '...' + s)
} else {
f.write(ns + s)
} }
f.write('type $node.name = ') }
mut sum_type_names := []string{} if !is_last_arg {
for t in node.variants { f.write(', ')
sum_type_names << f.table.type_to_str(t.typ)
}
sum_type_names.sort()
for i, name in sum_type_names {
f.write(name)
if i < sum_type_names.len - 1 {
f.write(' | ')
}
if i < sum_type_names.len - 1 {
f.wrap_long_line(3, true)
}
}
comments << node.comments
} }
} }
if comments.len > 0 { f.write(')')
f.write(' ') if fn_info.return_type.idx() != table.void_type_idx {
f.comments(comments, has_nl: false) ret_str := f.no_cur_mod(f.table.type_to_str(fn_info.return_type))
f.write(' $ret_str')
} else if fn_info.return_type.has_flag(.optional) {
f.write(' ?')
} }
f.comments(node.comments, has_nl: false)
f.writeln('\n')
}
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
if node.is_pub {
f.write('pub ')
}
f.write('type $node.name = ')
mut sum_type_names := []string{}
for t in node.variants {
sum_type_names << f.table.type_to_str(t.typ)
}
sum_type_names.sort()
for i, name in sum_type_names {
f.write(name)
if i < sum_type_names.len - 1 {
f.write(' | ')
}
if i < sum_type_names.len - 1 {
f.wrap_long_line(3, true)
}
}
f.comments(node.comments, has_nl: false)
f.writeln('\n') f.writeln('\n')
} }