Skip to content

Commit

Permalink
chore: Fix undesirable line break before '{' in function signature wi…
Browse files Browse the repository at this point in the history
…th comments (#3473)
  • Loading branch information
kek kek kek authored Nov 15, 2023
1 parent fc3edf7 commit fc84894
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,7 @@ pub(crate) fn first_line_width(exprs: &str) -> usize {
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: ()
) {}

0 comments on commit fc84894

Please sign in to comment.