From e2e79138457d6be4c52f02b74538d9e7bc7ea635 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 23:18:00 +0100 Subject: [PATCH] chore: add method call formatter (#3106) --- tooling/nargo_fmt/src/visitor/expr.rs | 19 +++++++++++++++++++ .../nargo_fmt/tests/expected/method_call.nr | 3 +++ tooling/nargo_fmt/tests/input/method_call.nr | 3 +++ 3 files changed, 25 insertions(+) create mode 100644 tooling/nargo_fmt/tests/expected/method_call.nr create mode 100644 tooling/nargo_fmt/tests/input/method_call.nr diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 433235e0017..b5ff3e523bc 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -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::>() + .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) diff --git a/tooling/nargo_fmt/tests/expected/method_call.nr b/tooling/nargo_fmt/tests/expected/method_call.nr new file mode 100644 index 00000000000..c2ee71237f8 --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/method_call.nr @@ -0,0 +1,3 @@ +fn foo() { + my_object.some_method(10, var_value, inner_method(20, 30)); +} diff --git a/tooling/nargo_fmt/tests/input/method_call.nr b/tooling/nargo_fmt/tests/input/method_call.nr new file mode 100644 index 00000000000..175647b4650 --- /dev/null +++ b/tooling/nargo_fmt/tests/input/method_call.nr @@ -0,0 +1,3 @@ +fn foo() { + my_object . some_method( 10,var_value,inner_method( 20 , 30) ); +}