Auto merge of #12370 - andylizi:macro-expand-underscore, r=lnicola

ide: insert whitespaces surrounding `_` in macro expansion

#### Before

```rust
for_in 0..10 {
  foo();
}
```

#### After

```rust
for _ in 0..10 {
  foo();
}
```
This commit is contained in:
bors 2022-05-24 06:52:00 +00:00
commit d5965aa871
2 changed files with 21 additions and 3 deletions

View File

@ -57,7 +57,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
|f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
match tok.kind() {
k if is_text(k) && is_next(|it| !it.is_punct(), true) => {
k if is_text(k) && is_next(|it| !it.is_punct() || it == UNDERSCORE, false) => {
mods.push(do_ws(after, tok));
}
L_CURLY if is_next(|it| it != R_CURLY, true) => {
@ -118,5 +118,5 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
}
fn is_text(k: SyntaxKind) -> bool {
k.is_keyword() || k.is_literal() || k == IDENT
k.is_keyword() || k.is_literal() || k == IDENT || k == UNDERSCORE
}

View File

@ -237,6 +237,24 @@ fn main() {
);
}
#[test]
fn macro_expand_underscore() {
check(
r#"
macro_rules! bar {
($i:tt) => { for _ in 0..$i {} }
}
fn main() {
ba$0r!(42);
}
"#,
expect![[r#"
bar
for _ in 0..42{}
"#]],
);
}
#[test]
fn macro_expand_recursive_expansion() {
check(
@ -385,7 +403,7 @@ fn main() {
"#,
expect![[r#"
foo
0 "#]],
0"#]],
);
}