Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add constructor formatter #3107

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ impl std::hash::Hash for Ident {

impl Display for Ident {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.contents.fmt(f)
let content = &self.0.contents;
let formatted_content = if content.starts_with("plain::") {
&content["plain::".len()..]
} else {
content
};
f.write_str(formatted_content)
}
}

Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use iter_extended::vecmap;
use noirc_frontend::{
hir::resolution::errors::Span, ArrayLiteral, BlockExpression, Expression, ExpressionKind,
Literal, Statement,
Expand Down Expand Up @@ -39,6 +40,15 @@ impl FmtVisitor<'_> {
self.format_expr(infix.rhs)
)
}
ExpressionKind::Constructor(constructor_expr) => {
let type_str = constructor_expr.type_name.to_string();
let formatted_fields =
vecmap(&constructor_expr.fields, |(field_ident, field_value)| {
format!("{}: {}", field_ident, self.format_expr(field_value.clone()))
})
.join(", ");
format!("{} {{ {} }}", type_str, formatted_fields)
}
ExpressionKind::Call(call_expr) => {
let formatted_func = self.format_expr(*call_expr.func);
let formatted_args = call_expr
Expand Down
8 changes: 8 additions & 0 deletions tooling/nargo_fmt/tests/expected/constructor.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Point {
x: u8,
y: u8,
}

fn foo() {
Point { x: 5, y: 10 };
}
9 changes: 9 additions & 0 deletions tooling/nargo_fmt/tests/input/constructor.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct Point {
x: u8,
y: u8,
}

fn foo() {
Point{x :5,
y: 10 };
}
Loading