From 3f4f2eed604c5c8ffd0ff617891a7b8e926fed6b Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Tue, 24 Oct 2023 08:24:46 -0700 Subject: [PATCH] chore: fix empty constructor formatting (#3265) --- tooling/nargo_fmt/src/visitor.rs | 10 +++++++--- tooling/nargo_fmt/src/visitor/expr.rs | 6 ++++-- tooling/nargo_fmt/tests/expected/let.nr | 7 +++++++ tooling/nargo_fmt/tests/input/let.nr | 7 +++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/tooling/nargo_fmt/src/visitor.rs b/tooling/nargo_fmt/src/visitor.rs index d1c87d311e7..bc769e3f298 100644 --- a/tooling/nargo_fmt/src/visitor.rs +++ b/tooling/nargo_fmt/src/visitor.rs @@ -4,7 +4,10 @@ mod stmt; use noirc_frontend::{hir::resolution::errors::Span, token::Token}; -use crate::{config::Config, utils::FindToken}; +use crate::{ + config::Config, + utils::{self, FindToken}, +}; pub(crate) struct FmtVisitor<'me> { config: &'me Config, @@ -76,9 +79,10 @@ impl<'me> FmtVisitor<'me> { } #[track_caller] - fn push_rewrite(&mut self, s: String, span: Span) { + fn push_rewrite(&mut self, rewrite: String, span: Span) { + let rewrite = utils::recover_comment_removed(self.slice(span), rewrite); self.format_missing_indent(span.start(), true); - self.push_str(&s); + self.push_str(&rewrite); } fn format_missing(&mut self, end: u32) { diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index dcf4dbbd3b1..66d586888a6 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -15,7 +15,6 @@ impl FmtVisitor<'_> { let span = expr.span; let rewrite = self.format_expr(expr, expr_type); - let rewrite = utils::recover_comment_removed(self.slice(span), rewrite); self.push_rewrite(rewrite, span); self.last_position = span.end(); @@ -174,7 +173,7 @@ impl FmtVisitor<'_> { } } ExpressionKind::Constructor(constructor) => { - let type_name = self.slice(constructor.type_name.span()); + let type_name = self.slice(span.start()..constructor.type_name.span().end()); let fields_span = self .span_before(constructor.type_name.span().end()..span.end(), Token::LeftBrace); @@ -248,6 +247,7 @@ impl FmtVisitor<'_> { ) -> String { let fields = { let mut visitor = self.fork(); + let is_unit_struct = constructor.fields.is_empty(); visitor.indent.block_indent(visitor.config); @@ -270,6 +270,8 @@ impl FmtVisitor<'_> { nested_indent.indent.to_string_with_newline(), visitor.shape().indent.to_string_with_newline() ) + } else if is_unit_struct { + exprs } else { format!(" {exprs} ") } diff --git a/tooling/nargo_fmt/tests/expected/let.nr b/tooling/nargo_fmt/tests/expected/let.nr index c53f43c7e65..dccdb76328c 100644 --- a/tooling/nargo_fmt/tests/expected/let.nr +++ b/tooling/nargo_fmt/tests/expected/let.nr @@ -48,4 +48,11 @@ fn let_() { }; let expr = Expr { /*A boolean literal (true, false).*/ kind: ExprKind::Bool(true) }; + + let mut V = dep::crate2::MyStruct { Q: x }; + let mut V = dep::crate2::MyStruct {}; + let mut V = dep::crate2::MyStruct {/*test*/}; + let mut V = dep::crate2::MyStruct { + // sad + }; } diff --git a/tooling/nargo_fmt/tests/input/let.nr b/tooling/nargo_fmt/tests/input/let.nr index c603d44a4ad..ae23f0150d9 100644 --- a/tooling/nargo_fmt/tests/input/let.nr +++ b/tooling/nargo_fmt/tests/input/let.nr @@ -24,4 +24,11 @@ kind: ExprKind::Bool(true), }; let expr = Expr {/*A boolean literal (true, false).*/kind: ExprKind::Bool(true),}; + + let mut V = dep::crate2::MyStruct { Q: x }; + let mut V = dep::crate2::MyStruct {}; + let mut V = dep::crate2::MyStruct {/*test*/}; + let mut V = dep::crate2::MyStruct { + // sad + }; }