Skip to content

Commit

Permalink
chore: support directives to control whether noirfmt runs on a sectio…
Browse files Browse the repository at this point in the history
…n of code (#3468)
  • Loading branch information
kek kek kek authored Nov 9, 2023
1 parent 1647e33 commit 8974ace
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
34 changes: 32 additions & 2 deletions tooling/nargo_fmt/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};

pub(crate) struct FmtVisitor<'me> {
ignore_next_node: bool,
pub(crate) config: &'me Config,
buffer: String,
pub(crate) source: &'me str,
Expand All @@ -20,6 +21,7 @@ pub(crate) struct FmtVisitor<'me> {
impl<'me> FmtVisitor<'me> {
pub(crate) fn new(source: &'me str, config: &'me Config) -> Self {
Self {
ignore_next_node: false,
buffer: String::new(),
config,
source,
Expand Down Expand Up @@ -61,6 +63,7 @@ impl<'me> FmtVisitor<'me> {
pub(crate) fn fork(&self) -> Self {
Self {
buffer: String::new(),
ignore_next_node: self.ignore_next_node,
config: self.config,
source: self.source,
last_position: self.last_position,
Expand All @@ -77,6 +80,24 @@ impl<'me> FmtVisitor<'me> {
}

fn push_str(&mut self, s: &str) {
let comments = Lexer::new(s).skip_comments(false).flatten().flat_map(|token| {
if let Token::LineComment(content, _) | Token::BlockComment(content, _) =
token.into_token()
{
let content = content.trim();
content.strip_prefix("noir-fmt:").map(ToOwned::to_owned)
} else {
None
}
});

for comment in comments {
match comment.as_str() {
"ignore" => self.ignore_next_node = true,
this => unreachable!("unknown settings {this}"),
}
}

self.buffer.push_str(s);
}

Expand All @@ -89,10 +110,19 @@ impl<'me> FmtVisitor<'me> {
panic!("not formatted because a comment would be lost: {rewrite:?}");
}

let rewrite = if changed_comment_content { original.to_string() } else { rewrite };

self.format_missing_indent(span.start(), true);

let rewrite = if changed_comment_content || std::mem::take(&mut self.ignore_next_node) {
original.to_string()
} else {
rewrite
};

self.push_str(&rewrite);

if rewrite.starts_with('{') && rewrite.ends_with('}') {
self.ignore_next_node = false;
}
}

#[track_caller]
Expand Down
19 changes: 15 additions & 4 deletions tooling/nargo_fmt/src/visitor/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ impl super::FmtVisitor<'_> {
for Item { kind, span } in module.items {
match kind {
ItemKind::Function(func) => {
self.format_missing_indent(span.start(), true);

if std::mem::take(&mut self.ignore_next_node) {
self.push_str(self.slice(span));
self.last_position = span.end();
continue;
}

let (fn_before_block, force_brace_newline) =
self.format_fn_before_block(func.clone(), span.start());

self.format_missing_indent(span.start(), true);

self.push_str(&fn_before_block);
self.push_str(if force_brace_newline { "\n" } else { " " });

self.visit_block(func.def.body, func.def.span);
}
ItemKind::Submodules(module) => {
let name = module.name;

self.format_missing_indent(span.start(), true);

if std::mem::take(&mut self.ignore_next_node) {
self.push_str(self.slice(span));
self.last_position = span.end();
continue;
}

let name = module.name;
let after_brace = self.span_after(span, Token::LeftBrace).start();
self.last_position = after_brace;

Expand Down
26 changes: 26 additions & 0 deletions tooling/nargo_fmt/tests/expected/ignore.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn main() {
// noir-fmt:ignore
assert( x != y );
assert(x != y);
{
// noir-fmt:ignore
};
assert(x != y);
}
// noir-fmt:ignore
fn main() {
1;
2;
3;
}
// noir-fmt:ignore
mod items {
fn hello() {}
}

fn mk_array() {
// noir-fmt:ignore
let array = [1,
];
let array = [1];
}
27 changes: 27 additions & 0 deletions tooling/nargo_fmt/tests/input/ignore.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
fn main() {
// noir-fmt:ignore
assert( x != y );
assert( x != y );
{
// noir-fmt:ignore
};
assert( x != y );
}
// noir-fmt:ignore
fn main() {
1;
2;
3;
}
// noir-fmt:ignore
mod items {
fn hello() {}
}

fn mk_array() {
// noir-fmt:ignore
let array = [1,
];
let array = [1,
];
}

0 comments on commit 8974ace

Please sign in to comment.