From 6da66226e55cbf05acd8e6164c03e781f5d6cf0b Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 4 Mar 2021 15:30:30 +0200 Subject: [PATCH] vfmt: keep `mut:` in interface declarations (#9053) --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/fmt.v | 5 ++++- vlib/v/fmt/tests/interface_with_mut_fields_keep.vv | 6 ++++++ vlib/v/parser/struct.v | 3 +++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/interface_with_mut_fields_keep.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index a3bb7e03ad..930998823c 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -231,6 +231,7 @@ pub: field_names []string is_pub bool methods []FnDecl + mut_pos int // mut: fields []StructField pos token.Position pre_comments []Comment diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index d87df10417..b3681ff55d 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -817,7 +817,10 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { f.writeln('') } f.comments_after_last_field(node.pre_comments) - for field in node.fields { + for i, field in node.fields { + if i == node.mut_pos { + f.writeln('mut:') + } // TODO: alignment, comments, etc. mut ft := f.no_cur_mod(f.table.type_to_str(field.typ)) if !ft.contains('C.') && !ft.contains('JS.') && !ft.contains('fn (') { diff --git a/vlib/v/fmt/tests/interface_with_mut_fields_keep.vv b/vlib/v/fmt/tests/interface_with_mut_fields_keep.vv new file mode 100644 index 0000000000..4b8595b45c --- /dev/null +++ b/vlib/v/fmt/tests/interface_with_mut_fields_keep.vv @@ -0,0 +1,6 @@ +interface Toto { + a int +mut: + b int + f() +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index bf781148ce..9d0c612868 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -461,6 +461,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { mut fields := []ast.StructField{cap: 20} mut methods := []ast.FnDecl{cap: 20} mut is_mut := false + mut mut_pos := -1 for p.tok.kind != .rcbr && p.tok.kind != .eof { if p.tok.kind == .key_mut { if is_mut { @@ -470,6 +471,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { p.next() p.check(.colon) is_mut = true + mut_pos = fields.len } if p.peek_tok.kind == .lpar { method_start_pos := p.tok.position() @@ -566,5 +568,6 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { is_pub: is_pub pos: pos pre_comments: pre_comments + mut_pos: mut_pos } }