Skip to content

Commit

Permalink
fix extra spaces in if-expr with bool negation
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhance committed Feb 2, 2024
1 parent 5169588 commit 7792fe9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ fn unsupported(pair: Pair<Rule>) -> DocBuilder<Arena> {
todo!()
}

fn if_expr_to_doc<'a>(
ctx: &Context,
arena: &'a Arena<'a, ()>,
pair: Pair<'a, Rule>,
) -> DocBuilder<'a, Arena<'a>> {
arena.concat(pair.into_inner().map(|p| {
if p.as_rule() == Rule::condition {
to_doc(ctx, p, arena).append(arena.space())
} else {
to_doc(ctx, p, arena)
}
}))
}

fn loop_to_doc<'a>(
ctx: &Context,
arena: &'a Arena<'a, ()>,
Expand All @@ -450,7 +464,9 @@ fn loop_to_doc<'a>(
_ => (),
});
arena.concat(pair.into_inner().map(|p| {
if let Some(c) = last_clause {
if p.as_rule() == Rule::condition {
to_doc(ctx, p, arena).append(arena.space())
} else if let Some(c) = last_clause {
if p.as_rule() == c {
to_doc(ctx, p, arena).append(arena.line())
} else {
Expand Down Expand Up @@ -1026,8 +1042,8 @@ fn to_doc<'a>(
}))
.group()
}
Rule::condition => map_to_doc(ctx, arena, pair).append(arena.space()),
Rule::if_expr => map_to_doc(ctx, arena, pair),
Rule::condition => map_to_doc(ctx, arena, pair),
Rule::if_expr => if_expr_to_doc(ctx, arena, pair),
Rule::loop_expr => loop_to_doc(ctx, arena, pair),
Rule::for_expr => arena.concat(pair.into_inner().map(|p| match p.as_rule() {
Rule::in_str => arena.space().append(to_doc(ctx, p, arena)),
Expand Down
3 changes: 3 additions & 0 deletions tests/rustfmt-matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ fn test_function() {
if arg.len() < 12 {
return 5;
}
if !b {
return 5;
}
extend_vec_u8();
if y {
1
Expand Down

0 comments on commit 7792fe9

Please sign in to comment.