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

@ -46,9 +46,9 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{}
scope: scope
global_scope: &ast.Scope{
start_pos: 0
parent: 0
}
start_pos: 0
parent: 0
}
}
p.init_parse_fns()
p.read_first_token()
@ -67,9 +67,9 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
file_name: path
pref: pref
scope: &ast.Scope{
start_pos: 0
parent: 0
}
start_pos: 0
parent: 0
}
global_scope: global_scope
}
// comments_mode: comments_mode
@ -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) {
@ -885,10 +885,10 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
left: left
pos: p.tok.position()
index: ast.RangeExpr{
low: ast.Expr{}
high: high
has_high: true
}
low: ast.Expr{}
high: high
has_high: true
}
}
}
expr := p.expr(0) // `[expr]` or `[expr..]`
@ -906,11 +906,11 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
left: left
pos: p.tok.position()
index: ast.RangeExpr{
low: expr
high: high
has_high: has_high
has_low: has_low
}
low: expr
high: high
has_high: has_high
has_low: has_low
}
}
}
// [expr]
@ -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 {
@ -1533,9 +1533,9 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
kind: .struct_
name: name
info: table.Struct{
fields: fields
is_typedef: is_typedef
}
fields: fields
is_typedef: is_typedef
}
}
mut ret := 0
if p.builtin_mod && t.name in table.builtin_type_names {
@ -1868,8 +1868,8 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
kind: .enum_
name: name
info: table.Enum{
vals: vals
}
vals: vals
}
})
return ast.EnumDecl{
name: name
@ -1902,8 +1902,8 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
kind: .sum_type
name: p.prepend_mod(name)
info: table.SumType{
variants: sum_variants
}
variants: sum_variants
}
})
return ast.SumTypeDecl{
name: name
@ -1929,8 +1929,8 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
name: p.prepend_mod(name)
parent_idx: pid
info: table.Alias{
foo: ''
}
foo: ''
}
})
return ast.AliasTypeDecl{
name: name