vfmt: more fixes; enable `v fmt` and `v fmt -w`

pull/4243/head
Alexander Medvednikov 2020-04-05 03:37:38 +02:00
parent 97db4c0e9a
commit 2a06263a5b
5 changed files with 33 additions and 27 deletions

View File

@ -7,7 +7,7 @@
<a href='https://patreon.com/vlang'><img src='https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.herokuapp.com%2Fvlang%2Fpledges&style=for-the-badge' height='20'></a>
[![Twitter handle][]][Twitter badge]
**V's backend has just been replaced on April 1, 15k lines of code were removed, some things are broken, we are working hard to fix them all by April 4.**
**V's backend has just been replaced on April 1, 15k lines of code were removed, some things are broken, we are working hard to fix them all by April 5.**
https://vlang.io

View File

@ -43,10 +43,10 @@ const (
)
fn main() {
if os.getenv('VFMT_ENABLE') == '' {
eprintln('v fmt is disabled for now')
exit(1)
}
//if os.getenv('VFMT_ENABLE') == '' {
//eprintln('v fmt is disabled for now')
//exit(1)
//}
toolexe := os.executable()
util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe))))
args := join_flags_and_argument()
@ -54,7 +54,7 @@ fn main() {
is_2: '-2' in args
is_c: '-c' in args
is_l: '-l' in args
is_w: '-ww' in args
is_w: '-w' in args
is_diff: '-diff' in args
is_verbose: '-verbose' in args || '--verbose' in args
is_all: '-all' in args || '--all' in args

View File

@ -361,7 +361,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
match node {
ast.ArrayInit {
// `x := []string`
if it.exprs.len == 0 && it.typ != 0 {
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
f.write(f.table.type_to_str(it.typ))
}
// `[1,2,3]`
@ -420,7 +420,8 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.or_expr(it.or_block)
}
else {
f.write('${it.name}(')
name := short_module(it.name)
f.write('${name}(')
f.call_args(it.args)
f.write(')')
f.or_expr(it.or_block)
@ -472,7 +473,8 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write('_')
}
else {
f.write('$it.name')
name := short_module(it.name)
f.write(name)
}
}
ast.InfixExpr {
@ -518,8 +520,8 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.writeln(' {')
f.indent++
for i, branch in it.branches {
// normal branch
if i < it.branches.len - 1 {
// normal branch
for j, expr in branch.exprs {
f.expr(expr)
if j < branch.exprs.len - 1 {
@ -527,8 +529,8 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
}
}
// else branch
else {
// else branch
f.write('else')
}
if branch.stmts.len == 0 {
@ -602,12 +604,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ)
name := short_module(type_sym.name)
// `Foo{}` on one line if there are no fields
if it.fields.len == 0 {
f.write('$type_sym.name{}')
f.write('$name{}')
}
else {
f.writeln('$type_sym.name{')
f.writeln('$name{')
for i, field in it.fields {
f.write('\t$field: ')
f.expr(it.exprs[i])
@ -676,3 +679,15 @@ fn (f mut Fmt) comment(node ast.Comment) {
}
f.writeln('*/')
}
// foo.bar.fn() => bar.fn()
fn short_module(name string) string {
if !name.contains('.') {
return name
}
vals := name.split('.')
if vals.len < 2 {
return name
}
return vals[vals.len-2] + '.' + vals[vals.len-1]
}

View File

@ -35,36 +35,24 @@ mut:
is_amp bool
returns bool
inside_match_case bool
inside_match_case bool // to separate `match_expr { }` from `Struct{}`
comments []ast.Comment
// sdfsdfd
}
//inside_match_case bool // to separate `match_expr { }` from `Struct{}`
// prefix_parse_fns []PrefixParseFn
// vars []string
// for tests
pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
s := scanner.new_scanner(text, .skip_comments)
a := 324
mut p := Parser{
scanner: s
table: table
pref: &pref.Preferences{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
global_scope: &ast.Scope{
start_pos: 0
parent: 0
}
}
// scope: &ast.Scope{start_pos: 0, parent: 0}
p.init_parse_fns()
p.read_first_token()
return p.stmt()

View File

@ -612,6 +612,9 @@ pub fn (table &Table) type_to_str(t Type) string {
if vals.len > 2 {
res = vals[vals.len - 2] + '.' + vals[vals.len - 1]
}
if sym.kind == .array {
res = '[]' + res
}
}
if type_is(t, .optional) {
res = '?' + res