Skip to content

Commit

Permalink
chore: update code formatting to prevent parameter line wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
f01dab1e committed Nov 27, 2023
1 parent 70ccb7e commit 86db44e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/src/rewrite/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use noirc_frontend::{hir::resolution::errors::Span, token::Token, Expression};

use crate::{
utils::{Expr, FindToken},
visitor::FmtVisitor,
visitor::{expr::NewlineMode, FmtVisitor},
};

pub(crate) fn rewrite(mut visitor: FmtVisitor, array: Vec<Expression>, array_span: Span) -> String {
Expand Down Expand Up @@ -80,6 +80,6 @@ pub(crate) fn rewrite(mut visitor: FmtVisitor, array: Vec<Expression>, array_spa
items_str.trim().into(),
nested_indent,
visitor.shape(),
true,
NewlineMode::IfContainsNewLineAndWidth,
)
}
8 changes: 4 additions & 4 deletions tooling/nargo_fmt/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ impl<'me, T> Exprs<'me, T> {

pub(crate) trait FindToken {
fn find_token(&self, token: Token) -> Option<Span>;
fn find_token_with(&self, f: impl Fn(&Token) -> bool) -> Option<u32>;
fn find_token_with(&self, f: impl Fn(&Token) -> bool) -> Option<Span>;
}

impl FindToken for str {
fn find_token(&self, token: Token) -> Option<Span> {
Lexer::new(self).flatten().find_map(|it| (it.token() == &token).then(|| it.to_span()))
}

fn find_token_with(&self, f: impl Fn(&Token) -> bool) -> Option<u32> {
fn find_token_with(&self, f: impl Fn(&Token) -> bool) -> Option<Span> {
Lexer::new(self)
.skip_comments(false)
.flatten()
.find_map(|spanned| f(spanned.token()).then(|| spanned.to_span().end()))
.find_map(|spanned| f(spanned.token()).then(|| spanned.to_span()))
}
}

Expand All @@ -142,7 +142,7 @@ pub(crate) fn find_comment_end(slice: &str, is_last: bool) -> usize {
.find_token_with(|token| {
matches!(token, Token::LineComment(_, _) | Token::BlockComment(_, _))
})
.map(|index| index as usize)
.map(|index| index.end() as usize)
.unwrap_or(slice.len())
}

Expand Down
59 changes: 48 additions & 11 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl FmtVisitor<'_> {
false,
exprs,
nested_indent,
true,
);

visitor.indent.block_unindent(visitor.config);
Expand Down Expand Up @@ -196,17 +197,18 @@ pub(crate) fn format_seq<T: Item>(
exprs: Vec<T>,
span: Span,
tactic: Tactic,
soft_newline: bool,
mode: NewlineMode,
reduce: bool,
) -> String {
let mut nested_indent = shape;
let shape = shape;

nested_indent.indent.block_indent(visitor.config);

let exprs: Vec<_> = utils::Exprs::new(&visitor, nested_indent, span, exprs).collect();
let exprs = format_exprs(visitor.config, tactic, trailing_comma, exprs, nested_indent);
let exprs = format_exprs(visitor.config, tactic, trailing_comma, exprs, nested_indent, reduce);

wrap_exprs(prefix, suffix, exprs, nested_indent, shape, soft_newline)
wrap_exprs(prefix, suffix, exprs, nested_indent, shape, mode)
}

pub(crate) fn format_brackets(
Expand All @@ -225,6 +227,7 @@ pub(crate) fn format_brackets(
exprs,
span,
Tactic::LimitedHorizontalVertical(array_width),
NewlineMode::Normal,
false,
)
}
Expand All @@ -236,10 +239,21 @@ pub(crate) fn format_parens(
trailing_comma: bool,
exprs: Vec<Expression>,
span: Span,
soft_newline: bool,
reduce: bool,
) -> String {
let tactic = max_width.map(Tactic::LimitedHorizontalVertical).unwrap_or(Tactic::Horizontal);
format_seq(shape, "(", ")", visitor, trailing_comma, exprs, span, tactic, soft_newline)
format_seq(
shape,
"(",
")",
visitor,
trailing_comma,
exprs,
span,
tactic,
NewlineMode::Normal,
reduce,
)
}

fn format_exprs(
Expand All @@ -248,11 +262,12 @@ fn format_exprs(
trailing_comma: bool,
exprs: Vec<Expr>,
shape: Shape,
reduce: bool,
) -> String {
let mut result = String::new();
let indent_str = shape.indent.to_string();

let tactic = tactic.definitive(&exprs, config.short_array_element_width_threshold);
let tactic = tactic.definitive(&exprs, config.short_array_element_width_threshold, reduce);
let mut exprs = exprs.into_iter().enumerate().peekable();
let mut line_len = 0;
let mut prev_expr_trailing_comment = false;
Expand Down Expand Up @@ -325,16 +340,32 @@ fn format_exprs(
result
}

#[derive(PartialEq, Eq)]
pub(crate) enum NewlineMode {
IfContainsNewLine,
IfContainsNewLineAndWidth,
Normal,
}

pub(crate) fn wrap_exprs(
prefix: &str,
suffix: &str,
exprs: String,
nested_shape: Shape,
shape: Shape,
soft_newline: bool,
newline_mode: NewlineMode,
) -> String {
let mut force_one_line = first_line_width(&exprs) <= shape.width;
if soft_newline && force_one_line {
let mut force_one_line = if newline_mode == NewlineMode::IfContainsNewLine {
true
} else {
first_line_width(&exprs) <= shape.width
};

if matches!(
newline_mode,
NewlineMode::IfContainsNewLine | NewlineMode::IfContainsNewLineAndWidth
) && force_one_line
{
force_one_line = !exprs.contains('\n');
}

Expand Down Expand Up @@ -373,7 +404,8 @@ impl Tactic {
fn definitive(
self,
exprs: &[Expr],
short_array_element_width_threshold: usize,
short_width_threshold: usize,
reduce: bool,
) -> DefinitiveTactic {
let tactic = || {
let has_single_line_comment = exprs.iter().any(|item| {
Expand Down Expand Up @@ -407,7 +439,12 @@ impl Tactic {
}
};

tactic().reduce(exprs, short_array_element_width_threshold)
let definitive_tactic = tactic();
if reduce {
definitive_tactic.reduce(exprs, short_width_threshold)
} else {
definitive_tactic
}
}
}

Expand Down
18 changes: 13 additions & 5 deletions tooling/nargo_fmt/src/visitor/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use noirc_frontend::{

use crate::{
utils::{last_line_contains_single_line_comment, last_line_used_width, FindToken},
visitor::expr::format_seq,
visitor::expr::{format_seq, NewlineMode},
};

use super::{
Expand Down Expand Up @@ -54,6 +54,7 @@ impl super::FmtVisitor<'_> {
generics,
span,
HorizontalVertical,
NewlineMode::IfContainsNewLine,
false,
);

Expand All @@ -63,12 +64,18 @@ impl super::FmtVisitor<'_> {
let parameters = if parameters.is_empty() {
self.slice(params_span).into()
} else {
let fn_start = result.find_token(Token::Keyword(Keyword::Fn)).unwrap().start();
let slice = self.slice(fn_start..result.len() as u32);
let fn_start = result
.find_token_with(|token| {
matches!(token, Token::Keyword(Keyword::Fn | Keyword::Unconstrained))
})
.unwrap()
.start();

let slice = self.slice(fn_start..result.len() as u32);
let indent = self.indent;
let used_width = last_line_used_width(slice, indent.width());
let one_line_budget = self.budget(used_width + return_type.len());
let overhead = if return_type.is_empty() { 2 } else { 3 }; // 2 = `()`, 3 = `() `
let one_line_budget = self.budget(used_width + return_type.len() + overhead);
let shape = Shape { width: one_line_budget, indent };

let tactic = LimitedHorizontalVertical(one_line_budget);
Expand All @@ -82,7 +89,8 @@ impl super::FmtVisitor<'_> {
parameters,
params_span.into(),
tactic,
true,
NewlineMode::IfContainsNewLine,
false,
)
};

Expand Down

0 comments on commit 86db44e

Please sign in to comment.