Skip to content

Commit

Permalink
fix(fmt): do not add extra spaces in empty parameter list when format…
Browse files Browse the repository at this point in the history
…ting (FuelLabs#4269)

## Description

Closes FuelLabs#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 <[email protected]>
  • Loading branch information
eightfilms and mohammadfawaz authored Mar 14, 2023
1 parent 259e5da commit d90f606
Showing 2 changed files with 36 additions and 29 deletions.
49 changes: 20 additions & 29 deletions swayfmt/src/items/item_fn/mod.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
16 changes: 16 additions & 0 deletions swayfmt/src/items/item_impl/tests.rs
Original file line number Diff line number Diff line change
@@ -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<u8>, EnumWithGeneric<bool>) {
(U8, BOOL, ARRAY, STR_4, STRUCT, ENUM)
}
}",
intermediate_whitespace
"impl TestContract for Contract {
fn return_configurables( ) -> ( u8, bool, [u32; 3], str[4], StructWithGeneric<u8>, EnumWithGeneric<bool>
) {
( U8, BOOL, ARRAY, STR_4 , STRUCT, ENUM )
}
}
"
);

0 comments on commit d90f606

Please sign in to comment.