fmt: initial support for -debug
parent
23df9b052e
commit
5eb71c4a13
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.'))
|
||||
|
|
|
@ -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.'))
|
||||
|
|
Loading…
Reference in New Issue