Skip to content

Commit

Permalink
Update chained methods and arguments
Browse files Browse the repository at this point in the history
Update rules to decide whether to split chained methods and their
arguments and how to count their width. The list of arguments is
independent from their previous bytes (like in chained
functions/methods)
  • Loading branch information
cr-fuel committed Oct 25, 2023
1 parent f78dbc1 commit 86e1d62
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 56 deletions.
7 changes: 1 addition & 6 deletions examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions examples/wallet_contract_caller_script/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ fn main() {
gas: 10000,
coins: 0,
asset_id: ZERO_B256,
}(
amount_to_send,
recipient_address,
);
}(amount_to_send, recipient_address);
}
69 changes: 39 additions & 30 deletions swayfmt/src/utils/language/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, FormatterError> {
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(
Expand Down
55 changes: 39 additions & 16 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
"#,
Expand Down Expand Up @@ -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);
}
"#,
)
}

0 comments on commit 86e1d62

Please sign in to comment.