vfmt2: allow running the new vfmt with 'v fmt -2'

pull/3774/head
Delyan Angelov 2020-02-18 21:00:06 +02:00 committed by GitHub
parent b991ca4ebc
commit 290feaac51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 15 deletions

View File

@ -9,9 +9,13 @@ import (
filepath filepath
compiler compiler
v.pref v.pref
v.fmt
v.parser
v.table
) )
struct FormatOptions { struct FormatOptions {
is_2 bool
is_l bool is_l bool
is_c bool is_c bool
is_w bool is_w bool
@ -39,6 +43,7 @@ fn main() {
compiler.set_vroot_folder(filepath.dir(filepath.dir(filepath.dir(toolexe)))) compiler.set_vroot_folder(filepath.dir(filepath.dir(filepath.dir(toolexe))))
args := join_flags_and_argument() args := join_flags_and_argument()
foptions := FormatOptions{ foptions := FormatOptions{
is_2: '-2' in args
is_c: '-c' in args is_c: '-c' in args
is_l: '-l' in args is_l: '-l' in args
is_w: '-w' in args is_w: '-w' in args
@ -71,10 +76,17 @@ fn main() {
} }
mut files := []string mut files := []string
for file in possible_files { for file in possible_files {
if foptions.is_2 {
if !file.ends_with('.v') && !file.ends_with('.vv') {
compiler.verror('v fmt -2 can only be used on .v or .vv files.\nOffending file: "$file" .')
continue
}
} else {
if !file.ends_with('.v') { if !file.ends_with('.v') {
compiler.verror('v fmt can only be used on .v files.\nOffending file: "$file" .') compiler.verror('v fmt can only be used on .v files.\nOffending file: "$file" .')
continue continue
} }
}
if !os.exists(file) { if !os.exists(file) {
compiler.verror('"$file" does not exist.') compiler.verror('"$file" does not exist.')
continue continue
@ -136,6 +148,22 @@ fn main() {
} }
fn (foptions &FormatOptions) format_file(file string) { fn (foptions &FormatOptions) format_file(file string) {
if foptions.is_2 {
if foptions.is_verbose {
eprintln('vfmt2 running fmt.fmt over file: $file')
}
table := table.new_table()
file_ast := parser.parse_file(file, table)
formatted_content := fmt.fmt(file_ast, table)
file_name := filepath.filename(file)
vfmt_output_path := filepath.join(os.tmpdir(), 'vfmt_' + file_name)
os.write_file(vfmt_output_path, formatted_content )
if foptions.is_verbose {
eprintln('vfmt2 fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .')
}
eprintln('${FORMATTED_FILE_TOKEN}${vfmt_output_path}')
return
}
tmpfolder := os.tmpdir() tmpfolder := os.tmpdir()
mut compiler_params := &pref.Preferences{} mut compiler_params := &pref.Preferences{}
target_os := file_to_target_os(file) target_os := file_to_target_os(file)
@ -274,6 +302,10 @@ Options:
-diff display only diffs between the formatted source and the original source. -diff display only diffs between the formatted source and the original source.
-l list files whose formatting differs from vfmt. -l list files whose formatting differs from vfmt.
-w write result to (source) file(s) instead of to stdout. -w write result to (source) file(s) instead of to stdout.
-2 Use the new V parser/vfmt. NB: this is EXPERIMENTAL for now.
The new vfmt is much faster and more forgiving.
It also may EAT some of your code for now.
Please be carefull, and make frequent BACKUPS, when running with -vfmt2 .
') ')
} }
@ -305,7 +337,7 @@ fn (foptions &FormatOptions) compile_file(file string, compiler_params &pref.Pre
} }
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_2: $f.is_2' + ' 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 {

View File

@ -10,8 +10,7 @@ import (
const ( const (
error_missing_vexe = 1 error_missing_vexe = 1
error_missing_diff = 2 error_failed_tests = 2
error_failed_tests = 3
) )
fn test_fmt() { fn test_fmt() {