Skip to content

Commit

Permalink
fmt: insert auto imports after shebang (#21038)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 16, 2024
1 parent 657e46e commit 9d66b34
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions vlib/v/fmt/fmt.v
Expand Up @@ -97,9 +97,14 @@ pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug
}
return res
}
source_for_imports := res[..f.import_pos] + f.out_imports.str()
source_after_imports := res[f.import_pos..]
return source_for_imports + source_after_imports
mut import_start_pos := f.import_pos
if stmt := file.stmts[1] {
if stmt is ast.ExprStmt && stmt.expr is ast.Comment
&& (stmt.expr as ast.Comment).text.starts_with('#!') {
import_start_pos = stmt.pos.len
}
}
return res[..import_start_pos] + f.out_imports.str() + res[import_start_pos..]
}

pub fn (mut f Fmt) process_file_imports(file &ast.File) {
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/fmt/tests/import_auto_with_shebang_expected.vv
@@ -0,0 +1,5 @@
#!/usr/bin/env -S v

import os

os.join_path('a', 'b', 'c')
3 changes: 3 additions & 0 deletions vlib/v/fmt/tests/import_auto_with_shebang_input.vv
@@ -0,0 +1,3 @@
#!/usr/bin/env -S v
os.join_path('a','b','c')

0 comments on commit 9d66b34

Please sign in to comment.