From d90f606fd55685bca4763f3bbbde1e6e2f9b3d8f Mon Sep 17 00:00:00 2001 From: bing Date: Tue, 14 Mar 2023 21:01:59 +0000 Subject: [PATCH] fix(fmt): do not add extra spaces in empty parameter list when formatting (#4269) ## Description Closes #4250 Moves the indent/unindent logic within the `LineStyle::Multiline` match branch, and add an additional check for empty args before trying to indent/unindent. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Mohammad Fawaz --- swayfmt/src/items/item_fn/mod.rs | 49 ++++++++++++---------------- swayfmt/src/items/item_impl/tests.rs | 16 +++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/swayfmt/src/items/item_fn/mod.rs b/swayfmt/src/items/item_fn/mod.rs index 3f8859da2d5..0c69453bd39 100644 --- a/swayfmt/src/items/item_fn/mod.rs +++ b/swayfmt/src/items/item_fn/mod.rs @@ -201,9 +201,21 @@ fn format_fn_args( formatter: &mut Formatter, ) -> Result<(), FormatterError> { match fn_args { - FnArgs::Static(args) => { - args.format(formatted_code, formatter)?; - } + FnArgs::Static(args) => match formatter.shape.code_line.line_style { + LineStyle::Multiline => { + if !args.value_separator_pairs.is_empty() || args.final_value_opt.is_some() { + formatter.shape.block_indent(&formatter.config); + args.format(formatted_code, formatter)?; + formatter.shape.block_unindent(&formatter.config); + write!( + formatted_code, + "{}", + formatter.shape.indent.to_string(&formatter.config)? + )?; + } + } + _ => args.format(formatted_code, formatter)?, + }, FnArgs::NonStatic { self_token, ref_self, @@ -212,6 +224,7 @@ fn format_fn_args( } => { match formatter.shape.code_line.line_style { LineStyle::Multiline => { + formatter.shape.block_indent(&formatter.config); write!( formatted_code, "\n{}", @@ -266,39 +279,17 @@ fn format_self( impl Parenthesis for FnSignature { fn open_parenthesis( line: &mut FormattedCode, - formatter: &mut Formatter, + _formatter: &mut Formatter, ) -> Result<(), FormatterError> { - let open_paren = Delimiter::Parenthesis.as_open_char(); - match formatter.shape.code_line.line_style { - LineStyle::Multiline => { - formatter.shape.block_indent(&formatter.config); - write!(line, "{open_paren}")?; - } - _ => { - write!(line, "{open_paren}")?; - } - } + write!(line, "{}", Delimiter::Parenthesis.as_open_char())?; Ok(()) } fn close_parenthesis( line: &mut FormattedCode, - formatter: &mut Formatter, + _formatter: &mut Formatter, ) -> Result<(), FormatterError> { - let close_paren = Delimiter::Parenthesis.as_close_char(); - match formatter.shape.code_line.line_style { - LineStyle::Multiline => { - formatter.shape.block_unindent(&formatter.config); - write!( - line, - "{}{close_paren}", - formatter.shape.indent.to_string(&formatter.config)? - )?; - } - _ => { - write!(line, "{close_paren}")?; - } - } + write!(line, "{}", Delimiter::Parenthesis.as_close_char())?; Ok(()) } diff --git a/swayfmt/src/items/item_impl/tests.rs b/swayfmt/src/items/item_impl/tests.rs index 0d74ef527dd..4d71d5092b7 100644 --- a/swayfmt/src/items/item_impl/tests.rs +++ b/swayfmt/src/items/item_impl/tests.rs @@ -74,3 +74,19 @@ fmt_test_item!( normal_with_generics } }" ); + +fmt_test_item!( impl_empty_fn_args +"impl TestContract for Contract { + fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric, EnumWithGeneric) { + (U8, BOOL, ARRAY, STR_4, STRUCT, ENUM) + } +}", + intermediate_whitespace + "impl TestContract for Contract { + fn return_configurables( ) -> ( u8, bool, [u32; 3], str[4], StructWithGeneric, EnumWithGeneric + ) { + ( U8, BOOL, ARRAY, STR_4 , STRUCT, ENUM ) + } +} +" +);