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 undesirable line break before '{' in function signature with comments #3473

Merged
merged 1 commit into from Nov 15, 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
4 changes: 4 additions & 0 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
let starts_with_single_line_comment = leading_trimmed.starts_with("//");

if ends_with_block_comment {
let comment_end = leading_trimmed.rfind(|c| c == '/').unwrap();

Check warning on line 92 in tooling/nargo_fmt/src/utils.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (rfind)

if leading[comment_end..].contains('\n') {
different_line = true;
Expand Down Expand Up @@ -190,7 +190,7 @@
}

pub(crate) fn count_newlines(slice: &str) -> usize {
bytecount::count(slice.as_bytes(), b'\n')

Check warning on line 193 in tooling/nargo_fmt/src/utils.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (bytecount)
}

pub(crate) trait Item {
Expand Down Expand Up @@ -244,3 +244,7 @@
pub(crate) fn is_single_line(s: &str) -> bool {
!s.chars().any(|c| c == '\n')
}

pub(crate) fn last_line_contains_single_line_comment(s: &str) -> bool {
s.lines().last().map_or(false, |line| line.contains("//"))
}
20 changes: 18 additions & 2 deletions tooling/nargo_fmt/src/visitor/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@ use noirc_frontend::{
NoirFunction, ParsedModule,
};

use crate::utils::last_line_contains_single_line_comment;

impl super::FmtVisitor<'_> {
fn format_fn_before_block(&self, func: NoirFunction, start: u32) -> (String, bool) {
let slice = self.slice(start..func.span().start());
let force_brace_newline = slice.contains("//");
(slice.trim_end().to_string(), force_brace_newline)

let params_open = self
.span_before(func.name_ident().span().end()..func.span().start(), Token::LeftParen)
.start();

let last_span = if func.parameters().is_empty() {
params_open..func.span().start()
} else {
func.parameters().last().unwrap().1.span.unwrap().end()..func.span().start()
};

let params_end = self.span_after(last_span, Token::RightParen).start();

let maybe_comment = self.slice(params_end..func.span().start());

(slice.trim_end().to_string(), last_line_contains_single_line_comment(maybe_comment))
}

pub(crate) fn visit_file(&mut self, module: ParsedModule) {
Expand Down
13 changes: 13 additions & 0 deletions tooling/nargo_fmt/tests/expected/fn.nr
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
fn main(x: pub u8, y: u8) {}

fn main()
// hello
{}

fn main(
// hello
) {}

fn main(
// hello
unit: ()
) {}
13 changes: 13 additions & 0 deletions tooling/nargo_fmt/tests/input/fn.nr
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
fn main(x: pub u8, y: u8) {}

fn main()
// hello
{}

fn main(
// hello
) {}

fn main(
// hello
unit: ()
) {}
Loading