Skip to content

Commit

Permalink
chore: add method call formatter (#3106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonybur authored and TomAFrench committed Oct 12, 2023
1 parent 7ad5728 commit e2e7913
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
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) );
}

0 comments on commit e2e7913

Please sign in to comment.