Skip to content

Commit

Permalink
Introduce ChunkGroup::space
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 19, 2024
1 parent cb299e5 commit 89a5a7e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
13 changes: 10 additions & 3 deletions tooling/nargo_fmt/src/formatter/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,16 @@ impl ChunkGroup {
}
}

/// Appends a semicolon to this group. This will actually end up attaching the
/// semicolon to the last text in this group so that we don't end up with
/// stray semicolons.
/// Appends a single space to this group by reading it from the given formatter.
pub(crate) fn space(&mut self, formatter: &mut Formatter<'_>) {
self.text(formatter.chunk(|formatter| {
formatter.write_space();
}));
}

/// Appends a semicolon to this group by reading it from the given formatter.
/// This will actually end up attaching the semicolon to the last text in this
/// group so that we don't end up with stray semicolons.
pub(crate) fn semicolon(&mut self, formatter: &mut Formatter<'_>) {
self.text_attached_to_last_group(formatter.chunk(|formatter| {
formatter.write_semicolon();
Expand Down
12 changes: 3 additions & 9 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ impl<'a> Formatter<'a> {

self.format_expression(*repeated_element, &mut group);
group.semicolon(self);
group.text(self.chunk(|formatter| {
formatter.write_space();
}));
group.space(self);
self.format_expression(*length, &mut group);

group.decrease_indentation();
Expand Down Expand Up @@ -778,9 +776,7 @@ impl<'a> Formatter<'a> {
*force_multiple_lines = true;
group.trailing_comment(comment_chunk_after_condition);
} else {
group.text(self.chunk(|formatter| {
formatter.write_space();
}));
group.space(self);
}

let ExpressionKind::Block(consequence_block) = if_expression.consequence.kind else {
Expand Down Expand Up @@ -817,9 +813,7 @@ impl<'a> Formatter<'a> {
*force_multiple_lines = true;
group.trailing_comment(comment_chunk_after_else);
} else {
group.text(self.chunk(|formatter| {
formatter.write_space();
}));
group.space(self);
}

let mut alternative_group = match alternative.kind {
Expand Down
4 changes: 1 addition & 3 deletions tooling/nargo_fmt/src/formatter/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ impl<'a> Formatter<'a> {
}
}

group.text(self.chunk(|formatter| {
formatter.write_space();
}));
group.space(self);

let ExpressionKind::Block(block) = for_loop.block.kind else {
panic!("Expected a block expression for for loop body");
Expand Down

0 comments on commit 89a5a7e

Please sign in to comment.