1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: minor fixes + tests

This commit is contained in:
Alexander Medvednikov 2020-04-07 04:05:59 +02:00
parent 887f1a73f7
commit fd75cce0f3
5 changed files with 38 additions and 22 deletions

View File

@ -78,7 +78,7 @@ const (
'vlib/time/time_test.v', 'vlib/time/time_test.v',
'vlib/time/misc/misc_test.v', 'vlib/time/misc/misc_test.v',
'vlib/v/doc/doc_test.v', 'vlib/v/doc/doc_test.v',
// 'vlib/v/fmt/fmt_keep_test.v', 'vlib/v/fmt/fmt_keep_test.v',
'vlib/v/fmt/fmt_test.v', 'vlib/v/fmt/fmt_test.v',
'vlib/v/gen/cgen_test.v', 'vlib/v/gen/cgen_test.v',
// 'vlib/v/parser/parser_test.v', // 'vlib/v/parser/parser_test.v',

View File

@ -2,11 +2,8 @@
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module ast module ast
/*
These methods are used only by vfmt, vdoc, and for debugging.
*/
// These methods are used only by vfmt, vdoc, and for debugging.
import ( import (
v.table v.table
strings strings
@ -21,7 +18,10 @@ pub fn (node &FnDecl) str(t &table.Table) string {
if node.is_method { if node.is_method {
sym := t.get_type_symbol(node.receiver.typ) sym := t.get_type_symbol(node.receiver.typ)
name := sym.name.after('.') name := sym.name.after('.')
m := if node.rec_mut { 'mut ' } else { '' } mut m := if node.rec_mut { 'mut ' } else { '' }
if !node.rec_mut && table.type_is_ptr(node.receiver.typ) {
m = '&'
}
receiver = '($node.receiver.name $m$name) ' receiver = '($node.receiver.name $m$name) '
} }
name := node.name.after('.') name := node.name.after('.')
@ -35,14 +35,13 @@ pub fn (node &FnDecl) str(t &table.Table) string {
continue continue
} }
is_last_arg := i == node.args.len - 1 is_last_arg := i == node.args.len - 1
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i ==
(node.is_variadic && i == node.args.len - 2) node.args.len - 2)
f.write(arg.name) f.write(arg.name)
if should_add_type { if should_add_type {
if node.is_variadic && is_last_arg { if node.is_variadic && is_last_arg {
f.write(' ...' + t.type_to_str(arg.typ)) f.write(' ...' + t.type_to_str(arg.typ))
} } else {
else {
f.write(' ' + t.type_to_str(arg.typ)) f.write(' ' + t.type_to_str(arg.typ))
} }
} }

View File

@ -1,8 +1,3 @@
import os
fn foo(a []os.File) {
}
struct IfExpr { struct IfExpr {
} }

View File

@ -0,0 +1,22 @@
import os
fn foo(a []os.File) {
}
struct User {
age int
name string
}
fn handle_users(users []User) {
println(users.len)
}
fn (u &User) foo(u2 &User) {
}
type Expr = IfExpr | IntegerLiteral
fn exprs(e []Expr) {
println(e.len)
}

View File

@ -613,7 +613,7 @@ pub fn (table &Table) type_to_str(t Type) string {
if vals.len > 2 { if vals.len > 2 {
res = vals[vals.len - 2] + '.' + vals[vals.len - 1] res = vals[vals.len - 2] + '.' + vals[vals.len - 1]
} }
if sym.kind == .array { if sym.kind == .array && !res.starts_with('[]') {
res = '[]' + res res = '[]' + res
} }
} }