1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: better function call wrapping inside ternary if branches (#8604)

* fmt: better funcation call wrap in singel line ifs

* tests

* format files
This commit is contained in:
Lukas Neubert 2021-02-06 21:46:52 +01:00 committed by GitHub
parent cf230644b6
commit db0fc8fbc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 16 deletions

View File

@ -1718,7 +1718,7 @@ pub fn (mut f Fmt) call_args(args []ast.CallArg) {
if arg.is_mut {
f.write(arg.share.str() + ' ')
}
if i > 0 {
if i > 0 && !f.single_line_if {
f.wrap_long_line(3, true)
}
f.expr(arg.expr)
@ -1782,14 +1782,6 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
}
f.expr(node.left)
f.write('.' + node.name)
f.write_generic_if_require(node)
f.write('(')
f.call_args(node.args)
f.write(')')
// if is_mut {
// f.write('!')
// }
f.or_expr(node.or_block)
} else {
f.write_language_prefix(node.language)
if node.left is ast.AnonFn {
@ -1804,12 +1796,12 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
}
f.write('$name')
}
f.write_generic_if_require(node)
f.write('(')
f.call_args(node.args)
f.write(')')
f.or_expr(node.or_block)
}
f.write_generic_if_require(node)
f.write('(')
f.call_args(node.args)
f.write(')')
f.or_expr(node.or_block)
f.comments(node.comments, has_nl: false)
f.use_short_fn_args = old_short_arg_state
}

View File

@ -31,3 +31,18 @@ fn condition_is_very_long_infix() {
'false'
}
}
fn branches_are_long_fn_calls() {
_ := if nr_dims == 1 {
t.find_or_register_array(elem_type)
} else {
t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1))
}
// With another arg to make fn call exceed the max_len after if unwrapping
_ := if nr_dims == 1 {
t.find_or_register_array(elem_type)
} else {
t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1,
'some string'))
}
}

View File

@ -11,3 +11,9 @@ fn main() {
fn condition_is_very_long_infix() {
val := if the_first_condition && this_is_required_too && (another_cond || foobar_to_exceed_the_max_len) { 'true' } else { 'false' }
}
fn branches_are_long_fn_calls() {
_ := if nr_dims == 1 { t.find_or_register_array(elem_type) } else { t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1)) }
// With another arg to make fn call exceed the max_len after if unwrapping
_ := if nr_dims == 1 { t.find_or_register_array(elem_type) } else { t.find_or_register_arra(t.find_or_register_array_with_dims(elem_type, nr_dims - 1, 'some string')) }
}

View File

@ -228,8 +228,11 @@ pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
cookie_header = ' ' + cookie_header
// println('cookie_header="$cookie_header"')
// println(ctx.req.headers)
cookie := if cookie_header.contains(';') { cookie_header.find_between(' $key=', ';') } else { cookie_header.find_between(' $key=',
'\r') }
cookie := if cookie_header.contains(';') {
cookie_header.find_between(' $key=', ';')
} else {
cookie_header.find_between(' $key=', '\r')
}
if cookie != '' {
return cookie.trim_space()
}