fmt: fix chain calls with comments (#14470)

master
yuyi 2022-05-20 16:47:44 +08:00 committed by GitHub
parent 11bdb04d0c
commit bf44572f30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -1639,6 +1639,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
for arg in node.args { for arg in node.args {
f.comments(arg.comments) f.comments(arg.comments)
} }
mut is_method_newline := false
if node.is_method { if node.is_method {
if node.name in ['map', 'filter', 'all', 'any'] { if node.name in ['map', 'filter', 'all', 'any'] {
f.in_lambda_depth++ f.in_lambda_depth++
@ -1658,6 +1659,11 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
} }
} }
f.expr(node.left) f.expr(node.left)
is_method_newline = node.left.pos().last_line != node.name_pos.line_nr
if is_method_newline {
f.indent++
f.writeln('')
}
f.write('.' + node.name) f.write('.' + node.name)
} else { } else {
f.write_language_prefix(node.language) f.write_language_prefix(node.language)
@ -1680,6 +1686,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
f.write(')') f.write(')')
f.or_expr(node.or_block) f.or_expr(node.or_block)
f.comments(node.comments, has_nl: false) f.comments(node.comments, has_nl: false)
if is_method_newline {
f.indent--
}
} }
fn (mut f Fmt) write_generic_call_if_require(node ast.CallExpr) { fn (mut f Fmt) write_generic_call_if_require(node ast.CallExpr) {

View File

@ -0,0 +1,10 @@
fn main() {
options := cmdline
.only_options(args)
.filter(it != '-') // options, not including '-'
.map(if it.bytes().len > 1 {
1
})
println(options)
}

View File

@ -0,0 +1,10 @@
fn main() {
options := cmdline
.only_options(args)
.filter(it != '-') // options, not including '-'
.map(if it.bytes().len>1{
1
})
println(options)
}