Skip to content

Commit

Permalink
chore: format array and index expressions (noir-lang#3223)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored Oct 21, 2023
1 parent 86704ba commit 7de37c6
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 85 deletions.
3 changes: 3 additions & 0 deletions tooling/nargo_fmt/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ macro_rules! config {
}

config! {
max_width: usize, 100, "Maximum width of each line";
tab_spaces: usize, 4, "Number of spaces per tab";
remove_nested_parens: bool, true, "Remove nested parens";
short_array_element_width_threshold: usize, 10, "Width threshold for an array element to be considered short";
array_width: usize, 60, "Maximum width of an array literal before falling back to vertical formatting";
}

impl Config {
Expand Down
38 changes: 31 additions & 7 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ pub(crate) fn comments(source: &str) -> impl Iterator<Item = String> + '_ {
#[derive(Debug)]
pub(crate) struct Expr {
pub(crate) leading: String,
pub(crate) expr: String,
pub(crate) value: String,
pub(crate) trailing: String,
pub(crate) different_line: bool,
}

impl Expr {
pub(crate) fn total_width(&self) -> usize {
comment_len(&self.leading) + self.value.chars().count() + comment_len(&self.trailing)
}

pub(crate) fn is_multiline(&self) -> bool {
self.leading.contains('\n') || self.trailing.contains('\n')
}
}

pub(crate) struct Exprs<'me> {
pub(crate) visitor: &'me FmtVisitor<'me>,
pub(crate) elements: std::iter::Peekable<std::vec::IntoIter<Expression>>,
Expand Down Expand Up @@ -71,17 +81,17 @@ impl Iterator for Exprs<'_> {
let is_last = self.elements.peek().is_none();
let next_start = self.elements.peek().map_or(self.end_position, |expr| expr.span.start());

let (leading, newlines) = self.leading(start, end);
let (leading, different_line) = self.leading(start, end);
let expr = self.visitor.format_expr(element);
let trailing = self.trailing(element_span.end(), next_start, is_last);

Expr { leading, expr, trailing, different_line: newlines }.into()
Expr { leading, value: expr, trailing, different_line }.into()
}
}

impl<'me> Exprs<'me> {
pub(crate) fn leading(&mut self, start: u32, end: u32) -> (String, bool) {
let mut newlines = false;
let mut different_line = false;

let leading = slice!(self.visitor, start, end);
let leading_trimmed = slice!(self.visitor, start, end).trim();
Expand All @@ -94,13 +104,13 @@ impl<'me> Exprs<'me> {
let comment_end = leading_trimmed.rfind(|c| c == '/').unwrap();

if leading[comment_end..].contains('\n') {
newlines = true;
different_line = true;
}
} else if starts_with_single_line_comment || starts_with_block_comment {
newlines = true;
different_line = true;
};

(leading_trimmed.to_string(), newlines)
(leading_trimmed.to_string(), different_line)
}

pub(crate) fn trailing(&mut self, start: u32, end: u32, is_last: bool) -> String {
Expand Down Expand Up @@ -177,3 +187,17 @@ pub(crate) fn find_comment_end(slice: &str, is_last: bool) -> usize {
0
}
}

fn comment_len(comment: &str) -> usize {
match comment {
"" => 0,
_ => {
let len = comment.trim().len();
if len > 0 {
len + 6
} else {
len
}
}
}
}
19 changes: 18 additions & 1 deletion tooling/nargo_fmt/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ impl<'me> FmtVisitor<'me> {
}
}

fn shape(&self) -> Shape {
Shape {
width: self.config.max_width.saturating_sub(self.indent.width()),
indent: self.indent,
}
}

pub(crate) fn fork(&self) -> Self {
Self {
buffer: String::new(),
Expand Down Expand Up @@ -151,12 +158,16 @@ impl<'me> FmtVisitor<'me> {
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
struct Indent {
block_indent: usize,
}

impl Indent {
fn width(&self) -> usize {
self.block_indent
}

fn block_indent(&mut self, config: &Config) {
self.block_indent += config.tab_spaces;
}
Expand All @@ -174,3 +185,9 @@ impl Indent {
" ".repeat(self.block_indent)
}
}

#[derive(Clone, Copy, Debug)]
struct Shape {
width: usize,
indent: Indent,
}
Loading

0 comments on commit 7de37c6

Please sign in to comment.