Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 28, 2024
1 parent 03da4e8 commit 7d20cf9
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ pub mut:
wsinfix_depth int
format_state FormatState
source_text string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file.
inside_vmodules bool
}

@[params]
Expand All @@ -71,12 +70,6 @@ pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug
out: strings.new_builder(1000)
out_imports: strings.new_builder(200)
}
for p in os.vmodules_paths() {
if file.path.starts_with(os.real_path(p)) {
f.inside_vmodules = true
break
}
}

f.source_text = options.source_text
f.process_file_imports(file)
Expand Down Expand Up @@ -380,30 +373,27 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
}

pub fn (f Fmt) imp_stmt_str(imp ast.Import) string {
normalized_mod := if f.inside_vmodules {
imp.source_name
} else if imp.mod.len == 0 {
imp.alias
} else {
mod_last := imp.mod.all_after_last('.')
if imp.source_name == mod_last {
mod_last
} else {
imp.mod
mut imp_res := imp.source_name
if imp.mod.starts_with('src.') || (imp.mod.contains('src.') && imp.mod != imp.source_name) {
imp_after_src := imp.mod.all_after('src.')
if imp.source_name.all_after('src.') == imp_after_src {
imp_res = imp_after_src
}
}
is_diff := imp.alias != normalized_mod && !normalized_mod.ends_with('.' + imp.alias)
mut imp_alias_suffix := if is_diff { ' as ${imp.alias}' } else { '' }
// Format / remove unused selective import symbols
// E.g.: `import foo { Foo }` || `import foo as f { Foo }`
has_alias := imp.alias != imp.source_name.all_after_last('.')
mut suffix := if has_alias { ' as ${imp.alias}' } else { '' }
mut syms := imp.syms.map(it.name).filter(f.import_syms_used[it])
syms.sort()
if syms.len > 0 {
imp_alias_suffix += if imp.syms[0].pos.line_nr == imp.pos.line_nr {
suffix += if imp.syms[0].pos.line_nr == imp.pos.line_nr {
' { ' + syms.join(', ') + ' }'
} else {
' {\n\t' + syms.join(',\n\t') + ',\n}'
}
}
return '${normalized_mod}${imp_alias_suffix}'
return '${imp_res}${suffix}'
}

//=== Node helpers ===//
Expand Down

0 comments on commit 7d20cf9

Please sign in to comment.