diff --git a/examples/subcurrency/src/main.sw b/examples/subcurrency/src/main.sw index 25afc6d56f2..e21e7fdc31a 100644 --- a/examples/subcurrency/src/main.sw +++ b/examples/subcurrency/src/main.sw @@ -89,12 +89,7 @@ impl Token for Contract { // Reduce the balance of sender let sender_amount = storage.balances.get(sender).try_read().unwrap_or(0); assert(sender_amount > amount); - storage - .balances - .insert( - sender, - sender_amount - amount, - ); + storage.balances.insert(sender, sender_amount - amount); // Increase the balance of receiver storage diff --git a/examples/wallet_contract_caller_script/src/main.sw b/examples/wallet_contract_caller_script/src/main.sw index 43513926a95..d7482c8de15 100644 --- a/examples/wallet_contract_caller_script/src/main.sw +++ b/examples/wallet_contract_caller_script/src/main.sw @@ -15,8 +15,5 @@ fn main() { gas: 10000, coins: 0, asset_id: ZERO_B256, - }( - amount_to_send, - recipient_address, - ); + }(amount_to_send, recipient_address); } diff --git a/swayfmt/src/utils/language/expr/mod.rs b/swayfmt/src/utils/language/expr/mod.rs index a7883533093..9992982972d 100644 --- a/swayfmt/src/utils/language/expr/mod.rs +++ b/swayfmt/src/utils/language/expr/mod.rs @@ -737,39 +737,48 @@ where }, )?; - let mut buf = FormattedCode::new(); - args.format(&mut buf, formatter)?; - - Ok(if has_single_argument_and_can_be_inlined { - buf.trim().to_owned() - } else { - // Check if the arguments can fit on a single line - let expr_width = buf.chars().count(); - formatter.shape.code_line.add_width(expr_width); + formatter.with_shape( formatter .shape - .get_line_style(None, Some(expr_width), &formatter.config); + .with_code_line_from(LineStyle::Normal, ExprKind::Function), + |formatter| -> Result { + let mut buf = FormattedCode::new(); + args.format(&mut buf, formatter)?; - if expr_width == 0 { - return Ok("".to_owned()); - } - match formatter.shape.code_line.line_style { - LineStyle::Multiline => { - // force each param to be a new line - formatter.shape.code_line.update_expr_new_line(true); - formatter.indent(); - // should be rewritten to a multi-line - let mut formatted_code = FormattedCode::new(); - let mut buf = FormattedCode::new(); - args.format(&mut buf, formatter)?; - formatter.unindent(); - writeln!(formatted_code, "{}", buf.trim_end())?; - formatter.write_indent_into_buffer(&mut formatted_code)?; - formatted_code - } - _ => buf.trim().to_owned(), - } - }) + Ok(if has_single_argument_and_can_be_inlined { + buf.trim().to_owned() + } else { + // Check if the arguments can fit on a single line + let expr_width = buf.chars().count(); + formatter.shape.code_line.add_width(expr_width); + formatter.shape.get_line_style( + Some(expr_width), + Some(expr_width), + &formatter.config, + ); + + if expr_width == 0 { + return Ok("".to_owned()); + } + match formatter.shape.code_line.line_style { + LineStyle::Multiline => { + // force each param to be a new line + formatter.shape.code_line.update_expr_new_line(true); + formatter.indent(); + // should be rewritten to a multi-line + let mut formatted_code = FormattedCode::new(); + let mut buf = FormattedCode::new(); + args.format(&mut buf, formatter)?; + formatter.unindent(); + writeln!(formatted_code, "{}", buf.trim_end())?; + formatter.write_indent_into_buffer(&mut formatted_code)?; + formatted_code + } + _ => buf.trim().to_owned(), + } + }) + }, + ) } fn format_method_call( diff --git a/swayfmt/tests/mod.rs b/swayfmt/tests/mod.rs index fe79c5f8fda..2cde30d78a8 100644 --- a/swayfmt/tests/mod.rs +++ b/swayfmt/tests/mod.rs @@ -1165,30 +1165,17 @@ fn main() { // This is a comment storage .pledge_history - .insert( - (user, pledge_history_index), - pledge, - ); + .insert((user, pledge_history_index), pledge); } // This is also a comment, // but multiline else if true { // This is yet another comment - storage - .pledge_count - .insert( - user, - pledge_count + 1, - ); + storage.pledge_count.insert(user, pledge_count + 1); } // This is the last comment else { - storage - .pledge_count - .insert( - user, - pledge_count + 1, - ); + storage.pledge_count.insert(user, pledge_count + 1); } } "#, @@ -2181,3 +2168,39 @@ fn main() { "#, ); } + +#[test] +fn method_call_2() { + check( + r#" + library; + +fn main() { + let contract_address = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b; + let caller = abi(Wallet, contract_address); + let amount_to_send = 200; + let recipient_address = Address::from(contract_address); + caller.send_funds { gas: 10000, coins: 0, asset_id: ZERO_B256 }( + amount_to_send, + recipient_address, + ); +} + + "#, + r#"library; + +fn main() { + let contract_address = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b; + let caller = abi(Wallet, contract_address); + let amount_to_send = 200; + let recipient_address = Address::from(contract_address); + caller + .send_funds { + gas: 10000, + coins: 0, + asset_id: ZERO_B256, + }(amount_to_send, recipient_address); +} +"#, + ) +}