Skip to content

Commit

Permalink
fix: (formatter) indent after infix lhs (#6331)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #4980

## Summary

In reality the new formatter didn't crash anymore, it just formatted
this:

```noir
fn foo() {
    let x = 1 // one
        + 2 // two
        + 3; // three
}
```

like this:

```noir
fn foo() {
    let x = 1 // one
    + 2 // two
        + 3; // three
}
```

but making it look like the first snippet was easy, so here's that PR
:-)

## Additional Context

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Oct 24, 2024
1 parent 07ab515 commit c891ffd
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> {
}
};

// Indent right after the lhs so that if there's a trailing comment,
// the next line is indented correctly.
if increase_indentation {
group.increase_indentation();
}

let mut comment_chunk_after_lhs = self.skip_comments_and_whitespace_chunk();

// If the comment is not empty but doesn't have newlines, it's surely `/* comment */`.
Expand All @@ -719,10 +725,6 @@ impl<'a, 'b> ChunkFormatter<'a, 'b> {
group.trailing_comment(comment_chunk_after_lhs);
}

if increase_indentation {
group.increase_indentation();
}

group.space_or_line();
group.text(self.chunk(|formatter| {
let tokens_count =
Expand Down Expand Up @@ -1421,6 +1423,23 @@ global y = 1;
assert_format_with_max_width(src, expected, "one + two + three + four".len() - 1);
}

#[test]
fn format_infix_with_trailing_comments() {
let src = "fn foo() {
let x = 1 // one
+ 2 // two
+ 3; // three
}
";
let expected = "fn foo() {
let x = 1 // one
+ 2 // two
+ 3; // three
}
";
assert_format(src, expected);
}

#[test]
fn format_empty_block() {
let src = "global x = { } ;";
Expand Down

0 comments on commit c891ffd

Please sign in to comment.