vfmt: fix compilation; handle the case when fmt.tabs is not enough

pull/4639/head
Delyan Angelov 2020-04-28 16:04:37 +03:00
parent eb9448e59e
commit 8ea087f957
2 changed files with 15 additions and 8 deletions

View File

@ -141,7 +141,7 @@ fn main() {
} }
fn (foptions &FormatOptions) format_file(file string) { fn (foptions &FormatOptions) format_file(file string) {
prefs := pref.new_preferences() mut prefs := pref.new_preferences()
prefs.is_fmt = util.is_fmt() prefs.is_fmt = util.is_fmt()
if foptions.is_verbose { if foptions.is_verbose {
eprintln('vfmt2 running fmt.fmt over file: $file') eprintln('vfmt2 running fmt.fmt over file: $file')

View File

@ -39,7 +39,7 @@ pub fn fmt(file ast.File, table &table.Table) string {
file: file file: file
} }
f.cur_mod = 'main' f.cur_mod = 'main'
for i, stmt in file.stmts { for stmt in file.stmts {
// TODO `if stmt is ast.Import` // TODO `if stmt is ast.Import`
match stmt { match stmt {
ast.Import { ast.Import {
@ -69,7 +69,14 @@ fn (f mut Fmt) find_comment(line_nr int) {
*/ */
pub fn (mut f Fmt) write(s string) { pub fn (mut f Fmt) write(s string) {
if f.indent > 0 && f.empty_line { if f.indent > 0 && f.empty_line {
if f.indent < tabs.len {
f.out.write(tabs[f.indent]) f.out.write(tabs[f.indent])
} else {
// too many indents, do it the slow way:
for i in 0 .. f.indent {
f.out.write('\t')
}
}
f.line_len += f.indent * 4 f.line_len += f.indent * 4
} }
f.out.write(s) f.out.write(s)
@ -208,7 +215,7 @@ fn (mut f Fmt) stmt(node ast.Stmt) {
} }
} }
f.indent++ f.indent++
for i, field in it.fields { for field in it.fields {
name := field.name.after('.') name := field.name.after('.')
f.write('$name ') f.write('$name ')
f.write(strings.repeat(` `, max - field.name.len)) f.write(strings.repeat(` `, max - field.name.len))
@ -694,7 +701,7 @@ fn (mut f Fmt) expr(node ast.Expr) {
} else { } else {
f.writeln('$name{') f.writeln('$name{')
f.indent++ f.indent++
for i, field in it.fields { for field in it.fields {
f.write('$field.name: ') f.write('$field.name: ')
f.expr(field.expr) f.expr(field.expr)
f.writeln('') f.writeln('')
@ -886,7 +893,7 @@ fn (mut f Fmt) match_expr(it ast.MatchExpr) {
f.writeln(' {') f.writeln(' {')
f.indent++ f.indent++
mut single_line := true mut single_line := true
for i, branch in it.branches { for branch in it.branches {
if branch.stmts.len > 1 { if branch.stmts.len > 1 {
single_line = false single_line = false
break break
@ -907,7 +914,7 @@ fn (mut f Fmt) match_expr(it ast.MatchExpr) {
break break
} }
} }
for i, branch in it.branches { for branch in it.branches {
if branch.comment.text != '' { if branch.comment.text != '' {
f.comment(branch.comment) f.comment(branch.comment)
} }