Skip to content

Commit

Permalink
Fix formatting of abi & trait function signatures (#5186)
Browse files Browse the repository at this point in the history
Closes #4285 

What this does:
- Adds the necessary information in `Shape` for `FnSignature`'s `Format`
implementation, to always format as a `Function`. This bug occurred
because `FnSignature` always inherited the parent type's `ExprKind`,
meaning it will format correctly so long as the parent type (`ItemFn`)
exists. Otherwise it will default to the normal format.
- Adds tests for `ItemAbi` & `ItemTrait`
- Removes an unnecessary indentation from `ItemImpl`

---------

Co-authored-by: Chris O'Brien <[email protected]>
Co-authored-by: Sophie Dankel <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2023
1 parent 66a4abc commit 53fd58d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
14 changes: 14 additions & 0 deletions swayfmt/src/items/item_abi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ fn hi() -> bool;
fn hi2(hello: bool);
fn hi3(hello: bool)-> u64;// here too
}");

fmt_test_item!(abi_multiline_method
"abi MyContract {
fn complex_function(
arg1: MyStruct<[b256; 3], u8>,
arg2: [MyStruct<u64, bool>; 4],
arg3: (str[5], bool),
arg4: MyOtherStruct,
) -> str[6];
}",
intermediate_whitespace
"abi MyContract {
fn complex_function( arg1: MyStruct<[b256; 3], u8> , arg2: [MyStruct <u64, bool>; 4], arg3: ( str[5], bool ), arg4: MyOtherStruct) -> str[6] ;
}");
4 changes: 3 additions & 1 deletion swayfmt/src/items/item_fn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ impl Format for FnSignature {
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
formatter.shape.code_line.has_where_clause = formatter.with_shape(
formatter.shape,
formatter
.shape
.with_code_line_from(LineStyle::Normal, ExprKind::Function),
|formatter| -> Result<bool, FormatterError> {
let mut fn_sig = FormattedCode::new();
let mut fn_args = FormattedCode::new();
Expand Down
1 change: 0 additions & 1 deletion swayfmt/src/items/item_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl Format for ItemImpl {
formatter.indent();
writeln!(formatted_code)?;
for item in contents.iter() {
write!(formatted_code, "{}", formatter.indent_to_str()?,)?;
item.format(formatted_code, formatter)?;
writeln!(formatted_code)?;
}
Expand Down
13 changes: 13 additions & 0 deletions swayfmt/src/items/item_trait/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ intermediate_whitespace
fn b(self);
} "
);
fmt_test_item!(trait_multiline_method
"trait MyComplexTrait {
fn complex_function(
arg1: MyStruct<[b256; 3], u8>,
arg2: [MyStruct<u64, bool>; 4],
arg3: (str[5], bool),
arg4: MyOtherStruct,
) -> str[6];
}",
intermediate_whitespace
"trait MyComplexTrait {
fn complex_function( arg1: MyStruct<[b256; 3], u8> , arg2: [MyStruct <u64, bool>; 4], arg3: ( str[5], bool ), arg4: MyOtherStruct) -> str[6] ;
}");

0 comments on commit 53fd58d

Please sign in to comment.