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
12 changes: 12 additions & 0 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
self.format_expr(infix.rhs)
)
}
ExpressionKind::Constructor(constructor_expr) => {
let type_str = constructor_expr.type_name.to_string();
let formatted_fields = constructor_expr
.fields
.iter()
.map(|(field_ident, field_value)| {
format!("{}: {}", field_ident, self.format_expr(field_value.clone()))
})
.collect::<Vec<_>>()
jonybur marked this conversation as resolved.
Show resolved Hide resolved
.join(", ");
format!("{} {{ {} }}", type_str, formatted_fields)
}
ExpressionKind::Literal(literal) => match literal {
Literal::Integer(_) => slice!(self, span.start(), span.end()).to_string(),
Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => {
Expand Down Expand Up @@ -96,7 +108,7 @@
if let Some(first_stmt) = block.first() {
let slice = slice!(self, self.last_position, first_stmt.span.start());
let len =
slice.chars().take_while(|ch| ch.is_whitespace()).collect::<String>().rfind('\n');

Check warning on line 111 in tooling/nargo_fmt/src/visitor/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (rfind)
self.last_position += len.unwrap_or(0) as u32;
}
}
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: i32,
y: i32,
}

fn foo() {
Point { x: 5, y: 10 };
}
8 changes: 8 additions & 0 deletions tooling/nargo_fmt/tests/input/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 };
}
jonybur marked this conversation as resolved.
Show resolved Hide resolved
Loading