fmt fmt.v, vfmt.v

pull/4447/head
Kris Cherven 2020-04-16 09:54:17 -04:00 committed by GitHub
parent 54226e74cf
commit 182108faca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 57 deletions

View File

@ -3,22 +3,20 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module main module main
import ( import os
os import os.cmdline
os.cmdline import v.ast
v.ast import v.pref
v.pref import v.fmt
v.fmt import v.util
v.util import v.parser
v.parser import v.table
v.table import vhelp
vhelp
)
struct FormatOptions { struct FormatOptions {
is_l bool is_l bool
is_c bool is_c bool
is_js bool is_js bool
is_w bool is_w bool
is_diff bool is_diff bool
is_verbose bool is_verbose bool
@ -29,24 +27,18 @@ struct FormatOptions {
} }
const ( const (
platform_and_file_extensions = [['windows', '_win.v', '_windows.v'], platform_and_file_extensions = [['windows', '_win.v', '_windows.v'], ['linux', '_lin.v',
['linux', '_lin.v', '_linux.v', '_nix.v'], '_linux.v', '_nix.v'], ['macos', '_mac.v', '_darwin.v'], ['freebsd', '_bsd.v', '_freebsd.v'],
['macos', '_mac.v', '_darwin.v'], ['netbsd', '_bsd.v', '_netbsd.v'], ['openbsd', '_bsd.v', '_openbsd.v'], ['solaris', '_solaris.v'],
['freebsd', '_bsd.v', '_freebsd.v'], ['haiku', '_haiku.v'], ['qnx', '_qnx.v']]
['netbsd', '_bsd.v', '_netbsd.v'], FORMATTED_FILE_TOKEN = '\@\@\@' + 'FORMATTED_FILE: '
['openbsd', '_bsd.v', '_openbsd.v'],
['solaris', '_solaris.v'],
['haiku', '_haiku.v'],
['qnx', '_qnx.v'],
]
FORMATTED_FILE_TOKEN = '\@\@\@' + 'FORMATTED_FILE: '
) )
fn main() { fn main() {
//if os.getenv('VFMT_ENABLE') == '' { // if os.getenv('VFMT_ENABLE') == '' {
//eprintln('v fmt is disabled for now') // eprintln('v fmt is disabled for now')
//exit(1) // exit(1)
//} // }
toolexe := os.executable() toolexe := os.executable()
util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe)))) util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe))))
args := util.join_env_vflags_and_os_args() 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 env_vflags_and_os_args: ' + args.str())
eprintln('vfmt possible_files: ' + possible_files.str()) eprintln('vfmt possible_files: ' + possible_files.str())
} }
mut files := []string var files := []string
for file in possible_files { for file in possible_files {
if !file.ends_with('.v') && !file.ends_with('.vv') { if !file.ends_with('.v') && !file.ends_with('.vv') {
verror('v fmt can only be used on .v files.\nOffending file: "$file"') verror('v fmt can only be used on .v files.\nOffending file: "$file"')
@ -95,19 +87,19 @@ fn main() {
files << file files << file
} }
if files.len == 0 { if files.len == 0 {
vhelp.show_topic('fmt') vhelp.show_topic('fmt')
exit(0) exit(0)
} }
mut cli_args_no_files := []string var cli_args_no_files := []string
for a in os.args { for a in os.args {
if !(a in files) { if !(a in files) {
cli_args_no_files << a cli_args_no_files << a
} }
} }
mut errors := 0 var errors := 0
for file in files { for file in files {
fpath := os.real_path(file) 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_command_array << ['-worker', fpath]
worker_cmd := worker_command_array.join(' ') worker_cmd := worker_command_array.join(' ')
if foptions.is_verbose { if foptions.is_verbose {
@ -154,22 +146,24 @@ fn (foptions &FormatOptions) format_file(file string) {
eprintln('vfmt2 running fmt.fmt over file: $file') eprintln('vfmt2 running fmt.fmt over file: $file')
} }
table := table.new_table() table := table.new_table()
//checker := checker.new_checker(table, prefs) // checker := checker.new_checker(table, prefs)
file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{parent: 0}) file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{
//checker.check(file_ast) parent: 0
})
// checker.check(file_ast)
formatted_content := fmt.fmt(file_ast, table) formatted_content := fmt.fmt(file_ast, table)
file_name := os.file_name(file) file_name := os.file_name(file)
vfmt_output_path := os.join_path(os.temp_dir(), 'vfmt_' + file_name) 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 { if foptions.is_verbose {
eprintln('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .') eprintln('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .')
} }
eprintln('${FORMATTED_FILE_TOKEN}${vfmt_output_path}') eprintln('${FORMATTED_FILE_TOKEN}${vfmt_output_path}')
} }
fn print_compiler_options( compiler_params &pref.Preferences ) { fn print_compiler_options(compiler_params &pref.Preferences) {
eprintln(' os: ' + compiler_params.os.str() ) eprintln(' os: ' + compiler_params.os.str())
eprintln(' ccompiler: $compiler_params.ccompiler' ) eprintln(' ccompiler: $compiler_params.ccompiler')
eprintln(' mod: $compiler_params.mod ') eprintln(' mod: $compiler_params.mod ')
eprintln(' path: $compiler_params.path ') eprintln(' path: $compiler_params.path ')
eprintln(' out_name: $compiler_params.out_name ') 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 ') 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 { if formatted_file_path.len == 0 {
return return
} }
@ -221,8 +215,7 @@ fn (foptions &FormatOptions) post_process_file(file string, formatted_file_path
panic(err) panic(err)
} }
eprintln('Reformatted file: $file') eprintln('Reformatted file: $file')
} } else {
else {
eprintln('Already formatted file: $file') eprintln('Already formatted file: $file')
} }
return return
@ -243,7 +236,8 @@ fn find_working_diff_command() ?string {
} }
pub fn (f FormatOptions) str() 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 { fn file_to_target_os(file string) string {
@ -257,11 +251,11 @@ fn file_to_target_os(file string) string {
return '' return ''
} }
fn file_to_mod_name_and_is_module_file(file string) (string,bool) { fn file_to_mod_name_and_is_module_file(file string) (string, bool) {
mut mod_name := 'main' var mod_name := 'main'
mut is_module_file := false var is_module_file := false
flines := read_source_lines(file) or { flines := read_source_lines(file) or {
return mod_name,is_module_file return mod_name, is_module_file
} }
for fline in flines { for fline in flines {
line := fline.trim_space() line := fline.trim_space()
@ -273,7 +267,7 @@ fn file_to_mod_name_and_is_module_file(file string) (string,bool) {
break break
} }
} }
return mod_name,is_module_file return mod_name, is_module_file
} }
fn read_source_lines(file string) ?[]string { 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 { all_files_in_pfolder := os.ls(pfolder) or {
panic(err) panic(err)
} }
mut vfiles := []string var vfiles := []string
for f in all_files_in_pfolder { for f in all_files_in_pfolder {
vf := os.join_path(pfolder, f) vf := os.join_path(pfolder, f)
if f.starts_with('.') || !f.ends_with('.v') || os.is_dir(vf) { 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 // containing `fn main` then the folder contains multiple standalone
// v programs. If only one contains `fn main` then the folder is // v programs. If only one contains `fn main` then the folder is
// a project folder, that should be compiled with `v pfolder`. // a project folder, that should be compiled with `v pfolder`.
mut main_fns := 0 var main_fns := 0
for f in vfiles { for f in vfiles {
slines := read_source_lines(f) or { slines := read_source_lines(f) or {
panic(err) panic(err)
@ -327,6 +321,6 @@ fn get_compile_name_of_potential_v_project(file string) string {
return pfolder return pfolder
} }
fn verror(s string){ fn verror(s string) {
util.verror('vfmt error', s) util.verror('vfmt error', s)
} }

View File

@ -31,7 +31,7 @@ mut:
} }
pub fn fmt(file ast.File, table &table.Table) string { pub fn fmt(file ast.File, table &table.Table) string {
mut f := Fmt{ var f := Fmt{
out: strings.new_builder(1000) out: strings.new_builder(1000)
out_imports: strings.new_builder(200) out_imports: strings.new_builder(200)
table: table table: table
@ -205,7 +205,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.write('pub ') f.write('pub ')
} }
f.writeln('const (') f.writeln('const (')
mut max := 0 var max := 0
for field in it.fields { for field in it.fields {
if field.name.len > max { if field.name.len > max {
max = field.name.len max = field.name.len
@ -393,7 +393,7 @@ fn (f mut Fmt) type_decl(node ast.TypeDecl) {
f.write('pub ') f.write('pub ')
} }
f.write('type $it.name = ') f.write('type $it.name = ')
mut sum_type_names := []string var sum_type_names := []string
for t in it.sub_types { for t in it.sub_types {
sum_type_names << f.type_to_str(t) 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('.') name := node.name.after('.')
f.writeln('struct $name {') f.writeln('struct $name {')
mut max := 0 var max := 0
for field in node.fields { for field in node.fields {
if field.name.len > max { if field.name.len > max {
max = field.name.len max = field.name.len
@ -691,7 +691,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.StructInit { ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ) type_sym := f.table.get_type_symbol(it.typ)
// f.write('<old name: $type_sym.name>') // f.write('<old name: $type_sym.name>')
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' { if name == 'void' {
name = '' name = ''
} }
@ -773,7 +773,7 @@ fn (f mut Fmt) or_expr(or_block ast.OrExpr) {
fn (f mut Fmt) comment(node ast.Comment) { fn (f mut Fmt) comment(node ast.Comment) {
if !node.text.contains('\n') { if !node.text.contains('\n') {
is_separate_line := node.text.starts_with('|') 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 == '' { if s == '' {
s = '//' s = '//'
} else { } else {