diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index 731ca01cbd..d9b75563ce 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -3,22 +3,20 @@ // that can be found in the LICENSE file. module main -import ( - os - os.cmdline - v.ast - v.pref - v.fmt - v.util - v.parser - v.table - vhelp -) +import os +import os.cmdline +import v.ast +import v.pref +import v.fmt +import v.util +import v.parser +import v.table +import vhelp struct FormatOptions { is_l bool is_c bool - is_js bool + is_js bool is_w bool is_diff bool is_verbose bool @@ -29,24 +27,18 @@ struct FormatOptions { } const ( - platform_and_file_extensions = [['windows', '_win.v', '_windows.v'], - ['linux', '_lin.v', '_linux.v', '_nix.v'], - ['macos', '_mac.v', '_darwin.v'], - ['freebsd', '_bsd.v', '_freebsd.v'], - ['netbsd', '_bsd.v', '_netbsd.v'], - ['openbsd', '_bsd.v', '_openbsd.v'], - ['solaris', '_solaris.v'], - ['haiku', '_haiku.v'], - ['qnx', '_qnx.v'], - ] - FORMATTED_FILE_TOKEN = '\@\@\@' + 'FORMATTED_FILE: ' + platform_and_file_extensions = [['windows', '_win.v', '_windows.v'], ['linux', '_lin.v', + '_linux.v', '_nix.v'], ['macos', '_mac.v', '_darwin.v'], ['freebsd', '_bsd.v', '_freebsd.v'], + ['netbsd', '_bsd.v', '_netbsd.v'], ['openbsd', '_bsd.v', '_openbsd.v'], ['solaris', '_solaris.v'], + ['haiku', '_haiku.v'], ['qnx', '_qnx.v']] + FORMATTED_FILE_TOKEN = '\@\@\@' + 'FORMATTED_FILE: ' ) fn main() { - //if os.getenv('VFMT_ENABLE') == '' { - //eprintln('v fmt is disabled for now') - //exit(1) - //} + // if os.getenv('VFMT_ENABLE') == '' { + // eprintln('v fmt is disabled for now') + // exit(1) + // } toolexe := os.executable() util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe)))) args := util.join_env_vflags_and_os_args() @@ -82,7 +74,7 @@ fn main() { eprintln('vfmt env_vflags_and_os_args: ' + args.str()) eprintln('vfmt possible_files: ' + possible_files.str()) } - mut files := []string + var files := []string for file in possible_files { if !file.ends_with('.v') && !file.ends_with('.vv') { verror('v fmt can only be used on .v files.\nOffending file: "$file"') @@ -95,19 +87,19 @@ fn main() { files << file } if files.len == 0 { - vhelp.show_topic('fmt') + vhelp.show_topic('fmt') exit(0) } - mut cli_args_no_files := []string + var cli_args_no_files := []string for a in os.args { if !(a in files) { cli_args_no_files << a } } - mut errors := 0 + var errors := 0 for file in files { fpath := os.real_path(file) - mut worker_command_array := cli_args_no_files.clone() + var worker_command_array := cli_args_no_files.clone() worker_command_array << ['-worker', fpath] worker_cmd := worker_command_array.join(' ') if foptions.is_verbose { @@ -154,22 +146,24 @@ fn (foptions &FormatOptions) format_file(file string) { eprintln('vfmt2 running fmt.fmt over file: $file') } table := table.new_table() - //checker := checker.new_checker(table, prefs) - file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{parent: 0}) - //checker.check(file_ast) + // checker := checker.new_checker(table, prefs) + file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{ + parent: 0 + }) + // checker.check(file_ast) formatted_content := fmt.fmt(file_ast, table) file_name := os.file_name(file) vfmt_output_path := os.join_path(os.temp_dir(), 'vfmt_' + file_name) - os.write_file(vfmt_output_path, formatted_content ) + os.write_file(vfmt_output_path, formatted_content) if foptions.is_verbose { eprintln('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .') } eprintln('${FORMATTED_FILE_TOKEN}${vfmt_output_path}') } -fn print_compiler_options( compiler_params &pref.Preferences ) { - eprintln(' os: ' + compiler_params.os.str() ) - eprintln(' ccompiler: $compiler_params.ccompiler' ) +fn print_compiler_options(compiler_params &pref.Preferences) { + eprintln(' os: ' + compiler_params.os.str()) + eprintln(' ccompiler: $compiler_params.ccompiler') eprintln(' mod: $compiler_params.mod ') eprintln(' path: $compiler_params.path ') eprintln(' out_name: $compiler_params.out_name ') @@ -181,7 +175,7 @@ fn print_compiler_options( compiler_params &pref.Preferences ) { eprintln(' is_script: $compiler_params.is_script ') } -fn (foptions &FormatOptions) post_process_file(file string, formatted_file_path string) { +fn (foptions &FormatOptions) post_process_file(file, formatted_file_path string) { if formatted_file_path.len == 0 { return } @@ -221,8 +215,7 @@ fn (foptions &FormatOptions) post_process_file(file string, formatted_file_path panic(err) } eprintln('Reformatted file: $file') - } - else { + } else { eprintln('Already formatted file: $file') } return @@ -243,7 +236,8 @@ fn find_working_diff_command() ?string { } pub 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' + ' }' + 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' + ' }' } fn file_to_target_os(file string) string { @@ -257,11 +251,11 @@ fn file_to_target_os(file string) string { return '' } -fn file_to_mod_name_and_is_module_file(file string) (string,bool) { - mut mod_name := 'main' - mut is_module_file := false +fn file_to_mod_name_and_is_module_file(file string) (string, bool) { + var mod_name := 'main' + var is_module_file := false flines := read_source_lines(file) or { - return mod_name,is_module_file + return mod_name, is_module_file } for fline in flines { line := fline.trim_space() @@ -273,7 +267,7 @@ fn file_to_mod_name_and_is_module_file(file string) (string,bool) { break } } - return mod_name,is_module_file + return mod_name, is_module_file } fn read_source_lines(file string) ?[]string { @@ -293,7 +287,7 @@ fn get_compile_name_of_potential_v_project(file string) string { all_files_in_pfolder := os.ls(pfolder) or { panic(err) } - mut vfiles := []string + var 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) { @@ -310,7 +304,7 @@ fn get_compile_name_of_potential_v_project(file string) string { // 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 + var main_fns := 0 for f in vfiles { slines := read_source_lines(f) or { panic(err) @@ -327,6 +321,6 @@ fn get_compile_name_of_potential_v_project(file string) string { return pfolder } -fn verror(s string){ +fn verror(s string) { util.verror('vfmt error', s) } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 37e9d75638..0b3d27364b 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -31,7 +31,7 @@ mut: } pub fn fmt(file ast.File, table &table.Table) string { - mut f := Fmt{ + var f := Fmt{ out: strings.new_builder(1000) out_imports: strings.new_builder(200) table: table @@ -205,7 +205,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) { f.write('pub ') } f.writeln('const (') - mut max := 0 + var max := 0 for field in it.fields { if field.name.len > max { max = field.name.len @@ -393,7 +393,7 @@ fn (f mut Fmt) type_decl(node ast.TypeDecl) { f.write('pub ') } f.write('type $it.name = ') - mut sum_type_names := []string + var sum_type_names := []string for t in it.sub_types { sum_type_names << f.type_to_str(t) } @@ -412,7 +412,7 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) { } name := node.name.after('.') f.writeln('struct $name {') - mut max := 0 + var max := 0 for field in node.fields { if field.name.len > max { max = field.name.len @@ -691,7 +691,7 @@ fn (f mut Fmt) expr(node ast.Expr) { ast.StructInit { type_sym := f.table.get_type_symbol(it.typ) // f.write('') - mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? + var name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? if name == 'void' { name = '' } @@ -773,7 +773,7 @@ fn (f mut Fmt) or_expr(or_block ast.OrExpr) { fn (f mut Fmt) comment(node ast.Comment) { if !node.text.contains('\n') { is_separate_line := node.text.starts_with('|') - mut s := if is_separate_line { node.text[1..] } else { node.text } + var s := if is_separate_line { node.text[1..] } else { node.text } if s == '' { s = '//' } else {