fmt: fix file with just imports (fix #14267) (#14513)

yuyi 2022-05-25 01:14:08 +08:00 committed by Jef Roosens
parent 95429e5cc8
commit 7b0dae5da7
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 11 additions and 4 deletions

View File

@ -7,7 +7,6 @@ import strings
import v.ast
import v.util
import v.pref
import v.mathutil
const (
bs = '\\'
@ -72,8 +71,16 @@ pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug boo
if res.len == 1 {
return f.out_imports.str().trim_space() + '\n'
}
bounded_import_pos := mathutil.min(res.len, f.import_pos)
return res[..bounded_import_pos] + f.out_imports.str() + res[bounded_import_pos..]
if res.len <= f.import_pos {
imp_str := f.out_imports.str().trim_space()
if imp_str.len > 0 {
return res + '\n' + imp_str + '\n'
} else {
return res
}
} else {
return res[..f.import_pos] + f.out_imports.str() + res[f.import_pos..]
}
}
pub fn (mut f Fmt) process_file_imports(file &ast.File) {

View File

@ -1,3 +1,3 @@
module proto
import emily33901.vproto
import emily33901.vproto