vfmt: fix struct init indent and wrapped lines

pull/4289/head^2
Alexander Medvednikov 2020-04-08 00:59:28 +02:00
parent 2fbed2f880
commit d54150cd22
3 changed files with 55 additions and 50 deletions

View File

@ -235,7 +235,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.expr(it.cond)
f.write('; ')
f.expr(it.inc)
f.writeln('{ ')
f.writeln(' {')
f.stmts(it.stmts)
f.writeln('}')
}
@ -439,7 +439,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.writeln('\t$it.var_name |')
// TODO StructInit copy pasta
for i, field in it.fields {
f.write('\t$field: ')
f.write('$field: ')
f.expr(it.exprs[i])
f.writeln('')
}
@ -625,11 +625,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write('$name{}')
} else {
f.writeln('$name{')
f.indent++
for i, field in it.fields {
f.write('\t$field: ')
f.write('$field: ')
f.expr(it.exprs[i])
f.writeln('')
}
f.indent--
f.write('}')
}
}
@ -652,6 +654,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
fn (f mut Fmt) wrap_long_line() {
if f.line_len > max_len {
if f.out.buf[f.out.buf.len - 1] == ' ' {
f.out.go_back(1)
}
f.write('\n' + tabs[f.indent + 1])
f.line_len = 0
}
@ -713,8 +718,8 @@ fn short_module(name string) string {
}
fn (f mut Fmt) if_expr(it ast.IfExpr) {
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && it.branches[1].stmts.len ==
1 && (it.is_expr || f.is_assign)
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 &&
it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign)
f.single_line_if = single_line
for i, branch in it.branches {
if branch.comment.text != '' {

View File

@ -661,9 +661,9 @@ pub fn (p mut Parser) name_expr() ast.Expr {
x := p.call_expr(is_c, mod) // TODO `node,typ :=` should work
node = x
}
} else if p.peek_tok.kind == .lcbr && !p.inside_match_case &&
( is_c || p.tok.lit[0].is_capital() || (p.builtin_mod && p.tok.lit in table.builtin_type_names) ) &&
( p.tok.lit.len in [1, 2, 3] || !p.tok.lit[p.tok.lit.len - 1].is_capital() || p.table.known_type(p.tok.lit) ) {
} else if p.peek_tok.kind == .lcbr && !p.inside_match_case && (is_c || p.tok.lit[0].is_capital() ||
(p.builtin_mod && p.tok.lit in table.builtin_type_names)) && (p.tok.lit.len in [1, 2, 3] ||
!p.tok.lit[p.tok.lit.len - 1].is_capital() || p.table.known_type(p.tok.lit)) {
// short_syntax: false
return p.struct_init(false)
} else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var) {
@ -1287,7 +1287,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
}
} else {
// [1,2,3]
for i := 0; p.tok.kind != .rsbr; i++{
for i := 0; p.tok.kind != .rsbr; i++ {
expr := p.expr(0)
exprs << expr
if p.tok.kind == .comma {