From 7792fe9f45a2e643573f3d80583ea6ad624b4170 Mon Sep 17 00:00:00 2001 From: Travis Hance Date: Fri, 2 Feb 2024 01:55:59 -0500 Subject: [PATCH] fix extra spaces in if-expr with bool negation --- src/lib.rs | 22 +++++++++++++++++++--- tests/rustfmt-matching.rs | 3 +++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4371ac4..b948de3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -434,6 +434,20 @@ fn unsupported(pair: Pair) -> DocBuilder { 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, ()>, @@ -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 { @@ -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)), diff --git a/tests/rustfmt-matching.rs b/tests/rustfmt-matching.rs index 876fc83..63e59fd 100644 --- a/tests/rustfmt-matching.rs +++ b/tests/rustfmt-matching.rs @@ -282,6 +282,9 @@ fn test_function() { if arg.len() < 12 { return 5; } + if !b { + return 5; + } extend_vec_u8(); if y { 1