Skip to content

Commit

Permalink
fix extra spaces in if-expr with bool negation (#24)
Browse files Browse the repository at this point in the history
Currently, verusfmt adds a space for each 'condition' node. However, the
grammar is set up so that this has _two_ negation nodes:

```
if !b {
}
```

so it ends up formatted as:

```
if !b  {
}
```

See the expansion has two 'condition' nodes:

```
if_expr {
                  if_str {}
                  condition {
                    bang_str {}
                    condition {
                      expr_no_struct {
                        path_expr_no_generics {
                          path_no_generics {
                            path_segment_no_generics {
                              name_ref {
                                identifier {}
                              }
                            }
                          }
                        }
                      }
                    }
                  }
```

The PR changes the handling of the space to be with an 'if' expression
or 'while' expression, rather than having it handled with the
'condition' node.

Co-authored-by: Travis Hance <tjhance7@gmail.com>
  • Loading branch information
parno and tjhance authored Feb 7, 2024
2 parents 066eaae + 7792fe9 commit 91fd350
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 91fd350

Please sign in to comment.