diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index ea68a86da3..296f4de1b0 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -152,7 +152,7 @@ fn (foptions &FormatOptions) format_file(file string) { parent: 0 }) // checker.check(file_ast) - formatted_content := fmt.fmt(file_ast, table) + formatted_content := fmt.fmt(file_ast, table, foptions.is_debug) 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) diff --git a/cmd/v/help/fmt.txt b/cmd/v/help/fmt.txt index 6a0b72b02d..a07cf4ff08 100644 --- a/cmd/v/help/fmt.txt +++ b/cmd/v/help/fmt.txt @@ -9,7 +9,9 @@ Options: -diff Display only diffs between the formatted source and the original source. -l List files whose formatting differs from vfmt. -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 careful, and make frequent BACKUPS, when running with -vfmt2 . + -debug Print the kinds of encountered AST statements/expressions on stderr. + +NB: vfmt after 2020/04/01 is based on the new AST compiler code, and +thus is much faster, and more flexible than before. +It may also EAT some of your code for now, so please be careful, and +make frequent BACKUPS. diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 2a2adb9a3c..da46ab86ed 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -28,15 +28,17 @@ mut: auto_imports []string // automatically inserted imports that the user forgot to specify import_pos int // position of the imports in the resulting string for later autoimports insertion used_imports []string // to remove unused imports + is_debug bool } -pub fn fmt(file ast.File, table &table.Table) string { +pub fn fmt(file ast.File, table &table.Table, is_debug bool) string { mut f := Fmt{ out: strings.new_builder(1000) out_imports: strings.new_builder(200) table: table indent: 0 file: file + is_debug: is_debug } f.cur_mod = 'main' for stmt in file.stmts { @@ -141,6 +143,9 @@ fn (mut f Fmt) stmts(stmts []ast.Stmt) { } fn (mut f Fmt) stmt(node ast.Stmt) { + if f.is_debug { + eprintln('stmt: ${node.position():-42} | node: ${typeof(node):-20}') + } match node { ast.AssignStmt { for i, ident in it.left { @@ -492,6 +497,9 @@ fn (f &Fmt) type_to_str(t table.Type) string { } fn (mut f Fmt) expr(node ast.Expr) { + if f.is_debug { + eprintln('expr: ${node.position():-42} | node: ${typeof(node):-20} | ${node.str()}') + } match node { ast.AnonFn { f.fn_decl(it.decl) diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index 2ae0b3d445..16eb073a03 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -45,7 +45,7 @@ fn test_fmt() { file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{ parent: 0 }) - result_ocontent := fmt.fmt(file_ast, table) + result_ocontent := fmt.fmt(file_ast, table, false) if expected_ocontent != result_ocontent { fmt_bench.fail() eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) diff --git a/vlib/v/fmt/fmt_test.v b/vlib/v/fmt/fmt_test.v index c34c1f4fa9..3ccbcad3a8 100644 --- a/vlib/v/fmt/fmt_test.v +++ b/vlib/v/fmt/fmt_test.v @@ -47,7 +47,7 @@ fn test_fmt() { file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{ parent: 0 }) - result_ocontent := fmt.fmt(file_ast, table) + result_ocontent := fmt.fmt(file_ast, table, false) if expected_ocontent != result_ocontent { fmt_bench.fail() eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))