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

pull/9133/head
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
1 changed files with 79 additions and 73 deletions

View File

@ -476,17 +476,25 @@ 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) }
ast.FnTypeDecl { f.fn_type_decl(node) }
ast.SumTypeDecl { f.sum_type_decl(node) }
}
}
pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) {
if node.is_pub { if node.is_pub {
f.write('pub ') f.write('pub ')
} }
ptype := f.table.type_to_str(node.parent_type) ptype := f.table.type_to_str(node.parent_type)
f.write('type $node.name = $ptype') f.write('type $node.name = $ptype')
comments << node.comments
} f.comments(node.comments, has_nl: false)
ast.FnTypeDecl { f.writeln('\n')
}
pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
if node.is_pub { if node.is_pub {
f.write('pub ') f.write('pub ')
} }
@ -529,9 +537,12 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
} else if fn_info.return_type.has_flag(.optional) { } else if fn_info.return_type.has_flag(.optional) {
f.write(' ?') f.write(' ?')
} }
comments << node.comments
} f.comments(node.comments, has_nl: false)
ast.SumTypeDecl { f.writeln('\n')
}
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
if node.is_pub { if node.is_pub {
f.write('pub ') f.write('pub ')
} }
@ -550,13 +561,8 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
f.wrap_long_line(3, true) f.wrap_long_line(3, true)
} }
} }
comments << node.comments
} f.comments(node.comments, has_nl: false)
}
if comments.len > 0 {
f.write(' ')
f.comments(comments, has_nl: false)
}
f.writeln('\n') f.writeln('\n')
} }