From a5353217a1f49b83daf11d5fa55e0bcccebf0271 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 10 Oct 2023 10:33:36 +0100 Subject: [PATCH] feat(formatter): add formatter support for array literals (#3061) Co-authored-by: kevaundray --- .../brillig_unitialised_arrays/src/main.nr | 4 ++-- .../conditional_regression_661/src/main.nr | 2 +- .../distinct_keyword/src/main.nr | 2 +- tooling/nargo_fmt/src/visitor/expr.rs | 19 ++++++++++++++++++- tooling/nargo_fmt/tests/expected/literals.nr | 13 +++++++++++++ tooling/nargo_fmt/tests/input/literals.nr | 15 +++++++++++++++ 6 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tooling/nargo_fmt/tests/expected/literals.nr create mode 100644 tooling/nargo_fmt/tests/input/literals.nr diff --git a/tooling/nargo_cli/tests/execution_success/brillig_unitialised_arrays/src/main.nr b/tooling/nargo_cli/tests/execution_success/brillig_unitialised_arrays/src/main.nr index e0efbad1f42..4f24e179124 100644 --- a/tooling/nargo_cli/tests/execution_success/brillig_unitialised_arrays/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/brillig_unitialised_arrays/src/main.nr @@ -9,5 +9,5 @@ fn sum_x(notes: [Field; 2], x: Field, y: Field) -> Field { } unconstrained fn create_notes(x: Field, y: Field) -> [Field; 2] { - [x,y] -} \ No newline at end of file + [x, y] +} diff --git a/tooling/nargo_cli/tests/execution_success/conditional_regression_661/src/main.nr b/tooling/nargo_cli/tests/execution_success/conditional_regression_661/src/main.nr index a4765ecfc00..375b49cc27b 100644 --- a/tooling/nargo_cli/tests/execution_success/conditional_regression_661/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/conditional_regression_661/src/main.nr @@ -18,7 +18,7 @@ fn test5(a : u32) { fn issue_661_foo(array: [u32;4], b:u32) ->[u32;1] { - [array[0]+b] + [array[0] + b] } fn issue_661_bar(a : [u32;4]) ->[u32;4] { diff --git a/tooling/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr b/tooling/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr index d84be844d8e..0e55a011a48 100644 --- a/tooling/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr @@ -1,4 +1,4 @@ // Example that uses the distinct keyword fn main(x: pub Field) -> distinct pub [Field;2] { - [x+1, x] + [x + 1, x] } diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 5209f669ce1..fa55ac71f78 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -1,5 +1,6 @@ use noirc_frontend::{ - hir::resolution::errors::Span, BlockExpression, Expression, ExpressionKind, Statement, + hir::resolution::errors::Span, ArrayLiteral, BlockExpression, Expression, ExpressionKind, + Literal, Statement, }; use super::FmtVisitor; @@ -38,6 +39,22 @@ impl FmtVisitor<'_> { self.format_expr(infix.rhs) ) } + ExpressionKind::Literal(literal) => match literal { + Literal::Integer(_) => slice!(self, span.start(), span.end()).to_string(), + Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => { + format!("[{}; {length}]", self.format_expr(*repeated_element)) + } + // TODO: Handle line breaks when array gets too long. + Literal::Array(ArrayLiteral::Standard(exprs)) => { + let contents: Vec = + exprs.into_iter().map(|expr| self.format_expr(expr)).collect(); + format!("[{}]", contents.join(", ")) + } + + Literal::Bool(_) | Literal::Str(_) | Literal::FmtStr(_) | Literal::Unit => { + literal.to_string() + } + }, // TODO: _expr => slice!(self, span.start(), span.end()).to_string(), } diff --git a/tooling/nargo_fmt/tests/expected/literals.nr b/tooling/nargo_fmt/tests/expected/literals.nr new file mode 100644 index 00000000000..44a74a5db68 --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/literals.nr @@ -0,0 +1,13 @@ +fn main() { + [1, 2, 3, 4, 5]; + + [1; 5]; + + [0xff; 5]; + + true; + + "hello world"; + + () +} diff --git a/tooling/nargo_fmt/tests/input/literals.nr b/tooling/nargo_fmt/tests/input/literals.nr new file mode 100644 index 00000000000..5ca6af41a13 --- /dev/null +++ b/tooling/nargo_fmt/tests/input/literals.nr @@ -0,0 +1,15 @@ + +fn main() { + [1,2,3,4,5]; + + + [1;5]; + + [0xff;5]; + + true; + + "hello world"; + + () +}