diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5b3ff495cd..eba6133981 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -29,7 +29,7 @@ pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var // TODO: replace Param pub type Node = CallArg | ConstField | EmptyNode | EnumField | Expr | File | GlobalField | IfBranch | MatchBranch | NodeError | Param | ScopeObject | SelectBranch | Stmt | StructField | - StructInitField + StructInitField | SumTypeVariant pub struct TypeNode { pub: @@ -913,6 +913,7 @@ pub: is_pub bool parent_type Type pos token.Position + type_pos token.Position comments []Comment } @@ -940,6 +941,7 @@ pub: is_pub bool typ Type pos token.Position + type_pos token.Position comments []Comment } @@ -1606,6 +1608,17 @@ pub fn (node Node) position() token.Position { for sym in node.syms { pos = pos.extend(sym.pos) } + } else if node is TypeDecl { + match node { + FnTypeDecl, AliasTypeDecl { + pos = pos.extend(node.type_pos) + } + SumTypeDecl { + for variant in node.variants { + pos = pos.extend(variant.pos) + } + } + } } if node is AssignStmt { return pos.extend(node.right.last().position()) @@ -1621,9 +1634,13 @@ pub fn (node Node) position() token.Position { StructField { return node.pos.extend(node.type_pos) } - MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg { + MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg, + SumTypeVariant { return node.pos } + Param { + return node.pos.extend(node.type_pos) + } IfBranch { return node.pos.extend(node.body_pos) } @@ -1643,9 +1660,6 @@ pub fn (node Node) position() token.Position { } } } - Param { - return node.pos.extend(node.type_pos) - } File { mut pos := token.Position{} if node.stmts.len > 0 { @@ -1767,6 +1781,11 @@ pub fn (node Node) children() []Node { children << node.params.map(Node(it)) children << node.stmts.map(Node(it)) } + TypeDecl { + if node is SumTypeDecl { + children << node.variants.map(Node(it)) + } + } else {} } } else if node is ScopeObject { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 58df0f9754..a20ab69f7a 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1304,6 +1304,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) { } f.comments(node.comments, has_nl: false) + f.writeln('') } pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) { diff --git a/vlib/v/fmt/tests/types_expected.vv b/vlib/v/fmt/tests/types_expected.vv index 26acba858d..1eed4362d6 100644 --- a/vlib/v/fmt/tests/types_expected.vv +++ b/vlib/v/fmt/tests/types_expected.vv @@ -3,7 +3,6 @@ type FooBar = Bar | Foo pub type PublicBar = Bar | Foo | FooBar type Uint = byte | u16 | u32 | u64 // This should stay on the same line - type Float = f32 | f64 // Alias type @@ -14,6 +13,7 @@ pub type Abc = f32 // Fn type decl type EmptyFn = fn () + type OneArgFn = fn (i int) type TwoDiffArgs = fn (i int, s string) bool diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index aed417392a..197a339105 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -510,6 +510,7 @@ fn (mut p Parser) fn_receiver(mut params []ast.Param, mut rec ReceiverParsingInf is_mut: rec.is_mut is_auto_rec: is_auto_rec typ: rec.typ + type_pos: rec.type_pos } p.check(.rpar) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 3d860c0c86..7d532c4ba1 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2955,12 +2955,14 @@ fn (mut p Parser) type_decl() ast.TypeDecl { // function type: `type mycallback = fn(string, int)` fn_name := p.prepend_mod(name) fn_type := p.parse_fn_type(fn_name) + type_pos = type_pos.extend(p.tok.position()) comments = p.eat_comments(same_line: true) return ast.FnTypeDecl{ name: fn_name is_pub: is_pub typ: fn_type pos: decl_pos + type_pos: type_pos comments: comments } } @@ -3045,6 +3047,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { name: name is_pub: is_pub parent_type: parent_type + type_pos: type_pos pos: decl_pos comments: comments }