fmt: allow single line ternary if as function argument (#8486)
parent
79e9084f7b
commit
cf1084105c
|
@ -35,7 +35,6 @@ pub mut:
|
|||
file ast.File
|
||||
did_imports bool
|
||||
is_assign bool
|
||||
is_arr_push bool
|
||||
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
|
||||
|
@ -1478,11 +1477,7 @@ pub fn (mut f Fmt) array_decompose(node ast.ArrayDecompose) {
|
|||
}
|
||||
|
||||
pub fn (mut f Fmt) lock_expr(lex ast.LockExpr) {
|
||||
f.write(if lex.is_rlock {
|
||||
'rlock '
|
||||
} else {
|
||||
'lock '
|
||||
})
|
||||
f.write(if lex.is_rlock { 'rlock ' } else { 'lock ' })
|
||||
for i, v in lex.lockeds {
|
||||
if i > 0 {
|
||||
f.write(', ')
|
||||
|
@ -1500,8 +1495,9 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
|
|||
if !f.buffering && node.op in [.logical_or, .and, .plus] {
|
||||
f.buffering = true
|
||||
}
|
||||
is_assign_save := f.is_assign
|
||||
if node.op == .left_shift {
|
||||
f.is_arr_push = true
|
||||
f.is_assign = true // To write ternary if on a single line
|
||||
}
|
||||
infix_start := f.out.len
|
||||
start_len := f.line_len
|
||||
|
@ -1527,7 +1523,7 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
|
|||
f.wrap_infix(infix_start, start_len)
|
||||
}
|
||||
}
|
||||
f.is_arr_push = false
|
||||
f.is_assign = is_assign_save
|
||||
f.or_expr(node.or_block)
|
||||
}
|
||||
|
||||
|
@ -1592,7 +1588,8 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int) {
|
|||
pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
|
||||
dollar := if it.is_comptime { '$' } else { '' }
|
||||
mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0])
|
||||
&& branch_is_single_line(it.branches[1])&& (it.is_expr || f.is_assign || f.is_arr_push)
|
||||
&& branch_is_single_line(it.branches[1])
|
||||
&& (it.is_expr || f.is_assign || f.single_line_fields)
|
||||
f.single_line_if = single_line
|
||||
if_start := f.line_len
|
||||
for {
|
||||
|
|
|
@ -8,6 +8,8 @@ fn valid_single_line() {
|
|||
[0, 1] << if true { 2 } else { 3 }
|
||||
// Empty or literal syntax struct inits
|
||||
_ := if false { Foo{} } else { Foo{5, 6} }
|
||||
// As argument for a function call
|
||||
some_func(if cond { 'param1' } else { 'param2' })
|
||||
}
|
||||
|
||||
fn requires_multiple_lines() {
|
||||
|
|
|
@ -3363,11 +3363,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
|||
if node.op in [.plus, .minus, .mul, .div, .mod, .lt, .gt, .eq, .ne, .le, .ge]
|
||||
&& ((a && b && e) || c|| d) {
|
||||
// Overloaded operators
|
||||
g.write(g.typ(if !d {
|
||||
left_type
|
||||
} else {
|
||||
(left_sym.info as table.Alias).parent_type
|
||||
}))
|
||||
g.write(g.typ(if !d { left_type } else { (left_sym.info as table.Alias).parent_type }))
|
||||
g.write('_')
|
||||
g.write(util.replace_op(node.op.str()))
|
||||
g.write('(')
|
||||
|
@ -3710,11 +3706,7 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) {
|
|||
objs_array := g.new_tmp_var()
|
||||
g.write('array_voidptr $objs_array = new_array_from_c_array($n_channels, $n_channels, sizeof(voidptr), _MOV((voidptr[$n_channels]){')
|
||||
for i in 0 .. n_channels {
|
||||
g.write(if i > 0 {
|
||||
', &'
|
||||
} else {
|
||||
'&'
|
||||
})
|
||||
g.write(if i > 0 { ', &' } else { '&' })
|
||||
if tmp_objs[i] == '' {
|
||||
g.expr(objs[i])
|
||||
} else {
|
||||
|
|
|
@ -292,11 +292,7 @@ fn (mut g Gen) expr_to_sql(expr ast.Expr) {
|
|||
// true/false literals were added to Sqlite 3.23 (2018-04-02)
|
||||
// but lots of apps/distros use older sqlite (e.g. Ubuntu 18.04 LTS )
|
||||
g.inc_sql_i()
|
||||
g.sql_bind_int(if expr.val {
|
||||
'1'
|
||||
} else {
|
||||
'0'
|
||||
})
|
||||
g.sql_bind_int(if expr.val { '1' } else { '0' })
|
||||
}
|
||||
ast.Ident {
|
||||
// `name == user_name` => `name == ?1`
|
||||
|
|
|
@ -424,11 +424,7 @@ pub fn parse_args(args []string) (&Preferences, string) {
|
|||
continue
|
||||
}
|
||||
eprint('Unknown argument `$arg`')
|
||||
eprintln(if command.len == 0 {
|
||||
''
|
||||
} else {
|
||||
' for command `$command`'
|
||||
})
|
||||
eprintln(if command.len == 0 { '' } else { ' for command `$command`' })
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue