fmt: interface decl fixes (#5829)

pull/5825/head
Enzo 2020-07-14 18:52:28 +02:00 committed by GitHub
parent 60ce938149
commit d301611de7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 30 deletions

View File

@ -179,6 +179,7 @@ pub struct InterfaceDecl {
pub: pub:
name string name string
field_names []string field_names []string
is_pub bool
methods []FnDecl methods []FnDecl
pos token.Position pos token.Position
} }

View File

@ -75,7 +75,7 @@ pub fn fmt(file ast.File, table &table.Table, is_debug bool) string {
// for comment in file.comments { println('$comment.line_nr $comment.text') } // for comment in file.comments { println('$comment.line_nr $comment.text') }
f.imports(f.file.imports) // now that we have all autoimports, handle them f.imports(f.file.imports) // now that we have all autoimports, handle them
res := f.out.str().trim_space() + '\n' res := f.out.str().trim_space() + '\n'
bounded_import_pos := util.imin(res.len,f.import_pos) bounded_import_pos := util.imin(res.len, f.import_pos)
return res[..bounded_import_pos] + f.out_imports.str() + res[bounded_import_pos..] // + '\n' return res[..bounded_import_pos] + f.out_imports.str() + res[bounded_import_pos..] // + '\n'
} }
@ -190,7 +190,7 @@ fn (mut f Fmt) adjust_complete_line() {
} }
} }
pub fn (mut f Fmt) set_current_module_name(cmodname string){ pub fn (mut f Fmt) set_current_module_name(cmodname string) {
f.cur_mod = cmodname f.cur_mod = cmodname
f.table.cmod_prefix = cmodname + '.' f.table.cmod_prefix = cmodname + '.'
} }
@ -428,12 +428,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
// f.imports(f.file.imports) // f.imports(f.file.imports)
} }
ast.InterfaceDecl { ast.InterfaceDecl {
f.writeln('interface $it.name {') f.interface_decl(it)
for method in it.methods {
f.write('\t')
f.writeln(method.stringify(f.table, f.cur_mod).after('fn '))
}
f.writeln('}\n')
} }
ast.Module { ast.Module {
f.mod(it) f.mod(it)
@ -662,6 +657,19 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('}\n') f.writeln('}\n')
} }
pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
if node.is_pub {
f.write('pub ')
}
name := node.name.after('.')
f.writeln('interface $name {')
for method in node.methods {
f.write('\t')
f.writeln(method.stringify(f.table, f.cur_mod).after('fn '))
}
f.writeln('}\n')
}
pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) { pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) {
mut is_pe_amp_ce := false mut is_pe_amp_ce := false
mut ce := ast.CastExpr{} mut ce := ast.CastExpr{}
@ -1053,7 +1061,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
} }
ast.UnsafeExpr { ast.UnsafeExpr {
f.writeln('unsafe {') f.writeln('unsafe {')
f.stmts(it.stmts) f.stmts(node.stmts)
f.writeln('}') f.writeln('}')
} }
} }

View File

@ -0,0 +1,5 @@
module module_fmt
pub interface MyInterface {
fun()
}

View File

@ -396,6 +396,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
return ast.InterfaceDecl{ return ast.InterfaceDecl{
name: interface_name name: interface_name
methods: methods methods: methods
is_pub: is_pub
pos: start_pos pos: start_pos
} }
} }