From 7bada0455d9b9388867b7433d4b2f9a9a31579f4 Mon Sep 17 00:00:00 2001 From: f01dab1e Date: Tue, 14 Nov 2023 09:50:06 +0000 Subject: [PATCH 1/6] chore: update array formatting style --- tooling/nargo_fmt/src/rewrite.rs | 2 + tooling/nargo_fmt/src/rewrite/array.rs | 85 +++++++++++++++++++++++ tooling/nargo_fmt/src/visitor.rs | 25 +++++-- tooling/nargo_fmt/src/visitor/expr.rs | 12 ++-- tooling/nargo_fmt/tests/expected/array.nr | 54 +++++++------- tooling/nargo_fmt/tests/expected/let.nr | 26 +++---- 6 files changed, 148 insertions(+), 56 deletions(-) create mode 100644 tooling/nargo_fmt/src/rewrite/array.rs diff --git a/tooling/nargo_fmt/src/rewrite.rs b/tooling/nargo_fmt/src/rewrite.rs index 15ef3a1af33..c105e98bf15 100644 --- a/tooling/nargo_fmt/src/rewrite.rs +++ b/tooling/nargo_fmt/src/rewrite.rs @@ -1,3 +1,5 @@ +mod array; mod infix; pub(crate) use infix::rewrite as infix; +pub(crate) use array::rewrite as array; diff --git a/tooling/nargo_fmt/src/rewrite/array.rs b/tooling/nargo_fmt/src/rewrite/array.rs new file mode 100644 index 00000000000..dde2cbb1ec7 --- /dev/null +++ b/tooling/nargo_fmt/src/rewrite/array.rs @@ -0,0 +1,85 @@ +use noirc_frontend::{hir::resolution::errors::Span, token::Token, Expression}; + +use crate::{ + utils::{Expr, FindToken}, + visitor::FmtVisitor, +}; + +pub(crate) fn rewrite(mut visitor: FmtVisitor, array: Vec, array_span: Span) -> String { + let pattern: &[_] = &[' ', '\t']; + + visitor.indent.block_indent(visitor.config); + let nested_indent = visitor.shape(); + + let indent_str = nested_indent.indent.to_string(); + + let mut last_position = array_span.start() + 1; + let end_position = array_span.end() - 1; + + let mut items = array.into_iter().peekable(); + + let mut result = Vec::new(); + while let Some(item) = items.next() { + let item_span = item.span; + + let start: u32 = last_position; + let end = item_span.start(); + + let leading = visitor.slice(start..end).trim_matches(pattern); + let item = visitor.format_sub_expr(item); + let next_start = items.peek().map_or(end_position, |expr| expr.span.start()); + let trailing = visitor.slice(item_span.end()..next_start); + let offset = trailing + .find_token(Token::Comma) + .map(|span| span.end() as usize) + .unwrap_or(trailing.len()); + let trailing = trailing[..offset].trim_end_matches(',').trim_matches(pattern); + last_position = item_span.end() + offset as u32; + + let (leading, _) = visitor.format_comment_in_block(leading, 0, false); + let (trailing, _) = visitor.format_comment_in_block(trailing, 0, false); + + result.push(Expr { leading, value: item, trailing, different_line: false }); + } + + let slice = visitor.slice(last_position..end_position); + let (comment, _) = visitor.format_comment_in_block(slice, 0, false); + result.push(Expr { + leading: "".into(), + value: "".into(), + trailing: comment, + different_line: false, + }); + + visitor.indent.block_unindent(visitor.config); + + let mut items_str = String::new(); + let mut items = result.into_iter().peekable(); + while let Some(next) = items.next() { + items_str.push_str(&next.leading); + if next.leading.contains('\n') && !next.value.is_empty() { + items_str.push_str(&indent_str); + } + items_str.push_str(&next.value); + items_str.push_str(&next.trailing); + + if let Some(item) = items.peek() { + if !item.value.is_empty() { + items_str.push(','); + } + + if !item.leading.contains('\n') && !next.value.is_empty() { + items_str.push(' '); + } + } + } + + crate::visitor::expr::wrap_exprs( + "[", + "]", + items_str.trim().into(), + nested_indent, + visitor.shape(), + true, + ) +} diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index 2374fe2ef68..96b2c383e0f 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -1,4 +1,4 @@ -mod expr; +pub(crate) mod expr; mod item; mod stmt; @@ -53,7 +53,7 @@ impl<'me> FmtVisitor<'me> { (span.start() + offset..span.end()).into() } - fn shape(&self) -> Shape { + pub(crate) fn shape(&self) -> Shape { Shape { width: self.config.max_width.saturating_sub(self.indent.width()), indent: self.indent, @@ -106,7 +106,7 @@ impl<'me> FmtVisitor<'me> { let original = self.slice(span); let changed_comment_content = utils::changed_comment_content(original, &rewrite); - if changed_comment_content && self.config.error_on_lost_comment { + if changed_comment_content && false { panic!("not formatted because a comment would be lost: {rewrite:?}"); } @@ -163,7 +163,7 @@ impl<'me> FmtVisitor<'me> { self.push_vertical_spaces(slice); process_last_slice(self, "", slice); } else { - let (result, last_end) = self.format_comment_in_block(slice, start); + let (result, last_end) = self.format_comment_in_block(slice, start, true); if result.trim().is_empty() { process_last_slice(self, slice, slice); } else { @@ -174,7 +174,12 @@ impl<'me> FmtVisitor<'me> { } } - fn format_comment_in_block(&mut self, slice: &str, start: u32) -> (String, u32) { + pub(crate) fn format_comment_in_block( + &mut self, + slice: &str, + start: u32, + fix_indent: bool, + ) -> (String, u32) { let mut result = String::new(); let mut last_end = 0; @@ -185,12 +190,14 @@ impl<'me> FmtVisitor<'me> { let diff = start; let big_snippet = &self.source[..(span.start() + diff) as usize]; let last_char = big_snippet.chars().rev().find(|rev_c| ![' ', '\t'].contains(rev_c)); - let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c)); + let fix_indent = + fix_indent && last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c)); if let Token::LineComment(_, _) | Token::BlockComment(_, _) = comment.into_token() { let starts_with_newline = slice.starts_with('\n'); let comment = &slice[span.start() as usize..span.end() as usize]; + dbg!(fix_indent); if fix_indent { if let Some('{') = last_char { result.push('\n'); @@ -205,12 +212,16 @@ impl<'me> FmtVisitor<'me> { } else { match (starts_with_newline, self.at_start()) { (false, false) => { + dbg!(()); result.push(' '); } (true, _) => { + dbg!(()); result.push_str(&self.indent.to_string_with_newline()); } - _ => {} + (false, _) => { + result.push(' '); + } }; } diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index d8e0c6d9d31..a5c6ceae168 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -105,7 +105,7 @@ impl FmtVisitor<'_> { format!("[{repeated}; {length}]") } Literal::Array(ArrayLiteral::Standard(exprs)) => { - format_brackets(self.fork(), false, exprs, span) + rewrite::array(self.fork(), exprs, span) } Literal::Unit => "()".to_string(), }, @@ -370,7 +370,7 @@ fn format_expr_seq( visitor.indent.block_unindent(visitor.config); - wrap_exprs(prefix, suffix, exprs, nested_indent, visitor.shape()) + wrap_exprs(prefix, suffix, exprs, nested_indent, visitor.shape(), false) } fn format_brackets( @@ -483,16 +483,20 @@ fn format_exprs( result } -fn wrap_exprs( +pub(crate) fn wrap_exprs( prefix: &str, suffix: &str, exprs: String, nested_shape: Shape, shape: Shape, + array: bool, ) -> String { let first_line_width = first_line_width(&exprs); - if first_line_width <= shape.width { + let force_one_line = + if array { !exprs.contains('\n') } else { first_line_width <= shape.width }; + + if force_one_line { let allow_trailing_newline = exprs .lines() .last() diff --git a/tooling/nargo_fmt/tests/expected/array.nr b/tooling/nargo_fmt/tests/expected/array.nr index fdf81d3595c..3341afb31a5 100644 --- a/tooling/nargo_fmt/tests/expected/array.nr +++ b/tooling/nargo_fmt/tests/expected/array.nr @@ -1,42 +1,38 @@ fn big_array() { - [1, - 10, - 100, - 1000, - 10000, - 100000, - 1000000, - 10000000, - 100000000, - 1000000000, - 10000000000, - 100000000000, - 1000000000000, - 10000000000000, - 100000000000000, - 1000000000000000, - 10000000000000000, - 100000000000000000, - 1000000000000000000, + [ + 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]; + 1000000000000000000000000 + ]; - [1, 10]; + [ + 1, + 10 + ]; - [// hello! - 1, 10]; + [ + // hello! + 1, + 10 + ]; - [// hello! - 1, // asd - 10]; + [ + // hello! + 1, + // asd + 10 + ]; - [// hello! - 1, // asd - 10// asdasd + [ + // 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/let.nr b/tooling/nargo_fmt/tests/expected/let.nr index 1c7afc8df5f..e79e308410b 100644 --- a/tooling/nargo_fmt/tests/expected/let.nr +++ b/tooling/nargo_fmt/tests/expected/let.nr @@ -3,24 +3,18 @@ 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, + 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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0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ] - }; + let a = BigUint56 { limbs: [1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }; let person = Person { first_name: "John", From db23a629ee9408572c732ebddf3df504d6de528d Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Tue, 14 Nov 2023 01:52:03 -0800 Subject: [PATCH 2/6] remove dbg! --- tooling/nargo_fmt/src/visitor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index 96b2c383e0f..1cc2b68137b 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -197,7 +197,6 @@ impl<'me> FmtVisitor<'me> { let starts_with_newline = slice.starts_with('\n'); let comment = &slice[span.start() as usize..span.end() as usize]; - dbg!(fix_indent); if fix_indent { if let Some('{') = last_char { result.push('\n'); From f65dfd0ec3f45e2344e067bba280e01f395300d3 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Tue, 14 Nov 2023 01:52:49 -0800 Subject: [PATCH 3/6] remove dbg! --- tooling/nargo_fmt/src/visitor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index 1cc2b68137b..c4da23493f2 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -215,7 +215,6 @@ impl<'me> FmtVisitor<'me> { result.push(' '); } (true, _) => { - dbg!(()); result.push_str(&self.indent.to_string_with_newline()); } (false, _) => { From 1e9e47668c12fee276fcaf0e3fcf0354c529add7 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Tue, 14 Nov 2023 01:52:56 -0800 Subject: [PATCH 4/6] remove dbg! --- tooling/nargo_fmt/src/visitor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index c4da23493f2..5a42bc15cb8 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -211,7 +211,6 @@ impl<'me> FmtVisitor<'me> { } else { match (starts_with_newline, self.at_start()) { (false, false) => { - dbg!(()); result.push(' '); } (true, _) => { From fb2c74b8ec9dce76f67c30900aeba716c79dd7f5 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Tue, 14 Nov 2023 02:04:57 -0800 Subject: [PATCH 5/6] Update tooling/nargo_fmt/src/visitor.rs --- tooling/nargo_fmt/src/visitor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index 5a42bc15cb8..017d8406116 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -106,7 +106,7 @@ impl<'me> FmtVisitor<'me> { let original = self.slice(span); let changed_comment_content = utils::changed_comment_content(original, &rewrite); - if changed_comment_content && false { + if changed_comment_content && self.config.error_on_lost_comment { panic!("not formatted because a comment would be lost: {rewrite:?}"); } From ce15f29e99e71fca5b31f53f3c71bfa25d2af851 Mon Sep 17 00:00:00 2001 From: f01dab1e Date: Tue, 14 Nov 2023 10:12:18 +0000 Subject: [PATCH 6/6] cargo fmt --- tooling/nargo_fmt/src/rewrite.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/nargo_fmt/src/rewrite.rs b/tooling/nargo_fmt/src/rewrite.rs index c105e98bf15..c1ac585985e 100644 --- a/tooling/nargo_fmt/src/rewrite.rs +++ b/tooling/nargo_fmt/src/rewrite.rs @@ -1,5 +1,5 @@ mod array; mod infix; -pub(crate) use infix::rewrite as infix; pub(crate) use array::rewrite as array; +pub(crate) use infix::rewrite as infix;