diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 237213edaf..c70bed4f97 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -179,6 +179,7 @@ pub struct InterfaceDecl { pub: name string field_names []string + is_pub bool methods []FnDecl pos token.Position } @@ -213,9 +214,9 @@ pub: pub struct AnonFn { pub: - decl FnDecl + decl FnDecl pub mut: - typ table.Type + typ table.Type } pub struct FnDecl { @@ -390,23 +391,23 @@ pub fn (i &Ident) var_info() IdentVar { pub struct InfixExpr { pub: - op token.Kind - pos token.Position - left Expr - right Expr + op token.Kind + pos token.Position + left Expr + right Expr pub mut: - left_type table.Type - right_type table.Type - auto_locked string + left_type table.Type + right_type table.Type + auto_locked string } pub struct PostfixExpr { pub: - op token.Kind - expr Expr - pos token.Position + op token.Kind + expr Expr + pos token.Position pub mut: - auto_locked string + auto_locked string } pub struct PrefixExpr { @@ -440,20 +441,20 @@ pub mut: pub struct IfBranch { pub: - cond Expr - stmts []Stmt - pos token.Position - body_pos token.Position - comments []Comment + cond Expr + stmts []Stmt + pos token.Position + body_pos token.Position + comments []Comment pub mut: - smartcast bool // should only be true if cond is `x is sumtype`, it will be set in checker - if_expr + smartcast bool // should only be true if cond is `x is sumtype`, it will be set in checker - if_expr left_as_name string // only used in x is SumType check } pub struct UnsafeExpr { pub: - stmts []Stmt - pos token.Position + stmts []Stmt + pos token.Position } pub struct LockExpr { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 5c994d0cbd..e36267a9c3 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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') } f.imports(f.file.imports) // now that we have all autoimports, handle them 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' } @@ -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.table.cmod_prefix = cmodname + '.' } @@ -428,12 +428,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { // f.imports(f.file.imports) } ast.InterfaceDecl { - f.writeln('interface $it.name {') - for method in it.methods { - f.write('\t') - f.writeln(method.stringify(f.table, f.cur_mod).after('fn ')) - } - f.writeln('}\n') + f.interface_decl(it) } ast.Module { f.mod(it) @@ -662,6 +657,19 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { 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) { mut is_pe_amp_ce := false mut ce := ast.CastExpr{} @@ -1053,7 +1061,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { } ast.UnsafeExpr { f.writeln('unsafe {') - f.stmts(it.stmts) + f.stmts(node.stmts) f.writeln('}') } } diff --git a/vlib/v/fmt/tests/module_interface_keep.vv b/vlib/v/fmt/tests/module_interface_keep.vv new file mode 100644 index 0000000000..f07e5bffdf --- /dev/null +++ b/vlib/v/fmt/tests/module_interface_keep.vv @@ -0,0 +1,5 @@ +module module_fmt + +pub interface MyInterface { + fun() +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 904c946b96..e62ea5bbeb 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -396,6 +396,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { return ast.InterfaceDecl{ name: interface_name methods: methods + is_pub: is_pub pos: start_pos } }