diff --git a/tooling/nargo_fmt/src/rewrite.rs b/tooling/nargo_fmt/src/rewrite.rs index 99fd94ab1b1..5a9baf0aa05 100644 --- a/tooling/nargo_fmt/src/rewrite.rs +++ b/tooling/nargo_fmt/src/rewrite.rs @@ -4,6 +4,6 @@ mod infix; mod parenthesized; pub(crate) use array::rewrite as array; -pub(crate) use expr::{rewrite as expr, rewrite_subexpr as subexpr}; +pub(crate) use expr::{rewrite as expr, rewrite_sub_expr as sub_expr}; pub(crate) use infix::rewrite as infix; pub(crate) use parenthesized::rewrite as parenthesized; diff --git a/tooling/nargo_fmt/src/rewrite/array.rs b/tooling/nargo_fmt/src/rewrite/array.rs index 07210066920..f67ae5e75fe 100644 --- a/tooling/nargo_fmt/src/rewrite/array.rs +++ b/tooling/nargo_fmt/src/rewrite/array.rs @@ -26,7 +26,7 @@ pub(crate) fn rewrite(mut visitor: FmtVisitor, array: Vec, array_spa let end = item_span.start(); let leading = visitor.slice(start..end).trim_matches(pattern); - let item = super::subexpr(&visitor, item, visitor.shape()); + let item = super::sub_expr(&visitor, visitor.shape(), 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 diff --git a/tooling/nargo_fmt/src/rewrite/expr.rs b/tooling/nargo_fmt/src/rewrite/expr.rs index 04b605234bb..4d7279815df 100644 --- a/tooling/nargo_fmt/src/rewrite/expr.rs +++ b/tooling/nargo_fmt/src/rewrite/expr.rs @@ -5,10 +5,10 @@ use crate::visitor::{ ExpressionType, FmtVisitor, Indent, Shape, }; -pub(crate) fn rewrite_subexpr( +pub(crate) fn rewrite_sub_expr( visitor: &FmtVisitor, - expression: Expression, shape: Shape, + expression: Expression, ) -> String { rewrite(visitor, expression, ExpressionType::SubExpression, shape) } @@ -39,10 +39,10 @@ pub(crate) fn rewrite( } }; - format!("{op}{}", rewrite_subexpr(visitor, prefix.rhs, shape)) + format!("{op}{}", rewrite_sub_expr(visitor, shape, prefix.rhs)) } ExpressionKind::Cast(cast) => { - format!("{} as {}", rewrite_subexpr(visitor, cast.lhs, shape), cast.r#type) + format!("{} as {}", rewrite_sub_expr(visitor, shape, cast.lhs), cast.r#type) } kind @ ExpressionKind::Infix(_) => { super::infix(visitor.fork(), Expression { kind, span }, shape) @@ -51,7 +51,7 @@ pub(crate) fn rewrite( let args_span = visitor.span_before(call_expr.func.span.end()..span.end(), Token::LeftParen); - let callee = rewrite_subexpr(visitor, *call_expr.func, shape); + let callee = rewrite_sub_expr(visitor, shape, *call_expr.func); let args = format_parens( visitor.config.fn_call_width.into(), visitor.fork(), @@ -70,7 +70,7 @@ pub(crate) fn rewrite( Token::LeftParen, ); - let object = rewrite_subexpr(visitor, method_call_expr.object, shape); + let object = rewrite_sub_expr(visitor, shape, method_call_expr.object); let method = method_call_expr.method_name.to_string(); let args = format_parens( visitor.config.fn_call_width.into(), @@ -85,14 +85,14 @@ pub(crate) fn rewrite( format!("{object}.{method}{args}") } ExpressionKind::MemberAccess(member_access_expr) => { - let lhs_str = rewrite_subexpr(visitor, member_access_expr.lhs, shape); + let lhs_str = rewrite_sub_expr(visitor, shape, member_access_expr.lhs); format!("{}.{}", lhs_str, member_access_expr.rhs) } ExpressionKind::Index(index_expr) => { let index_span = visitor .span_before(index_expr.collection.span.end()..span.end(), Token::LeftBracket); - let collection = rewrite_subexpr(visitor, index_expr.collection, shape); + let collection = rewrite_sub_expr(visitor, shape, index_expr.collection); let index = format_brackets(visitor.fork(), false, vec![index_expr.index], index_span); format!("{collection}{index}") @@ -105,8 +105,8 @@ pub(crate) fn rewrite( visitor.slice(span).to_string() } Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => { - let repeated = rewrite_subexpr(visitor, *repeated_element, shape); - let length = rewrite_subexpr(visitor, *length, shape); + let repeated = rewrite_sub_expr(visitor, shape, *repeated_element); + let length = rewrite_sub_expr(visitor, shape, *length); format!("[{repeated}; {length}]") } @@ -115,7 +115,9 @@ pub(crate) fn rewrite( } Literal::Unit => "()".to_string(), }, - ExpressionKind::Parenthesized(sub_expr) => super::parenthesized(visitor, span, *sub_expr), + ExpressionKind::Parenthesized(sub_expr) => { + super::parenthesized(visitor, shape, span, *sub_expr) + } ExpressionKind::Constructor(constructor) => { let type_name = visitor.slice(span.start()..constructor.type_name.span().end()); let fields_span = visitor diff --git a/tooling/nargo_fmt/src/rewrite/infix.rs b/tooling/nargo_fmt/src/rewrite/infix.rs index 05f9eb413e3..15f5fe23aae 100644 --- a/tooling/nargo_fmt/src/rewrite/infix.rs +++ b/tooling/nargo_fmt/src/rewrite/infix.rs @@ -17,9 +17,9 @@ pub(crate) fn rewrite(visitor: FmtVisitor, expr: Expression, shape: Shape) -> St format!( "{} {} {}", - rewrite::subexpr(&visitor, infix.lhs, shape), + rewrite::sub_expr(&visitor, shape, infix.lhs), infix.operator.contents.as_string(), - rewrite::subexpr(&visitor, infix.rhs, shape) + rewrite::sub_expr(&visitor, shape, infix.rhs) ) } } @@ -88,10 +88,10 @@ pub(crate) fn flatten( } _ => { let rewrite = if result.is_empty() { - rewrite::subexpr(&visitor, node.clone(), visitor.shape()) + rewrite::sub_expr(&visitor, visitor.shape(), node.clone()) } else { visitor.indent.block_indent(visitor.config); - rewrite::subexpr(&visitor, node.clone(), visitor.shape()) + rewrite::sub_expr(&visitor, visitor.shape(), node.clone()) }; result.push(rewrite); diff --git a/tooling/nargo_fmt/src/rewrite/parenthesized.rs b/tooling/nargo_fmt/src/rewrite/parenthesized.rs index 46f224f3d6c..3926b52cb73 100644 --- a/tooling/nargo_fmt/src/rewrite/parenthesized.rs +++ b/tooling/nargo_fmt/src/rewrite/parenthesized.rs @@ -1,9 +1,10 @@ use noirc_frontend::{hir::resolution::errors::Span, Expression, ExpressionKind}; -use crate::visitor::FmtVisitor; +use crate::visitor::{FmtVisitor, Shape}; pub(crate) fn rewrite( visitor: &FmtVisitor<'_>, + shape: Shape, mut span: Span, mut sub_expr: Expression, ) -> String { @@ -31,7 +32,7 @@ pub(crate) fn rewrite( } if !leading.contains("//") && !trailing.contains("//") { - let sub_expr = super::subexpr(visitor, sub_expr, visitor.shape()); + let sub_expr = super::sub_expr(visitor, shape, sub_expr); format!("({leading}{sub_expr}{trailing})") } else { let mut visitor = visitor.fork(); @@ -40,7 +41,7 @@ pub(crate) fn rewrite( visitor.indent.block_indent(visitor.config); let nested_indent = visitor.indent.to_string_with_newline(); - let sub_expr = super::subexpr(&visitor, sub_expr, visitor.shape()); + let sub_expr = super::sub_expr(&visitor, shape, sub_expr); let mut result = String::new(); result.push('('); diff --git a/tooling/nargo_fmt/src/utils.rs b/tooling/nargo_fmt/src/utils.rs index 0387447e0ee..626795959a3 100644 --- a/tooling/nargo_fmt/src/utils.rs +++ b/tooling/nargo_fmt/src/utils.rs @@ -221,7 +221,7 @@ impl Item for Expression { } fn format(self, visitor: &FmtVisitor, shape: Shape) -> String { - rewrite::subexpr(visitor, self, shape) + rewrite::sub_expr(visitor, shape, self) } } @@ -235,7 +235,7 @@ impl Item for (Ident, Expression) { let (name, expr) = self; let name = name.0.contents; - let expr = rewrite::subexpr(visitor, expr, shape); + let expr = rewrite::sub_expr(visitor, shape, expr); if name == expr { name diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index f2226a2119b..a5e5a1c7846 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -19,8 +19,8 @@ impl FmtVisitor<'_> { } pub(crate) fn format_if(&self, if_expr: IfExpression) -> String { - let condition_str = rewrite::subexpr(self, if_expr.condition, self.shape()); - let consequence_str = rewrite::subexpr(self, if_expr.consequence, self.shape()); + let condition_str = rewrite::sub_expr(self, self.shape(), if_expr.condition); + let consequence_str = rewrite::sub_expr(self, self.shape(), if_expr.consequence); let mut result = format!("if {condition_str} {consequence_str}"); @@ -41,9 +41,9 @@ impl FmtVisitor<'_> { } pub(crate) fn format_if_single_line(&self, if_expr: IfExpression) -> Option { - let condition_str = rewrite::subexpr(self, if_expr.condition, self.shape()); + let condition_str = rewrite::sub_expr(self, self.shape(), if_expr.condition); let consequence_str = - rewrite::subexpr(self, extract_simple_expr(if_expr.consequence)?, self.shape()); + rewrite::sub_expr(self, self.shape(), extract_simple_expr(if_expr.consequence)?); let if_str = if let Some(alternative) = if_expr.alternative { let alternative_str = if let Some(ExpressionKind::If(_)) = diff --git a/tooling/nargo_fmt/src/visitor/stmt.rs b/tooling/nargo_fmt/src/visitor/stmt.rs index 599905feb9e..c27b7911d03 100644 --- a/tooling/nargo_fmt/src/visitor/stmt.rs +++ b/tooling/nargo_fmt/src/visitor/stmt.rs @@ -28,7 +28,7 @@ impl super::FmtVisitor<'_> { let let_str = self.slice(span.start()..let_stmt.expression.span.start()).trim_end(); - let expr_str = rewrite::subexpr(self, let_stmt.expression, self.shape()); + let expr_str = rewrite::sub_expr(self, self.shape(), let_stmt.expression); self.push_rewrite(format!("{let_str} {expr_str};"), span); } @@ -37,14 +37,14 @@ impl super::FmtVisitor<'_> { message.map_or(String::new(), |message| format!(", \"{message}\"")); let constrain = match kind { ConstrainKind::Assert => { - let assertion = rewrite::subexpr(self, expr, self.shape()); + let assertion = rewrite::sub_expr(self, self.shape(), expr); format!("assert({assertion}{message});") } ConstrainKind::AssertEq => { if let ExpressionKind::Infix(infix) = expr.kind { - let lhs = rewrite::subexpr(self, infix.lhs, self.shape()); - let rhs = rewrite::subexpr(self, infix.rhs, self.shape()); + let lhs = rewrite::sub_expr(self, self.shape(), infix.lhs); + let rhs = rewrite::sub_expr(self, self.shape(), infix.rhs); format!("assert_eq({lhs}, {rhs}{message});") } else { @@ -52,7 +52,7 @@ impl super::FmtVisitor<'_> { } } ConstrainKind::Constrain => { - let expr = rewrite::subexpr(self, expr, self.shape()); + let expr = rewrite::sub_expr(self, self.shape(), expr); format!("constrain {expr};") } }; @@ -64,12 +64,12 @@ impl super::FmtVisitor<'_> { let range = match for_stmt.range { ForRange::Range(start, end) => format!( "{}..{}", - rewrite::subexpr(self, start, self.shape()), - rewrite::subexpr(self, end, self.shape()) + rewrite::sub_expr(self, self.shape(), start), + rewrite::sub_expr(self, self.shape(), end) ), - ForRange::Array(array) => rewrite::subexpr(self, array, self.shape()), + ForRange::Array(array) => rewrite::sub_expr(self, self.shape(), array), }; - let block = rewrite::subexpr(self, for_stmt.block, self.shape()); + let block = rewrite::sub_expr(self, self.shape(), for_stmt.block); let result = format!("for {identifier} in {range} {block}"); self.push_rewrite(result, span);