fmt: re-run on parser.v and fmt.v

pull/5479/head^2
Alexander Medvednikov 2020-06-24 14:35:00 +02:00
parent 090e6e936a
commit 11871d9544
3 changed files with 50 additions and 30 deletions

View File

@ -44,7 +44,7 @@ fn main() {
foptions := FormatOptions{ foptions := FormatOptions{
is_c: '-c' in args is_c: '-c' in args
is_l: '-l' in args is_l: '-l' in args
is_w: '-ww' in args is_w: '-w' in args
is_diff: '-diff' in args is_diff: '-diff' in args
is_verbose: '-verbose' in args || '--verbose' in args is_verbose: '-verbose' in args || '--verbose' in args
is_all: '-all' in args || '--all' in args is_all: '-all' in args || '--all' in args

View File

@ -84,7 +84,6 @@ fn (mut f Fmt) find_comment(line_nr int) {
} }
} }
*/ */
pub fn (mut f Fmt) write(s string) { pub fn (mut f Fmt) write(s string) {
if f.expr_recursion == 0 || f.is_inside_interp { if f.expr_recursion == 0 || f.is_inside_interp {
if f.indent > 0 && f.empty_line { if f.indent > 0 && f.empty_line {
@ -118,7 +117,7 @@ pub fn (mut f Fmt) writeln(s string) {
f.write(f.expr_bufs[i]) f.write(f.expr_bufs[i])
f.wrap_long_line(p, true) f.wrap_long_line(p, true)
} }
f.write(f.expr_bufs[f.expr_bufs.len-1]) f.write(f.expr_bufs[f.expr_bufs.len - 1])
f.expr_bufs = []string{} f.expr_bufs = []string{}
f.penalties = []int{} f.penalties = []int{}
f.expr_recursion = 0 f.expr_recursion = 0
@ -127,7 +126,11 @@ pub fn (mut f Fmt) writeln(s string) {
// println(f.indent.str() + s) // println(f.indent.str() + s)
f.out.write(tabs[f.indent]) f.out.write(tabs[f.indent])
} }
f.out.writeln(if empty_fifo {''} else {s}) f.out.writeln(if empty_fifo {
''
} else {
s
})
f.empty_line = true f.empty_line = true
f.line_len = 0 f.line_len = 0
} }
@ -140,9 +143,9 @@ fn (mut f Fmt) adjust_complete_line() {
for _, s in f.expr_bufs { for _, s in f.expr_bufs {
l += s.len l += s.len
} }
if f.line_len + l <= max_len[max_len.len-1] { if f.line_len + l <= max_len[max_len.len - 1] {
for i, _ in f.penalties { for i, _ in f.penalties {
f.penalties[i] = max_len.len-1 f.penalties[i] = max_len.len - 1
} }
} }
} }
@ -401,7 +404,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
} }
f.writeln('') f.writeln('')
} }
ast.SqlInsertExpr {} ast.SqlStmt {}
ast.StructDecl { ast.StructDecl {
f.struct_decl(it) f.struct_decl(it)
} }
@ -445,8 +448,8 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
} }
} }
is_last_arg := i == fn_info.args.len - 1 is_last_arg := i == fn_info.args.len - 1
should_add_type := is_last_arg || fn_info.args[i + 1].typ != arg.typ || (fn_info.is_variadic && should_add_type := is_last_arg || fn_info.args[i + 1].typ != arg.typ ||
i == fn_info.args.len - 2) (fn_info.is_variadic && i == fn_info.args.len - 2)
if should_add_type { if should_add_type {
if fn_info.is_variadic && is_last_arg { if fn_info.is_variadic && is_last_arg {
f.write(' ...' + s) f.write(' ...' + s)
@ -501,9 +504,11 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
end_pos := field.pos.pos + field.pos.len end_pos := field.pos.pos + field.pos.len
mut comments_len := 0 // Length of comments between field name and type mut comments_len := 0 // Length of comments between field name and type
for comment in field.comments { for comment in field.comments {
if comment.pos.pos >= end_pos { break } if comment.pos.pos >= end_pos {
break
}
if comment.pos.pos > field.pos.pos { if comment.pos.pos > field.pos.pos {
comments_len += '/* ${comment.text} */ '.len comments_len += '/* $comment.text */ '.len
} }
} }
if comments_len + field.name.len > max { if comments_len + field.name.len > max {
@ -562,7 +567,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.struct_field_expr(field.default_expr) f.struct_field_expr(field.default_expr)
} }
// Handle comments after field type (same line) // Handle comments after field type (same line)
for j < comments.len && field.pos.line_nr == comments[j].pos.line_nr{ for j < comments.len && field.pos.line_nr == comments[j].pos.line_nr {
f.write(' // ${comments[j].text}') // TODO: handle in a function f.write(' // ${comments[j].text}') // TODO: handle in a function
j++ j++
} }
@ -740,7 +745,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write(f.expr_bufs[i]) f.write(f.expr_bufs[i])
f.wrap_long_line(p, true) f.wrap_long_line(p, true)
} }
f.write(f.expr_bufs[f.expr_bufs.len-1]) f.write(f.expr_bufs[f.expr_bufs.len - 1])
f.expr_bufs = []string{} f.expr_bufs = []string{}
f.penalties = []int{} f.penalties = []int{}
} }
@ -910,7 +915,11 @@ pub fn (mut f Fmt) wrap_long_line(penalty int, add_indent bool) bool {
if f.out.buf[f.out.buf.len - 1] == ` ` { if f.out.buf[f.out.buf.len - 1] == ` ` {
f.out.go_back(1) f.out.go_back(1)
} }
f.write('\n' + tabs[f.indent + if add_indent { 1 } else { 0 }]) f.write('\n' + tabs[f.indent + if add_indent {
1
} else {
0
}])
f.line_len = 0 f.line_len = 0
return true return true
} }
@ -1015,8 +1024,10 @@ pub fn (mut f Fmt) short_module(name string) string {
} }
pub fn (mut f Fmt) if_expr(it ast.IfExpr) { pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && single_line := it.branches.len == 2 && it.has_else &&
it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign) it.branches[0].stmts.len == 1 &&
it.branches[1].stmts.len == 1 &&
(it.is_expr || f.is_assign)
f.single_line_if = single_line f.single_line_if = single_line
for i, branch in it.branches { for i, branch in it.branches {
if branch.comment.text != '' { if branch.comment.text != '' {
@ -1313,12 +1324,15 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
if last_line_nr < line_nr { if last_line_nr < line_nr {
penalty-- penalty--
} }
if i == 0 || it.exprs[i-1] is ast.ArrayInit || it.exprs[i-1] is ast.StructInit || if i == 0 || it.exprs[i - 1] is ast.ArrayInit ||
it.exprs[i-1] is ast.MapInit || it.exprs[i-1] is ast.CallExpr { it.exprs[i - 1] is ast.StructInit ||
it.exprs[i - 1] is ast.MapInit ||
it.exprs[i - 1] is ast.CallExpr {
penalty-- penalty--
} }
if expr is ast.ArrayInit || expr is ast.StructInit || if expr is ast.ArrayInit ||
expr is ast.MapInit || expr is ast.CallExpr { expr is ast.StructInit || expr is ast.MapInit ||
expr is ast.CallExpr {
penalty-- penalty--
} }
is_new_line := f.wrap_long_line(penalty, !inc_indent) is_new_line := f.wrap_long_line(penalty, !inc_indent)

View File

@ -734,8 +734,9 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt {
left0 := left[0] left0 := left[0]
if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() { if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() {
return p.partial_assign_stmt(left) return p.partial_assign_stmt(left)
} else if is_top_level && tok.kind !in [.key_if, .key_match] && left0 !is ast.CallExpr && } else if is_top_level && tok.kind !in [.key_if, .key_match] &&
left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) && left0 !is ast.CallExpr && left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr &&
(left0 as ast.InfixExpr).op == .left_shift) &&
left0 !is ast.ComptimeCall { left0 !is ast.ComptimeCall {
p.error_with_pos('expression evaluated but not used', left0.position()) p.error_with_pos('expression evaluated but not used', left0.position())
} }
@ -848,7 +849,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
else {} else {}
} }
} }
if p.peek_tok.kind == .dot && !known_var && (language != .v || p.known_import(p.tok.lit) || if p.peek_tok.kind == .dot && !known_var &&
(language != .v || p.known_import(p.tok.lit) ||
p.mod.all_after_last('.') == p.tok.lit) { p.mod.all_after_last('.') == p.tok.lit) {
if language == .c { if language == .c {
mod = 'C' mod = 'C'
@ -867,7 +869,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
} }
// p.warn('name expr $p.tok.lit $p.peek_tok.str()') // p.warn('name expr $p.tok.lit $p.peek_tok.str()')
// fn call or type cast // fn call or type cast
if p.peek_tok.kind == .lpar || (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name && if p.peek_tok.kind == .lpar ||
(p.peek_tok.kind == .lt && p.peek_tok2.kind == .name &&
p.peek_tok3.kind == .gt) { p.peek_tok3.kind == .gt) {
// foo() or foo<int>() // foo() or foo<int>()
mut name := p.tok.lit mut name := p.tok.lit
@ -877,7 +880,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
name_w_mod := p.prepend_mod(name) name_w_mod := p.prepend_mod(name)
// type cast. TODO: finish // type cast. TODO: finish
// if name in table.builtin_type_names { // if name in table.builtin_type_names {
if !known_var && (name in p.table.type_idxs || name_w_mod in p.table.type_idxs) && if !known_var && (name in p.table.type_idxs ||
name_w_mod in p.table.type_idxs) &&
name !in ['C.stat', 'C.sigaction'] { name !in ['C.stat', 'C.sigaction'] {
// TODO handle C.stat() // TODO handle C.stat()
mut to_typ := p.parse_type() mut to_typ := p.parse_type()
@ -914,8 +918,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
} else if p.peek_tok.kind == .lcbr && !p.inside_match && !p.inside_match_case && !p.inside_if && } else if p.peek_tok.kind == .lcbr && !p.inside_match && !p.inside_match_case && !p.inside_if &&
!p.inside_for { // && (p.tok.lit[0].is_capital() || p.builtin_mod) { !p.inside_for { // && (p.tok.lit[0].is_capital() || p.builtin_mod) {
return p.struct_init(false) // short_syntax: false return p.struct_init(false) // short_syntax: false
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var && language == } else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var && language == .v) {
.v) {
// `Color.green` // `Color.green`
mut enum_name := p.check_name() mut enum_name := p.check_name()
if mod != '' { if mod != '' {
@ -1332,7 +1335,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
mut comments := []ast.Comment{} mut comments := []ast.Comment{}
for p.tok.kind == .comment { for p.tok.kind == .comment {
comments << p.comment() comments << p.comment()
if p.tok.kind == .rpar {break} if p.tok.kind == .rpar {
break
}
} }
pos := p.tok.position() pos := p.tok.position()
name := p.check_name() name := p.check_name()
@ -1389,8 +1394,9 @@ const (
// left hand side of `=` or `:=` in `a,b,c := 1,2,3` // left hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (mut p Parser) global_decl() ast.GlobalDecl { fn (mut p Parser) global_decl() ast.GlobalDecl {
if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v && if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v &&
p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals && p.mod != 'ui' && p.mod != 'gg2' &&
!p.pref.is_fmt && p.mod !in global_enabled_mods { p.mod != 'uiold' && !p.pref.enable_globals && !p.pref.is_fmt &&
p.mod !in global_enabled_mods {
p.error('use `v --enable-globals ...` to enable globals') p.error('use `v --enable-globals ...` to enable globals')
} }
start_pos := p.tok.position() start_pos := p.tok.position()