From 2007dbc7b5abaac8c524af1bbc5c32502521d69b Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Tue, 26 Jan 2021 11:19:32 +0100 Subject: [PATCH] fmt: put the opening brace on a new line again for infix (#8336) --- vlib/v/checker/checker.v | 3 ++- vlib/v/fmt/fmt.v | 11 ++++++++++- vlib/v/fmt/tests/if_brace_on_newline_expected.vv | 12 ++++++++++++ vlib/v/fmt/tests/if_brace_on_newline_input.vv | 10 ++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/if_brace_on_newline_expected.vv create mode 100644 vlib/v/fmt/tests/if_brace_on_newline_input.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7ee9b4296..99ac0ff21 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1537,7 +1537,8 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { } if call_expr.generic_types.len > 0 && method.return_type != 0 { if typ := c.resolve_generic_type(method.return_type, method.generic_names, - call_expr.generic_types) { + call_expr.generic_types) + { call_expr.return_type = typ return typ } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 810f31317..1396460ab 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1564,8 +1564,17 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) { } if i < it.branches.len - 1 || !it.has_else { f.write('${dollar}if ') + cur_pos := f.out.len f.expr(branch.cond) - f.write(' ') + cond_len := f.out.len - cur_pos + is_cond_wrapped := cond_len > 0 + && (branch.cond is ast.IfGuardExpr || branch.cond is ast.CallExpr) + && '\n' in f.out.last_n(cond_len) + if is_cond_wrapped { + f.writeln('') + } else { + f.write(' ') + } } f.write('{') if single_line { diff --git a/vlib/v/fmt/tests/if_brace_on_newline_expected.vv b/vlib/v/fmt/tests/if_brace_on_newline_expected.vv new file mode 100644 index 000000000..66131089a --- /dev/null +++ b/vlib/v/fmt/tests/if_brace_on_newline_expected.vv @@ -0,0 +1,12 @@ +fn get_typ() Type { + { + { + // The opening brace should be put on a new line here for readability + if typ := c.resolve_generic_type(method.return_type, method.generic_names, + call_expr.generic_types) + { + return typ + } + } + } +} diff --git a/vlib/v/fmt/tests/if_brace_on_newline_input.vv b/vlib/v/fmt/tests/if_brace_on_newline_input.vv new file mode 100644 index 000000000..52b9b2a2d --- /dev/null +++ b/vlib/v/fmt/tests/if_brace_on_newline_input.vv @@ -0,0 +1,10 @@ +fn get_typ() Type { + { + { + // The opening brace should be put on a new line here for readability + if typ := c.resolve_generic_type(method.return_type, method.generic_names, call_expr.generic_types) { + return typ + } + } + } +} -- 2.39.5