@@ -391,46 +391,47 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
391391 prev_line_nr := prev_node.pos ().last_line
392392 // The nodes are Stmts
393393 if node is ast.Stmt && prev_node is ast.Stmt {
394- stmt := node
395- prev_stmt := prev_node
396- // Force a newline after a block of HashStmts
397- if prev_stmt is ast.HashStmt && stmt ! in [ast.HashStmt, ast.ExprStmt] {
398- return true
399- }
400- // Force a newline after function declarations
401- // The only exception is inside an block of no_body functions
402- if prev_stmt is ast.FnDecl {
403- if stmt ! is ast.FnDecl || ! prev_stmt.no_body {
394+ match prev_node {
395+ // Force a newline after a block of HashStmts
396+ ast.HashStmt {
397+ if node ! in [ast.HashStmt, ast.ExprStmt] {
398+ return true
399+ }
400+ }
401+ // Force a newline after function declarations
402+ // The only exception is inside a block of no_body functions
403+ ast.FnDecl {
404+ if node ! is ast.FnDecl || ! prev_node.no_body {
405+ return true
406+ }
407+ }
408+ // Force a newline after struct declarations
409+ ast.StructDecl {
404410 return true
405411 }
406- }
407- // Force a newline after struct declarations
408- if prev_stmt is ast.StructDecl {
409- return true
410- }
411- // Empty line after an block of type declarations
412- if prev_stmt is ast.TypeDecl && stmt ! is ast.TypeDecl {
413- return true
414- }
415- // Imports are handled special hence they are ignored here
416- if stmt is ast.Import || prev_stmt is ast.Import {
417- return false
418- }
419- // Attributes are not respected in the stmts position, so this requires manual checking
420- if stmt is ast.StructDecl {
421- if stmt.attrs.len > 0 && stmt.attrs[0 ].pos.line_nr - prev_line_nr < = 1 {
422- return false
412+ // Empty line after a block of type declarations
413+ ast.TypeDecl {
414+ if node ! is ast.TypeDecl {
415+ return true
416+ }
423417 }
424- }
425- if stmt is ast.EnumDecl {
426- if stmt.attrs.len > 0 && stmt.attrs[0 ].pos.line_nr - prev_line_nr < = 1 {
418+ // Imports are handled special hence they are ignored here
419+ ast.Import {
427420 return false
428421 }
422+ else {}
429423 }
430- if stmt is ast.FnDecl {
431- if stmt.attrs.len > 0 && stmt.attrs[0 ].pos.line_nr - prev_line_nr < = 1 {
424+ match node {
425+ // Attributes are not respected in the stmts position, so this requires manual checking
426+ ast.StructDecl, ast.EnumDecl, ast.FnDecl {
427+ if node.attrs.len > 0 && node.attrs[0 ].pos.line_nr - prev_line_nr < = 1 {
428+ return false
429+ }
430+ }
431+ ast.Import {
432432 return false
433433 }
434+ else {}
434435 }
435436 }
436437 // The node shouldn't have a newline before
@@ -800,8 +801,8 @@ fn expr_is_single_line(expr ast.Expr) bool {
800801pub fn (mut f Fmt) assert_stmt (node ast.AssertStmt) {
801802 f.write ('assert ' )
802803 mut expr := node.expr
803- for expr is ast.ParExpr {
804- expr = ( expr as ast.ParExpr ) .expr
804+ for mut expr is ast.ParExpr {
805+ expr = expr.expr
805806 }
806807 f.expr (expr)
807808 if node.extra ! is ast.EmptyExpr {
@@ -1288,7 +1289,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
12881289}
12891290
12901291pub fn (mut f Fmt) interface_field (field ast.StructField) {
1291- mut ft := f.no_cur_mod (f.table.type_to_str_using_aliases (field.typ, f.mod2 alias))
1292+ ft := f.no_cur_mod (f.table.type_to_str_using_aliases (field.typ, f.mod2 alias))
12921293 end_pos := field.pos.pos + field.pos.len
12931294 before_comments := field.comments.filter (it .pos.pos < field.pos.pos)
12941295 between_comments := field.comments[before_comments.len..].filter (it .pos.pos < end_pos)
@@ -1823,7 +1824,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
18231824 } else if node.language != .v {
18241825 f.write ('${node.name.after_char(`.`)} ' )
18251826 } else {
1826- mut name := f.short_module (node.name)
1827+ name := f.short_module (node.name)
18271828 f.mark_import_as_used (name)
18281829 f.write ('${name} ' )
18291830 }
@@ -1951,36 +1952,42 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
19511952 f.write ('\$ tmpl(${node.args[0].expr} )' )
19521953 }
19531954 } else {
1954- if node.is_embed {
1955- if node.embed_file.compression_type == 'none' {
1956- f.write ('\$ embed_file(${node.args[0].expr} )' )
1957- } else {
1958- f.write ('\$ embed_file(${node.args[0].expr} , .${node.embed_file.compression_type} )' )
1959- }
1960- } else if node.is_env {
1961- f.write ("\$ env('${node.args_var} ')" )
1962- } else if node.is_pkgconfig {
1963- f.write ("\$ pkgconfig('${node.args_var} ')" )
1964- } else if node.method_name in ['compile_error' , 'compile_warn' ] {
1965- f.write ("\$ ${node.method_name} ('${node.args_var} ')" )
1966- } else {
1967- inner_args := if node.args_var != '' {
1968- node.args_var
1969- } else {
1970- node.args.map (if it .expr is ast.ArrayDecompose {
1971- '...${it.expr.expr.str()} '
1955+ match true {
1956+ node.is_embed {
1957+ if node.embed_file.compression_type == 'none' {
1958+ f.write ('\$ embed_file(${node.args[0].expr} )' )
19721959 } else {
1973- it . str ( )
1974- }). join ( ', ' )
1960+ f. write ( ' \$ embed_file( ${node.args[0].expr} , . ${node.embed_file.compression_type} )' )
1961+ }
19751962 }
1976- method_expr := if node.has_parens {
1977- '(${node.method_name} (${inner_args} ))'
1978- } else {
1979- '${node.method_name} (${inner_args} )'
1963+ node.is_env {
1964+ f.write ("\$ env('${node.args_var} ')" )
1965+ }
1966+ node.is_pkgconfig {
1967+ f.write ("\$ pkgconfig('${node.args_var} ')" )
1968+ }
1969+ node.method_name in ['compile_error' , 'compile_warn' ] {
1970+ f.write ("\$ ${node.method_name} ('${node.args_var} ')" )
1971+ }
1972+ else {
1973+ inner_args := if node.args_var != '' {
1974+ node.args_var
1975+ } else {
1976+ node.args.map (if it .expr is ast.ArrayDecompose {
1977+ '...${it.expr.expr.str()} '
1978+ } else {
1979+ it .str ()
1980+ }).join (', ' )
1981+ }
1982+ method_expr := if node.has_parens {
1983+ '(${node.method_name} (${inner_args} ))'
1984+ } else {
1985+ '${node.method_name} (${inner_args} )'
1986+ }
1987+ f.expr (node.left)
1988+ f.write ('.$${method_expr} ' )
1989+ f.or_expr (node.or_block)
19801990 }
1981- f.expr (node.left)
1982- f.write ('.$${method_expr} ' )
1983- f.or_expr (node.or_block)
19841991 }
19851992 }
19861993}
@@ -2628,14 +2635,12 @@ pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {
26282635 if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is]
26292636 && node.right.expr.right ! is ast.InfixExpr {
26302637 f.expr (node.right.expr.left)
2631- if node.right.expr.op == .key_in {
2632- f.write (' !in ' )
2633- } else if node.right.expr.op == .not_in {
2634- f.write (' in ' )
2635- } else if node.right.expr.op == .key_is {
2636- f.write (' !is ' )
2637- } else if node.right.expr.op == .not_is {
2638- f.write (' is ' )
2638+ match node.right.expr.op {
2639+ .key_in { f.write (' !in ' ) }
2640+ .not_in { f.write (' in ' ) }
2641+ .key_is { f.write (' !is ' ) }
2642+ .not_is { f.write (' is ' ) }
2643+ else {}
26392644 }
26402645 f.expr (node.right.expr.right)
26412646 return
0 commit comments