From c0e602002014e54475b5de4c9106cc033438fc53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CF=87=C2=B2?= <88190723+ChiTimesChi@users.noreply.github.com> Date: Thu, 24 Oct 2024 18:26:07 +0100 Subject: [PATCH] fix(fmt): multiline single param only if func definition is multiline for `all_params` (#9187) * test: adjust single param multiline expected behavior * fix: `AllParams` single param multiline condition * refactor: try simplifying the condition logic --- crates/fmt/src/formatter.rs | 15 +++++++++------ .../FunctionDefinition/all-params.fmt.sol | 8 ++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/crates/fmt/src/formatter.rs b/crates/fmt/src/formatter.rs index 9213235fe331b..6bb814e232691 100644 --- a/crates/fmt/src/formatter.rs +++ b/crates/fmt/src/formatter.rs @@ -1638,14 +1638,17 @@ impl<'a, W: Write> Formatter<'a, W> { ¶ms, ",", )?; - // Write new line if we have only one parameter and params first all multi set. - if params.len() == 1 && + // Write new line if we have only one parameter and params first set, + // or if the function definition is multiline and all params set. + let single_param_multiline = matches!( + fmt.config.multiline_func_header, + MultilineFuncHeaderStyle::ParamsFirst + ) || params_multiline && matches!( fmt.config.multiline_func_header, - MultilineFuncHeaderStyle::ParamsFirst | - MultilineFuncHeaderStyle::AllParams - ) - { + MultilineFuncHeaderStyle::AllParams + ); + if params.len() == 1 && single_param_multiline { writeln!(fmt.buf())?; } fmt.write_chunks_separated(¶ms, ",", params_multiline)?; diff --git a/crates/fmt/testdata/FunctionDefinition/all-params.fmt.sol b/crates/fmt/testdata/FunctionDefinition/all-params.fmt.sol index f723b00496b63..db7164d284a54 100644 --- a/crates/fmt/testdata/FunctionDefinition/all-params.fmt.sol +++ b/crates/fmt/testdata/FunctionDefinition/all-params.fmt.sol @@ -3,9 +3,7 @@ interface FunctionInterfaces { function noParamsNoModifiersNoReturns(); - function oneParam( - uint256 x - ); + function oneParam(uint256 x); function oneModifier() modifier1; @@ -345,9 +343,7 @@ contract FunctionDefinitions { a = 1; } - function oneParam( - uint256 x - ) { + function oneParam(uint256 x) { a = 1; }