Skip to content

Commit

Permalink
chore: add support for index expressions in formatter (#3069)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
2 people authored and TomAFrench committed Oct 12, 2023
1 parent f3f30a2 commit b909e05
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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];
}

0 comments on commit b909e05

Please sign in to comment.