Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
f01dab1e committed Nov 8, 2023
1 parent 6fda18c commit 1b5dabc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
22 changes: 3 additions & 19 deletions tooling/nargo_fmt/src/rewrite/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ use noirc_frontend::{Expression, ExpressionKind};
use crate::{
utils::{first_line_width, is_single_line},
visitor::{ExpressionType, FmtVisitor, Shape},
Config,
};

pub(crate) fn rewrite(visitor: FmtVisitor, expr: Expression, shape: Shape) -> String {
match flatten(visitor.fork(), &expr) {
Some((exprs, separators)) => {
rewrite_single_line(visitor.config, shape, &exprs, &separators)
.unwrap_or_else(|| rewrite_multiline(visitor, &exprs, &separators))
}
Some((exprs, separators)) => rewrite_single_line(shape, &exprs, &separators)
.unwrap_or_else(|| rewrite_multiline(visitor, &exprs, &separators)),
None => {
let ExpressionKind::Infix(infix) = expr.kind else { unreachable!() };

Expand All @@ -27,12 +24,7 @@ pub(crate) fn rewrite(visitor: FmtVisitor, expr: Expression, shape: Shape) -> St
}
}

fn rewrite_single_line(
config: &Config,
shape: Shape,
exprs: &[String],
separators: &[String],
) -> Option<String> {
fn rewrite_single_line(shape: Shape, exprs: &[String], separators: &[String]) -> Option<String> {
let mut result = String::new();

for (rewrite, separator) in zip(exprs, separators) {
Expand All @@ -46,21 +38,13 @@ fn rewrite_single_line(
result.push(' ');
}

let prefix_len = result.len();

let last = exprs.last().unwrap();
result.push_str(last);

if first_line_width(&result) > shape.width {
return None;
}

if !(is_single_line(&result) || last.starts_with('{'))
&& (last.starts_with('(') || prefix_len > config.tab_spaces)
{
return None;
}

result.into()
}

Expand Down
3 changes: 1 addition & 2 deletions tooling/nargo_fmt/tests/expected/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ fn parenthesized() {
}

fn parenthesized() {
value
+ (
value + (
// line
x as Field
)
Expand Down

0 comments on commit 1b5dabc

Please sign in to comment.