From 6ffcad670e50a2ddcc27362431a1fa4d08fe45b8 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 17:17:58 +0100 Subject: [PATCH 1/3] Add member access --- tooling/nargo_fmt/src/visitor/expr.rs | 4 ++++ tooling/nargo_fmt/tests/expected/member_access.nr | 8 ++++++++ tooling/nargo_fmt/tests/input/member_access.nr | 8 ++++++++ 3 files changed, 20 insertions(+) create mode 100644 tooling/nargo_fmt/tests/expected/member_access.nr create mode 100644 tooling/nargo_fmt/tests/input/member_access.nr diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index fa55ac71f78..4d20b2c9aab 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -39,6 +39,10 @@ impl FmtVisitor<'_> { self.format_expr(infix.rhs) ) } + ExpressionKind::MemberAccess(member_access_expr) => { + let lhs_str = self.format_expr(member_access_expr.lhs); + format!("{}.{}", lhs_str, member_access_expr.rhs) + } ExpressionKind::Literal(literal) => match literal { Literal::Integer(_) => slice!(self, span.start(), span.end()).to_string(), Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => { diff --git a/tooling/nargo_fmt/tests/expected/member_access.nr b/tooling/nargo_fmt/tests/expected/member_access.nr new file mode 100644 index 00000000000..055ebff1ee6 --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/member_access.nr @@ -0,0 +1,8 @@ +struct Person { + name: String, + age: u8, +} + +fn foo(p: Person) { + p.name; +} diff --git a/tooling/nargo_fmt/tests/input/member_access.nr b/tooling/nargo_fmt/tests/input/member_access.nr new file mode 100644 index 00000000000..18c6e0c774b --- /dev/null +++ b/tooling/nargo_fmt/tests/input/member_access.nr @@ -0,0 +1,8 @@ +struct Person { + name: String, + age: u8, +} + +fn foo(p: Person) { + p . name; +} From 37a03f3ef952157367f467b10246ed56cd81c838 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 17:38:43 +0100 Subject: [PATCH 2/3] Add nested --- tooling/nargo_fmt/tests/expected/member_access.nr | 7 +++++++ tooling/nargo_fmt/tests/input/member_access.nr | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/tooling/nargo_fmt/tests/expected/member_access.nr b/tooling/nargo_fmt/tests/expected/member_access.nr index 055ebff1ee6..2330ec826c7 100644 --- a/tooling/nargo_fmt/tests/expected/member_access.nr +++ b/tooling/nargo_fmt/tests/expected/member_access.nr @@ -1,8 +1,15 @@ +struct Address { + city: String, + country: String, +} + struct Person { name: String, age: u8, + address: Address, } fn foo(p: Person) { p.name; + p.address.country; } diff --git a/tooling/nargo_fmt/tests/input/member_access.nr b/tooling/nargo_fmt/tests/input/member_access.nr index 18c6e0c774b..abcab671836 100644 --- a/tooling/nargo_fmt/tests/input/member_access.nr +++ b/tooling/nargo_fmt/tests/input/member_access.nr @@ -1,8 +1,15 @@ +struct Address { + city: String, + country: String, +} + struct Person { name: String, age: u8, + address: Address, } fn foo(p: Person) { p . name; + p.address . country; } From 60ce7469be3a159a84e09d5c876cab9523fd5061 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 17:56:04 +0100 Subject: [PATCH 3/3] Fix typo --- tooling/nargo_fmt/src/visitor/expr.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 46bb0ea773a..433235e0017 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -42,6 +42,7 @@ impl FmtVisitor<'_> { ExpressionKind::MemberAccess(member_access_expr) => { let lhs_str = self.format_expr(member_access_expr.lhs); format!("{}.{}", lhs_str, member_access_expr.rhs) + } ExpressionKind::Index(index_expr) => { let formatted_collection = self.format_expr(index_expr.collection).trim_end().to_string();