Skip to content

Commit

Permalink
tools: cleanup obsolete functions in cmd/tools/vfmt.v
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Apr 2, 2024
1 parent 015007c commit a1d4712
Showing 1 changed file with 7 additions and 64 deletions.
71 changes: 7 additions & 64 deletions cmd/tools/vfmt.v
Expand Up @@ -283,76 +283,19 @@ fn (mut foptions FormatOptions) post_process_file(file string, formatted_file_pa
flush_stdout()
}

fn (f FormatOptions) str() string {
return
'FormatOptions{ is_l: ${f.is_l}, is_w: ${f.is_w}, is_diff: ${f.is_diff}, is_verbose: ${f.is_verbose},' +
' is_all: ${f.is_all}, is_worker: ${f.is_worker}, is_debug: ${f.is_debug}, is_noerror: ${f.is_noerror},' +
' is_verify: ${f.is_verify}" }'
}

fn file_to_mod_name_and_is_module_file(file string) (string, bool) {
mut mod_name := 'main'
mut is_module_file := false
flines := read_source_lines(file) or { return mod_name, is_module_file }
for fline in flines {
line := fline.trim_space()
if line.starts_with('module ') {
if !line.starts_with('module main') {
is_module_file = true
mod_name = line.replace('module ', ' ').trim_space()
}
break
}
}
return mod_name, is_module_file
}

fn read_source_lines(file string) ![]string {
source_lines := os.read_lines(file) or { return error('can not read ${file}') }
return source_lines
}

fn get_compile_name_of_potential_v_project(file string) string {
// This function get_compile_name_of_potential_v_project returns:
// a) the file's folder, if file is part of a v project
// b) the file itself, if the file is a standalone v program
pfolder := os.real_path(os.dir(file))
// a .v project has many 'module main' files in one folder
// if there is only one .v file, then it must be a standalone
all_files_in_pfolder := os.ls(pfolder) or { panic(err) }
mut vfiles := []string{}
for f in all_files_in_pfolder {
vf := os.join_path(pfolder, f)
if f.starts_with('.') || !f.ends_with('.v') || os.is_dir(vf) {
continue
}
vfiles << vf
}
if vfiles.len == 1 {
return file
}
// /////////////////////////////////////////////////////////////
// At this point, we know there are many .v files in the folder
// We will have to read them all, and if there are more than one
// containing `fn main` then the folder contains multiple standalone
// v programs. If only one contains `fn main` then the folder is
// a project folder, that should be compiled with `v pfolder`.
mut main_fns := 0
for f in vfiles {
slines := read_source_lines(f) or { panic(err) }
for line in slines {
if line.contains('fn main()') {
main_fns++
if main_fns > 1 {
return file
}
}
}
}
return pfolder
}

@[noreturn]
fn verror(s string) {
util.verror('vfmt error', s)
}

fn (f FormatOptions) str() string {
return
'FormatOptions{ is_l: ${f.is_l}, is_w: ${f.is_w}, is_diff: ${f.is_diff}, is_verbose: ${f.is_verbose},' +
' is_all: ${f.is_all}, is_worker: ${f.is_worker}, is_debug: ${f.is_debug}, is_noerror: ${f.is_noerror},' +
' is_verify: ${f.is_verify}" }'
}

0 comments on commit a1d4712

Please sign in to comment.