diff --git a/Cargo.lock b/Cargo.lock index 6051d356fca..e31cee13364 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2388,6 +2388,7 @@ name = "nargo_fmt" version = "0.16.0" dependencies = [ "bytecount", + "iter-extended", "noirc_frontend", "serde", "similar-asserts", diff --git a/tooling/nargo_fmt/Cargo.toml b/tooling/nargo_fmt/Cargo.toml index 921c9893ab5..b33092bf868 100644 --- a/tooling/nargo_fmt/Cargo.toml +++ b/tooling/nargo_fmt/Cargo.toml @@ -10,6 +10,7 @@ bytecount = "0.6.3" noirc_frontend.workspace = true serde.workspace = true toml.workspace = true +iter-extended.workspace = true thiserror.workspace = true [dev-dependencies] diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 2e5916acae6..839298fe89e 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -1,3 +1,4 @@ +use iter_extended::vecmap; use noirc_frontend::{ hir::resolution::errors::Span, ArrayLiteral, BlockExpression, Expression, ExpressionKind, Literal, Statement, @@ -26,15 +27,22 @@ impl FmtVisitor<'_> { visitor.buffer } ExpressionKind::Lambda(lambda) => { - let formatted_params = lambda - .parameters - .iter() - .map(|(pattern, unresolved_type)| format!("{}: {}", pattern, unresolved_type)) - .collect::>() - .join(", "); - let formatted_body = self.format_expr(lambda.body.clone()); - format!("|{}| -> {}", formatted_params, formatted_body) - } + let formatted_params = vecmap(&lambda.parameters, |(pattern, unresolved_type)| { + // Convert to string and then trim + let pattern_str = pattern.to_string(); + let pattern_str = pattern_str.trim().to_string(); + + let type_str = unresolved_type.to_string(); + let type_str = type_str.trim().to_string(); + + format!("{}: {}", pattern_str, type_str) + }).join(", "); + + let formatted_body_str = self.format_expr(lambda.body.clone()); + let formatted_body = formatted_body_str.trim().to_string(); + + format!("|{}| {}", formatted_params, formatted_body) + } ExpressionKind::Prefix(prefix) => { format!("{}{}", prefix.operator, self.format_expr(prefix.rhs)) }