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 support for index expressions in formatter #3069

Merged
merged 11 commits into from
Oct 11, 2023
6 changes: 6 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,12 @@ impl FmtVisitor<'_> {
self.format_expr(infix.rhs)
)
}
ExpressionKind::Index(index_expr) => {
let formatted_collection =
self.format_expr(index_expr.collection).trim_end().to_string();
let formatted_index = self.format_expr(index_expr.index);
format!("{}[{}]", formatted_collection, formatted_index)
}
ExpressionKind::Literal(literal) => match literal {
Literal::Integer(_) => slice!(self, span.start(), span.end()).to_string(),
Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => {
Expand Down
5 changes: 5 additions & 0 deletions tooling/nargo_fmt/tests/expected/index.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn foo() {
let arr = [10, 20, 30, 40];
arr[2];
arr[2];
}
5 changes: 5 additions & 0 deletions tooling/nargo_fmt/tests/input/index.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn foo() {
let arr = [10, 20, 30, 40];
arr [2];
arr [2];
}
Loading