Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add method call formatter #3106

Merged
merged 12 commits into from
Oct 11, 2023
19 changes: 19 additions & 0 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ impl FmtVisitor<'_> {
self.format_expr(infix.rhs)
)
}
ExpressionKind::MethodCall(method_call_expr) => {
let formatted_object = self.format_expr(method_call_expr.object).trim().to_string();
let formatted_args = method_call_expr
.arguments
.iter()
.map(|arg| {
let arg_str = self.format_expr(arg.clone()).trim().to_string();
if arg_str.contains('(') {
return arg_str
.replace(" ,", ",")
.replace("( ", "(")
.replace(" )", ")");
}
arg_str
})
.collect::<Vec<_>>()
.join(", ");
format!("{}.{}({})", formatted_object, method_call_expr.method_name, formatted_args)
}
ExpressionKind::MemberAccess(member_access_expr) => {
let lhs_str = self.format_expr(member_access_expr.lhs);
format!("{}.{}", lhs_str, member_access_expr.rhs)
Expand Down
3 changes: 3 additions & 0 deletions tooling/nargo_fmt/tests/expected/method_call.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo() {
my_object.some_method(10, var_value, inner_method(20, 30));
}
3 changes: 3 additions & 0 deletions tooling/nargo_fmt/tests/input/method_call.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn foo() {
my_object . some_method( 10,var_value,inner_method( 20 , 30) );
}