Skip to content

Commit

Permalink
chore(formatter): trim spaces after opening brace (noir-lang#3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored and Sakapoi committed Oct 19, 2023
1 parent de73069 commit 01719c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
15 changes: 14 additions & 1 deletion tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use noirc_frontend::{hir::resolution::errors::Span, BlockExpression, Expression, ExpressionKind};
use noirc_frontend::{
hir::resolution::errors::Span, BlockExpression, Expression, ExpressionKind, Statement,
};

use super::FmtVisitor;

Expand Down Expand Up @@ -52,6 +54,8 @@ impl FmtVisitor<'_> {
self.last_position = block_span.start() + 1; // `{`
self.push_str("{");

self.trim_spaces_after_opening_brace(&block.0);

self.with_indent(|this| {
this.visit_stmts(block.0);
});
Expand All @@ -68,6 +72,15 @@ impl FmtVisitor<'_> {
self.push_str("}");
}

fn trim_spaces_after_opening_brace(&mut self, block: &[Statement]) {
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');
self.last_position += len.unwrap_or(0) as u32;
}
}

fn visit_empty_block(&mut self, block_span: Span, should_indent: bool) {
let slice = slice!(self, block_span.start(), block_span.end());
let comment_str = slice[1..slice.len() - 1].trim();
Expand Down
9 changes: 9 additions & 0 deletions tooling/nargo_fmt/tests/expected/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ fn commnet() {
1
//
}

fn test() {
34
}

fn test() {
// 324
34
}
20 changes: 19 additions & 1 deletion tooling/nargo_fmt/tests/input/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ fn only_comments() {
fn commnet() {
1
//
}
}

fn test() {





34
}

fn test() {




// 324
34
}

0 comments on commit 01719c9

Please sign in to comment.