Skip to content

Commit

Permalink
chore: fix empty constructor formatting (#3265)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored and guipublic committed Oct 30, 2023
1 parent 66a8453 commit 3f4f2ee
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
10 changes: 7 additions & 3 deletions tooling/nargo_fmt/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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} ")
}
Expand Down
7 changes: 7 additions & 0 deletions tooling/nargo_fmt/tests/expected/let.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
7 changes: 7 additions & 0 deletions tooling/nargo_fmt/tests/input/let.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

0 comments on commit 3f4f2ee

Please sign in to comment.