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: fix empty constructor formatting #3265

Merged
merged 1 commit into from Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@
}

#[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 Expand Up @@ -131,7 +135,7 @@
let newline_upper_bound = 2;
let newline_lower_bound = 1;

let mut newline_count = bytecount::count(slice.as_bytes(), b'\n');

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bytecount)
let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count();

if newline_count + offset > newline_upper_bound {
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,13 +15,12 @@
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();
}

pub(crate) fn format_subexpr(&self, expression: Expression) -> String {

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
self.format_expr(expression, ExpressionType::SubExpression)
}

Expand Down Expand Up @@ -50,17 +49,17 @@
}
};

format!("{op}{}", self.format_subexpr(prefix.rhs))

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
}
ExpressionKind::Cast(cast) => {
format!("{} as {}", self.format_subexpr(cast.lhs), cast.r#type)

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
}
ExpressionKind::Infix(infix) => {
format!(
"{} {} {}",
self.format_subexpr(infix.lhs),

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
infix.operator.contents.as_string(),
self.format_subexpr(infix.rhs)

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

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
)
}
ExpressionKind::Call(call_expr) => {
Expand Down Expand Up @@ -174,7 +173,7 @@
}
}
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 @@
) -> 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 @@
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 Expand Up @@ -314,7 +316,7 @@
if let Some(first_stmt) = block.first() {
let slice = self.slice(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 319 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
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
};
}
Loading