From 7b0dae5da730c9d82ffb79468357bc69132bfaa0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 25 May 2022 01:14:08 +0800 Subject: [PATCH] fmt: fix file with just imports (fix #14267) (#14513) --- vlib/v/fmt/fmt.v | 13 ++++++++++--- vlib/v/fmt/tests/file_with_just_imports_keep.vv | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 753f7331a4..f10e93b8f7 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) { diff --git a/vlib/v/fmt/tests/file_with_just_imports_keep.vv b/vlib/v/fmt/tests/file_with_just_imports_keep.vv index 122510a32a..acea4f317f 100644 --- a/vlib/v/fmt/tests/file_with_just_imports_keep.vv +++ b/vlib/v/fmt/tests/file_with_just_imports_keep.vv @@ -1,3 +1,3 @@ module proto -import emily33901.vproto +import emily33901.vproto