|
1 | 1 | use syntax::{
|
2 | 2 | ast::{self, AstNode},
|
3 |
| - SyntaxElement, SyntaxKind, TextRange, TextSize, T, |
| 3 | + match_ast, SyntaxElement, SyntaxKind, TextRange, TextSize, T, |
4 | 4 | };
|
5 | 5 |
|
6 | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists};
|
@@ -49,12 +49,29 @@ fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> {
|
49 | 49 | macro_text_with_brackets.len() - TextSize::of(')'),
|
50 | 50 | ));
|
51 | 51 |
|
52 |
| - let is_leaf = macro_call.syntax().next_sibling().is_none(); |
53 |
| - Some(if !is_leaf && needs_parentheses_around_macro_contents(contents) { |
54 |
| - format!("({})", macro_text_in_brackets) |
55 |
| - } else { |
56 |
| - macro_text_in_brackets.to_string() |
57 |
| - }) |
| 52 | + Some( |
| 53 | + if !is_leaf_or_control_flow_expr(macro_call) |
| 54 | + && needs_parentheses_around_macro_contents(contents) |
| 55 | + { |
| 56 | + format!("({})", macro_text_in_brackets) |
| 57 | + } else { |
| 58 | + macro_text_in_brackets.to_string() |
| 59 | + }, |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +fn is_leaf_or_control_flow_expr(macro_call: &ast::MacroCall) -> bool { |
| 64 | + macro_call.syntax().next_sibling().is_none() |
| 65 | + || match macro_call.syntax().parent() { |
| 66 | + Some(parent) => match_ast! { |
| 67 | + match parent { |
| 68 | + ast::Condition(_it) => true, |
| 69 | + ast::MatchExpr(_it) => true, |
| 70 | + _ => false, |
| 71 | + } |
| 72 | + }, |
| 73 | + None => false, |
| 74 | + } |
58 | 75 | }
|
59 | 76 |
|
60 | 77 | /// Verifies that the given macro_call actually matches the given name
|
@@ -361,4 +378,44 @@ fn main() {
|
361 | 378 | r#"let res = (foo..=bar).foo();"#,
|
362 | 379 | );
|
363 | 380 | }
|
| 381 | + |
| 382 | + #[test] |
| 383 | + fn test_remove_dbg_followed_by_block() { |
| 384 | + check_assist( |
| 385 | + remove_dbg, |
| 386 | + r#"fn foo() { |
| 387 | + if <|>dbg!(x || y) {} |
| 388 | +}"#, |
| 389 | + r#"fn foo() { |
| 390 | + if x || y {} |
| 391 | +}"#, |
| 392 | + ); |
| 393 | + check_assist( |
| 394 | + remove_dbg, |
| 395 | + r#"fn foo() { |
| 396 | + while let foo = <|>dbg!(&x) {} |
| 397 | +}"#, |
| 398 | + r#"fn foo() { |
| 399 | + while let foo = &x {} |
| 400 | +}"#, |
| 401 | + ); |
| 402 | + check_assist( |
| 403 | + remove_dbg, |
| 404 | + r#"fn foo() { |
| 405 | + if let foo = <|>dbg!(&x) {} |
| 406 | +}"#, |
| 407 | + r#"fn foo() { |
| 408 | + if let foo = &x {} |
| 409 | +}"#, |
| 410 | + ); |
| 411 | + check_assist( |
| 412 | + remove_dbg, |
| 413 | + r#"fn foo() { |
| 414 | + match <|>dbg!(&x) {} |
| 415 | +}"#, |
| 416 | + r#"fn foo() { |
| 417 | + match &x {} |
| 418 | +}"#, |
| 419 | + ); |
| 420 | + } |
364 | 421 | }
|
0 commit comments