mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fmt: single line if
This commit is contained in:
parent
15a2927f09
commit
d075be73e8
@ -16,12 +16,13 @@ const (
|
||||
)
|
||||
|
||||
struct Fmt {
|
||||
out strings.Builder
|
||||
table &table.Table
|
||||
out strings.Builder
|
||||
table &table.Table
|
||||
mut:
|
||||
indent int
|
||||
empty_line bool
|
||||
line_len int
|
||||
indent int
|
||||
empty_line bool
|
||||
line_len int
|
||||
single_line_if bool
|
||||
}
|
||||
|
||||
pub fn fmt(file ast.File, table &table.Table) string {
|
||||
@ -130,7 +131,9 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||
}
|
||||
ast.ExprStmt {
|
||||
f.expr(it.expr)
|
||||
f.writeln('')
|
||||
if !f.single_line_if {
|
||||
f.writeln('')
|
||||
}
|
||||
}
|
||||
ast.FnDecl {
|
||||
f.write(it.str(f.table))
|
||||
@ -241,16 +244,36 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||
f.write(it.val)
|
||||
}
|
||||
ast.IfExpr {
|
||||
single_line := it.stmts.len == 1 && it.else_stmts.len == 1 && it.typ != table.void_type
|
||||
f.single_line_if = single_line
|
||||
f.write('if ')
|
||||
f.expr(it.cond)
|
||||
f.writeln(' {')
|
||||
if single_line {
|
||||
f.write(' { ')
|
||||
}
|
||||
else {
|
||||
f.writeln(' {')
|
||||
}
|
||||
f.stmts(it.stmts)
|
||||
if single_line {
|
||||
f.write(' ')
|
||||
}
|
||||
f.write('}')
|
||||
if it.else_stmts.len > 0 {
|
||||
f.writeln(' else {')
|
||||
f.write(' else {')
|
||||
if single_line {
|
||||
f.write(' ')
|
||||
}
|
||||
else {
|
||||
f.writeln('')
|
||||
}
|
||||
f.stmts(it.else_stmts)
|
||||
if single_line {
|
||||
f.write(' ')
|
||||
}
|
||||
f.write('}')
|
||||
}
|
||||
f.single_line_if = false
|
||||
}
|
||||
ast.Ident {
|
||||
f.write('$it.name')
|
||||
|
@ -66,5 +66,6 @@ fn fn_with_3_args(arg1 string, arg2 int, arg3 User) int {
|
||||
}
|
||||
|
||||
fn (this User) fn_with_receiver() {
|
||||
x := if true { 1 } else { 2 }
|
||||
println('')
|
||||
}
|
||||
|
@ -70,6 +70,7 @@ return 0
|
||||
}
|
||||
|
||||
fn (this User) fn_with_receiver() {
|
||||
x := if true {1} else {2}
|
||||
println('')
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user