From 7de37c6935571c2865eca7ce11da0038ff735b2f Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Sat, 21 Oct 2023 05:59:04 -0700 Subject: [PATCH] chore: format array and index expressions (#3223) --- tooling/nargo_fmt/src/config.rs | 3 + tooling/nargo_fmt/src/utils.rs | 38 +++- tooling/nargo_fmt/src/visitor.rs | 19 +- tooling/nargo_fmt/src/visitor/expr.rs | 179 ++++++++++++++----- tooling/nargo_fmt/src/visitor/stmt.rs | 7 + tooling/nargo_fmt/tests/expected/array.nr | 23 +++ tooling/nargo_fmt/tests/expected/call.nr | 12 +- tooling/nargo_fmt/tests/expected/index.nr | 4 + tooling/nargo_fmt/tests/expected/let.nr | 17 ++ tooling/nargo_fmt/tests/expected/literals.nr | 2 + tooling/nargo_fmt/tests/expected/struct.nr | 4 +- tooling/nargo_fmt/tests/expected/tuple.nr | 37 ++-- tooling/nargo_fmt/tests/input/array.nr | 39 ++++ tooling/nargo_fmt/tests/input/index.nr | 5 + tooling/nargo_fmt/tests/input/let.nr | 15 ++ tooling/nargo_fmt/tests/input/literals.nr | 2 + 16 files changed, 321 insertions(+), 85 deletions(-) create mode 100644 tooling/nargo_fmt/tests/expected/array.nr create mode 100644 tooling/nargo_fmt/tests/expected/let.nr create mode 100644 tooling/nargo_fmt/tests/input/array.nr create mode 100644 tooling/nargo_fmt/tests/input/let.nr diff --git a/tooling/nargo_fmt/src/config.rs b/tooling/nargo_fmt/src/config.rs index 7cb80a9f04d..1514937caff 100644 --- a/tooling/nargo_fmt/src/config.rs +++ b/tooling/nargo_fmt/src/config.rs @@ -42,8 +42,11 @@ macro_rules! config { } config! { + max_width: usize, 100, "Maximum width of each line"; tab_spaces: usize, 4, "Number of spaces per tab"; remove_nested_parens: bool, true, "Remove nested parens"; + short_array_element_width_threshold: usize, 10, "Width threshold for an array element to be considered short"; + array_width: usize, 60, "Maximum width of an array literal before falling back to vertical formatting"; } impl Config { diff --git a/tooling/nargo_fmt/src/utils.rs b/tooling/nargo_fmt/src/utils.rs index a1aeadee523..4bea21be939 100644 --- a/tooling/nargo_fmt/src/utils.rs +++ b/tooling/nargo_fmt/src/utils.rs @@ -31,11 +31,21 @@ pub(crate) fn comments(source: &str) -> impl Iterator + '_ { #[derive(Debug)] pub(crate) struct Expr { pub(crate) leading: String, - pub(crate) expr: String, + pub(crate) value: String, pub(crate) trailing: String, pub(crate) different_line: bool, } +impl Expr { + pub(crate) fn total_width(&self) -> usize { + comment_len(&self.leading) + self.value.chars().count() + comment_len(&self.trailing) + } + + pub(crate) fn is_multiline(&self) -> bool { + self.leading.contains('\n') || self.trailing.contains('\n') + } +} + pub(crate) struct Exprs<'me> { pub(crate) visitor: &'me FmtVisitor<'me>, pub(crate) elements: std::iter::Peekable>, @@ -71,17 +81,17 @@ impl Iterator for Exprs<'_> { let is_last = self.elements.peek().is_none(); let next_start = self.elements.peek().map_or(self.end_position, |expr| expr.span.start()); - let (leading, newlines) = self.leading(start, end); + let (leading, different_line) = self.leading(start, end); let expr = self.visitor.format_expr(element); let trailing = self.trailing(element_span.end(), next_start, is_last); - Expr { leading, expr, trailing, different_line: newlines }.into() + Expr { leading, value: expr, trailing, different_line }.into() } } impl<'me> Exprs<'me> { pub(crate) fn leading(&mut self, start: u32, end: u32) -> (String, bool) { - let mut newlines = false; + let mut different_line = false; let leading = slice!(self.visitor, start, end); let leading_trimmed = slice!(self.visitor, start, end).trim(); @@ -94,13 +104,13 @@ impl<'me> Exprs<'me> { let comment_end = leading_trimmed.rfind(|c| c == '/').unwrap(); if leading[comment_end..].contains('\n') { - newlines = true; + different_line = true; } } else if starts_with_single_line_comment || starts_with_block_comment { - newlines = true; + different_line = true; }; - (leading_trimmed.to_string(), newlines) + (leading_trimmed.to_string(), different_line) } pub(crate) fn trailing(&mut self, start: u32, end: u32, is_last: bool) -> String { @@ -177,3 +187,17 @@ pub(crate) fn find_comment_end(slice: &str, is_last: bool) -> usize { 0 } } + +fn comment_len(comment: &str) -> usize { + match comment { + "" => 0, + _ => { + let len = comment.trim().len(); + if len > 0 { + len + 6 + } else { + len + } + } + } +} diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index e2a20053a3f..f357fb2f12b 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -33,6 +33,13 @@ impl<'me> FmtVisitor<'me> { } } + fn shape(&self) -> Shape { + Shape { + width: self.config.max_width.saturating_sub(self.indent.width()), + indent: self.indent, + } + } + pub(crate) fn fork(&self) -> Self { Self { buffer: String::new(), @@ -151,12 +158,16 @@ impl<'me> FmtVisitor<'me> { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] struct Indent { block_indent: usize, } impl Indent { + fn width(&self) -> usize { + self.block_indent + } + fn block_indent(&mut self, config: &Config) { self.block_indent += config.tab_spaces; } @@ -174,3 +185,9 @@ impl Indent { " ".repeat(self.block_indent) } } + +#[derive(Clone, Copy, Debug)] +struct Shape { + width: usize, + indent: Indent, +} diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 6c56d96e7a6..8d996276a10 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -5,8 +5,11 @@ use noirc_frontend::{ ExpressionKind, Literal, Statement, UnaryOp, }; -use super::{FmtVisitor, Indent}; -use crate::utils::{self, Expr, FindToken}; +use super::{FmtVisitor, Shape}; +use crate::{ + utils::{self, Expr, FindToken}, + Config, +}; impl FmtVisitor<'_> { pub(crate) fn visit_expr(&mut self, expr: Expression) { @@ -56,7 +59,8 @@ impl FmtVisitor<'_> { } ExpressionKind::Call(call_expr) => { let span = call_expr.func.span.end()..span.end(); - let span = normalized_parenthesized_span(slice!(self, span.start, span.end), span); + let span = + skip_useless_tokens(Token::LeftParen, slice!(self, span.start, span.end), span); let callee = self.format_expr(*call_expr.func); let args = format_parens(self.fork(), false, call_expr.arguments, span); @@ -65,7 +69,8 @@ impl FmtVisitor<'_> { } ExpressionKind::MethodCall(method_call_expr) => { let span = method_call_expr.method_name.span().end()..span.end(); - let span = normalized_parenthesized_span(slice!(self, span.start, span.end), span); + let span = + skip_useless_tokens(Token::LeftParen, slice!(self, span.start, span.end), span); let object = self.format_expr(method_call_expr.object); let method = method_call_expr.method_name.to_string(); @@ -78,10 +83,18 @@ impl FmtVisitor<'_> { format!("{}.{}", lhs_str, member_access_expr.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) + let span = index_expr.collection.span.end()..span.end(); + + let span = skip_useless_tokens( + Token::LeftBracket, + slice!(self, span.start, span.end), + span, + ); + + let collection = self.format_expr(index_expr.collection); + let index = format_brackets(self.fork(), false, vec![index_expr.index], span); + + format!("{collection}{index}") } ExpressionKind::Tuple(exprs) => { format_parens(self.fork(), exprs.len() == 1, exprs, span) @@ -91,13 +104,13 @@ impl FmtVisitor<'_> { slice!(self, span.start(), span.end()).to_string() } Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => { - format!("[{}; {length}]", self.format_expr(*repeated_element)) + let repeated = self.format_expr(*repeated_element); + let length = self.format_expr(*length); + + format!("[{repeated}; {length}]") } - // 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(", ")) + format_brackets(self.fork(), false, exprs, span) } Literal::Unit => "()".to_string(), }, @@ -224,58 +237,103 @@ impl FmtVisitor<'_> { } } -fn format_parens( +fn format_expr_seq( + prefix: &str, + sufix: &str, mut visitor: FmtVisitor, trailing_comma: bool, exprs: Vec, span: Span, + limit: Option, ) -> String { visitor.indent.block_indent(visitor.config); - let nested_indent = visitor.indent; + let nested_indent = visitor.shape(); let exprs: Vec<_> = utils::Exprs::new(&visitor, span, exprs).collect(); - let (exprs, force_one_line) = format_exprs(trailing_comma, exprs, nested_indent); + let exprs = format_exprs(visitor.config, trailing_comma, exprs, nested_indent, limit); visitor.indent.block_unindent(visitor.config); - wrap_exprs(exprs, nested_indent, visitor.indent, force_one_line) + wrap_exprs(prefix, sufix, exprs, nested_indent, visitor.shape()) } -fn format_exprs(trailing_comma: bool, exprs: Vec, indent: Indent) -> (String, bool) { - let mut result = String::new(); +fn format_brackets( + visitor: FmtVisitor, + trailing_comma: bool, + exprs: Vec, + span: Span, +) -> String { + let array_width = visitor.config.array_width; + format_expr_seq("[", "]", visitor, trailing_comma, exprs, span, array_width.into()) +} + +fn format_parens( + visitor: FmtVisitor, + trailing_comma: bool, + exprs: Vec, + span: Span, +) -> String { + format_expr_seq("(", ")", visitor, trailing_comma, exprs, span, None) +} - let mut force_one_line = true; - let indent_str = indent.to_string(); +fn format_exprs( + config: &Config, + trailing_comma: bool, + exprs: Vec, + shape: Shape, + limit: Option, +) -> String { + let mut result = String::new(); + let indent_str = shape.indent.to_string(); - let tactic = Tactic::of(&exprs); + let tactic = Tactic::of(&exprs, config.short_array_element_width_threshold, limit); let mut exprs = exprs.into_iter().enumerate().peekable(); + let mut line_len = 0; + let mut prev_expr_trailing_comment = false; while let Some((index, expr)) = exprs.next() { let is_first = index == 0; let separate = exprs.peek().is_some() || trailing_comma; + let separate_len = usize::from(separate); match tactic { - Tactic::Vertical if !is_first && !expr.expr.is_empty() && !result.is_empty() => { + Tactic::Vertical if !is_first && !expr.value.is_empty() && !result.is_empty() => { result.push('\n'); result.push_str(&indent_str); } Tactic::Horizontal if !is_first => { result.push(' '); } + Tactic::Mixed => { + let total_width = expr.total_width() + separate_len; + + if line_len > 0 && line_len + 1 + total_width > shape.width + || prev_expr_trailing_comment + { + result.push('\n'); + result.push_str(&indent_str); + line_len = 0; + } else if line_len > 0 { + result.push(' '); + line_len += 1; + } + + line_len += total_width; + } _ => {} } result.push_str(&expr.leading); if expr.different_line { - force_one_line = false; result.push('\n'); result.push_str(&indent_str); + line_len = expr.value.chars().count(); } else if !expr.leading.is_empty() { result.push(' '); } - result.push_str(&expr.expr); + result.push_str(&expr.value); if tactic == Tactic::Horizontal { result.push_str(&expr.trailing); @@ -285,48 +343,83 @@ fn format_exprs(trailing_comma: bool, exprs: Vec, indent: Indent) -> (Stri result.push(','); } - if tactic == Tactic::Vertical { - if !expr.different_line { + if tactic != Tactic::Horizontal { + prev_expr_trailing_comment = !expr.trailing.is_empty(); + + if !expr.different_line && !expr.trailing.is_empty() { result.push(' '); } + result.push_str(&expr.trailing); } } - (result, force_one_line) + result } fn wrap_exprs( + prefix: &str, + sufix: &str, exprs: String, - nested_indent: Indent, - indent: Indent, - force_one_line: bool, + nested_shape: Shape, + shape: Shape, ) -> String { - if force_one_line && !exprs.contains('\n') { - format!("({exprs})") + let first_line_width = exprs.lines().next().map_or(0, |line| line.chars().count()); + + if first_line_width <= shape.width { + let allow_trailing_newline = exprs + .lines() + .last() + .unwrap_or_default() + .find_token_with(|token| matches!(token, Token::LineComment(_, _))) + .is_some(); + + let trailing_newline = if allow_trailing_newline { + shape.indent.to_string_with_newline() + } else { + String::new() + }; + + format!("{prefix}{exprs}{trailing_newline}{sufix}") } else { - let nested_indent_str = "\n".to_string() + &nested_indent.to_string(); - let indent_str = "\n".to_string() + &indent.to_string(); + let nested_indent_str = nested_shape.indent.to_string_with_newline(); + let indent_str = shape.indent.to_string(); - format!("({nested_indent_str}{exprs}{indent_str})") + format!("{prefix}{nested_indent_str}{exprs}{indent_str}{sufix}") } } -#[derive(PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] enum Tactic { Vertical, Horizontal, + Mixed, } impl Tactic { - fn of(exprs: &[Expr]) -> Self { - if exprs.iter().any(|item| { + fn of(exprs: &[Expr], max_width: usize, limit: Option) -> Self { + let mut tactic = if exprs.iter().any(|item| { has_single_line_comment(&item.leading) || has_single_line_comment(&item.trailing) }) { Tactic::Vertical } else { Tactic::Horizontal + }; + + if tactic == Tactic::Vertical && no_long_exprs(exprs, max_width) { + tactic = Tactic::Mixed; } + + if let Some(limit) = limit { + let total_width: usize = exprs.iter().map(|expr| expr.total_width()).sum(); + if total_width <= limit && !exprs.iter().any(|expr| expr.is_multiline()) { + tactic = Tactic::Horizontal; + } else { + tactic = Tactic::Mixed; + } + } + + tactic } } @@ -334,8 +427,12 @@ fn has_single_line_comment(slice: &str) -> bool { slice.trim_start().starts_with("//") } -fn normalized_parenthesized_span(slice: &str, mut span: Range) -> Span { - let offset = slice.find_token(Token::LeftParen).expect("parenthesized expression"); +fn skip_useless_tokens(token: Token, slice: &str, mut span: Range) -> Span { + let offset = slice.find_token(token).unwrap(); span.start += offset; span.into() } + +fn no_long_exprs(exprs: &[Expr], max_width: usize) -> bool { + exprs.iter().all(|expr| expr.value.len() <= max_width) +} diff --git a/tooling/nargo_fmt/src/visitor/stmt.rs b/tooling/nargo_fmt/src/visitor/stmt.rs index 973167fd19a..d7a43db1e00 100644 --- a/tooling/nargo_fmt/src/visitor/stmt.rs +++ b/tooling/nargo_fmt/src/visitor/stmt.rs @@ -9,6 +9,13 @@ impl super::FmtVisitor<'_> { self.visit_expr(expr); self.push_str(";"); } + StatementKind::Let(let_stmt) => { + let let_str = + slice!(self, span.start(), let_stmt.expression.span.start()).trim_end(); + let expr_str = self.format_expr(let_stmt.expression); + + self.push_rewrite(format!("{let_str} {expr_str};"), span); + } StatementKind::Error => unreachable!(), _ => self.format_missing(span.end()), } diff --git a/tooling/nargo_fmt/tests/expected/array.nr b/tooling/nargo_fmt/tests/expected/array.nr new file mode 100644 index 00000000000..c0ffa30a85e --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/array.nr @@ -0,0 +1,23 @@ +fn big_array() { + [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, + 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, + 10000000000000000, 100000000000000000, 1000000000000000000, 10000000000000000000, + 100000000000000000000, 1000000000000000000000, 10000000000000000000000, + 100000000000000000000000, 1000000000000000000000000]; + + [1, 10]; + + [// hello! + 1, 10]; + + [// hello! + 1, // asd + 10]; + + [// hello! + 1, // asd + 10// asdasd + ]; + + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]; +} diff --git a/tooling/nargo_fmt/tests/expected/call.nr b/tooling/nargo_fmt/tests/expected/call.nr index eeb2e386bc8..8a104b6d5e0 100644 --- a/tooling/nargo_fmt/tests/expected/call.nr +++ b/tooling/nargo_fmt/tests/expected/call.nr @@ -1,23 +1,19 @@ fn foo() { my_function(10, some_value, another_func(20, 30)); - outer_function( - some_function(), // Original inner function call + outer_function(some_function(), // Original inner function call another_function() // Original inner function call ); - outer_function( - some_function(), // Original inner function call + outer_function(some_function(), // Original inner function call another_function() // Original inner function call ); - my_function( - // Comment + my_function(// Comment some_value, /* Multiline Comment */ - another_func(20, 30) - ); + another_func(20, 30)); my_function(some_function(10, "arg1", another_function()), another_func(20, some_function(), 30)); diff --git a/tooling/nargo_fmt/tests/expected/index.nr b/tooling/nargo_fmt/tests/expected/index.nr index 79430e5c0e6..54f2ed2cf39 100644 --- a/tooling/nargo_fmt/tests/expected/index.nr +++ b/tooling/nargo_fmt/tests/expected/index.nr @@ -2,4 +2,8 @@ fn foo() { let arr = [10, 20, 30, 40]; arr[2]; arr[2]; + arr[/*test*/ 2]; + arr[2/*test*/]; + arr[// test + 2]; } diff --git a/tooling/nargo_fmt/tests/expected/let.nr b/tooling/nargo_fmt/tests/expected/let.nr new file mode 100644 index 00000000000..261982cf3e1 --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/let.nr @@ -0,0 +1,17 @@ +fn let_() { + let fn_call = my_function(some_function(10, "arg1", another_function()), another_func(20, some_function(), 30)); + let array = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]; + + let padded_sha256_hash: [u8; 259] = [// Padded hash + 209, 50, 135, 178, 4, 155, 190, 229, 228, 111, 61, 174, 8, 49, 48, 116, 90, 226, 77, 7, 111, + 27, 19, 113, 154, 48, 138, 136, 138, 15, 230, 132, 32, 4, 0, 5, 1, 2, 4, 3, 101, 1, 72, 134, + 96, 9, 6, 13, 48, 49, 48, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 0, + // Rest is padded with 0s until max bytes + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0]; +} diff --git a/tooling/nargo_fmt/tests/expected/literals.nr b/tooling/nargo_fmt/tests/expected/literals.nr index 4dc47bf7e2e..abe14c14965 100644 --- a/tooling/nargo_fmt/tests/expected/literals.nr +++ b/tooling/nargo_fmt/tests/expected/literals.nr @@ -5,6 +5,8 @@ fn main() { [0xff; 5]; + [0 as u8; MAX_BYTES]; + true; "hello world"; diff --git a/tooling/nargo_fmt/tests/expected/struct.nr b/tooling/nargo_fmt/tests/expected/struct.nr index 5e3530e8364..f662e5757a5 100644 --- a/tooling/nargo_fmt/tests/expected/struct.nr +++ b/tooling/nargo_fmt/tests/expected/struct.nr @@ -53,7 +53,7 @@ fn get_dog() -> Animal { } fn main(x: Field, y: Field) { - let first = Foo::default(x,y); + let first = Foo::default(x, y); let p = Pair { first, second: 1 }; assert(p.bar() == x); @@ -61,7 +61,7 @@ fn main(x: Field, y: Field) { assert(p.first.array[0] != p.first.array[1]); // Nested structs - let (struct_from_tuple, a_bool) = test_struct_in_tuple(true,x,y); + let (struct_from_tuple, a_bool) = test_struct_in_tuple(true, x, y); assert(struct_from_tuple.my_bool == true); assert(a_bool == true); assert(struct_from_tuple.my_int == 5); diff --git a/tooling/nargo_fmt/tests/expected/tuple.nr b/tooling/nargo_fmt/tests/expected/tuple.nr index 71f67c9f14f..b190a5f7c55 100644 --- a/tooling/nargo_fmt/tests/expected/tuple.nr +++ b/tooling/nargo_fmt/tests/expected/tuple.nr @@ -1,9 +1,7 @@ fn main() { (1,); - ( - // hello - 1, - ); + (// hello + 1,); (/*hello*/ 1,); (1/*hello*/,); (1,); @@ -16,32 +14,19 @@ fn main() { (1/*test*/,); - ( - // - 1, - ); + (// + 1,); - ( - // 1 - 1, - // 2, - 2 - ); + (// 1 + 1, // 2, + 2); (/*1*/ 1, /*2*/ 2); // FIXME: - ( - ( - ( - //2 - 1, - ), - ), - ); - ( - /*a*/ + (((//2 + 1,),),); + (/*a*/ 1/*b*/, -/*c*/ 2/*d*/, /*c*/ 2/*d*/, /*e*/ 3/*f*/ - ); +/*c*/ 2/*d*/, /*c*/ 2/*d*/, /*e*/ 3/*f*/); } diff --git a/tooling/nargo_fmt/tests/input/array.nr b/tooling/nargo_fmt/tests/input/array.nr new file mode 100644 index 00000000000..73651ef76bd --- /dev/null +++ b/tooling/nargo_fmt/tests/input/array.nr @@ -0,0 +1,39 @@ +fn big_array() { + [ + 1,10,100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000, + 10000000000000000000, + 100000000000000000000, + 1000000000000000000000, + 10000000000000000000000, + 100000000000000000000000, + 1000000000000000000000000, + ]; + + [ + 1, + 10, + ]; + + [ +// hello! +1, +10, + ]; + + [ +// hello! +1, +// asd +10, + ]; + + [ +// hello! +1, +// asd +10, +// asdasd + ]; + + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]; +} diff --git a/tooling/nargo_fmt/tests/input/index.nr b/tooling/nargo_fmt/tests/input/index.nr index e1c6fed02c1..7d10e897b8d 100644 --- a/tooling/nargo_fmt/tests/input/index.nr +++ b/tooling/nargo_fmt/tests/input/index.nr @@ -2,4 +2,9 @@ fn foo() { let arr = [10, 20, 30, 40]; arr [2]; arr [2]; + arr [/*test*/2]; + arr [2/*test*/]; + arr [ + // test + 2]; } \ No newline at end of file diff --git a/tooling/nargo_fmt/tests/input/let.nr b/tooling/nargo_fmt/tests/input/let.nr new file mode 100644 index 00000000000..d73a6450a33 --- /dev/null +++ b/tooling/nargo_fmt/tests/input/let.nr @@ -0,0 +1,15 @@ +fn let_() { + let fn_call = my_function(some_function( 10, "arg1", another_function() ),another_func (20, some_function() , 30 )); + let array = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]; + + let padded_sha256_hash: [u8; 259] = [ + // Padded hash + 209, 50, 135, 178, 4, 155, 190, 229, 228, 111, 61, 174, 8, 49, 48, 116, 90, 226, 77, 7, 111, 27, 19, 113, 154, 48, 138, 136, 138, 15, 230, 132, 32, 4, 0, 5, 1, 2, 4, 3, 101, 1, 72, 134, 96, 9, 6, 13, 48, 49, + 48, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 0, + // Rest is padded with 0s until max bytes + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + ]; +} diff --git a/tooling/nargo_fmt/tests/input/literals.nr b/tooling/nargo_fmt/tests/input/literals.nr index ec7659ac66f..3490c1e7d0d 100644 --- a/tooling/nargo_fmt/tests/input/literals.nr +++ b/tooling/nargo_fmt/tests/input/literals.nr @@ -7,6 +7,8 @@ fn main() { [0xff;5]; + [0 as u8; MAX_BYTES]; + true; "hello world";