From d79fdc075da157c3519faed8ebe296f29f3c9e1f Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 20 May 2022 16:47:44 +0800 Subject: [PATCH] fmt: fix chain calls with comments (#14470) --- vlib/v/fmt/fmt.v | 9 +++++++++ vlib/v/fmt/tests/chain_calls_with_comments_expected.vv | 10 ++++++++++ vlib/v/fmt/tests/chain_calls_with_comments_input.vv | 10 ++++++++++ 3 files changed, 29 insertions(+) create mode 100644 vlib/v/fmt/tests/chain_calls_with_comments_expected.vv create mode 100644 vlib/v/fmt/tests/chain_calls_with_comments_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6550fb73be..8bd1e794ee 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1639,6 +1639,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { for arg in node.args { f.comments(arg.comments) } + mut is_method_newline := false if node.is_method { if node.name in ['map', 'filter', 'all', 'any'] { f.in_lambda_depth++ @@ -1658,6 +1659,11 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { } } 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) } else { f.write_language_prefix(node.language) @@ -1680,6 +1686,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { f.write(')') f.or_expr(node.or_block) 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) { diff --git a/vlib/v/fmt/tests/chain_calls_with_comments_expected.vv b/vlib/v/fmt/tests/chain_calls_with_comments_expected.vv new file mode 100644 index 0000000000..9528157274 --- /dev/null +++ b/vlib/v/fmt/tests/chain_calls_with_comments_expected.vv @@ -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) +} diff --git a/vlib/v/fmt/tests/chain_calls_with_comments_input.vv b/vlib/v/fmt/tests/chain_calls_with_comments_input.vv new file mode 100644 index 0000000000..300cf0665b --- /dev/null +++ b/vlib/v/fmt/tests/chain_calls_with_comments_input.vv @@ -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) +}