aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src')
-rw-r--r--crates/ide_assists/src/handlers/remove_dbg.rs85
1 files changed, 72 insertions, 13 deletions
diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs
index ae0f6b83f..46033fc12 100644
--- a/crates/ide_assists/src/handlers/remove_dbg.rs
+++ b/crates/ide_assists/src/handlers/remove_dbg.rs
@@ -24,18 +24,35 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
24 let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; 24 let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
25 let new_contents = adjusted_macro_contents(&macro_call)?; 25 let new_contents = adjusted_macro_contents(&macro_call)?;
26 26
27 let macro_text_range = if new_contents.is_empty() { 27 let parent = macro_call.syntax().parent();
28 let parent = macro_call.syntax().parent()?; 28
29 29 let macro_text_range = if let Some(it) = parent.as_ref() {
30 let start = parent 30 if new_contents.is_empty() {
31 .prev_sibling_or_token() 31 match_ast! {
32 .and_then(|el| { 32 match it {
33 Some(el.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start()) 33 ast::BlockExpr(it) => {
34 }) 34 macro_call.syntax()
35 .unwrap_or(parent.text_range().start()); 35 .prev_sibling_or_token()
36 let end = parent.text_range().end(); 36 .and_then(whitespace_start)
37 37 .map(|start| TextRange::new(start, macro_call.syntax().text_range().end()))
38 TextRange::new(start, end) 38 .unwrap_or(macro_call.syntax().text_range())
39 },
40 ast::ExprStmt(it) => {
41 let start = it
42 .syntax()
43 .prev_sibling_or_token()
44 .and_then(whitespace_start)
45 .unwrap_or(it.syntax().text_range().start());
46 let end = it.syntax().text_range().end();
47
48 TextRange::new(start, end)
49 },
50 _ => macro_call.syntax().text_range()
51 }
52 }
53 } else {
54 macro_call.syntax().text_range()
55 }
39 } else { 56 } else {
40 macro_call.syntax().text_range() 57 macro_call.syntax().text_range()
41 }; 58 };
@@ -51,11 +68,22 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
51 "Remove dbg!()", 68 "Remove dbg!()",
52 macro_text_range, 69 macro_text_range,
53 |builder| { 70 |builder| {
54 builder.replace(TextRange::new(macro_text_range.start(), macro_end), new_contents); 71 builder.replace(
72 TextRange::new(macro_text_range.start(), macro_end),
73 if new_contents.is_empty() && parent.and_then(ast::LetStmt::cast).is_some() {
74 ast::make::expr_unit().to_string()
75 } else {
76 new_contents
77 },
78 );
55 }, 79 },
56 ) 80 )
57} 81}
58 82
83fn whitespace_start(it: SyntaxElement) -> Option<TextSize> {
84 Some(it.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start())
85}
86
59fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> { 87fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> {
60 let contents = get_valid_macrocall_contents(&macro_call, "dbg")?; 88 let contents = get_valid_macrocall_contents(&macro_call, "dbg")?;
61 let macro_text_with_brackets = macro_call.token_tree()?.syntax().text(); 89 let macro_text_with_brackets = macro_call.token_tree()?.syntax().text();
@@ -441,5 +469,36 @@ $0dbg!();
441 r#"fn foo() { 469 r#"fn foo() {
442}"#, 470}"#,
443 ); 471 );
472 check_assist(
473 remove_dbg,
474 r#"fn foo() {
475let test = $0dbg!();
476}"#,
477 r#"fn foo() {
478let test = ();
479}"#,
480 );
481 check_assist(
482 remove_dbg,
483 r#"fn foo() {
484$0dbg!()
485}"#,
486 r#"fn foo() {
487}"#,
488 );
489 check_assist(
490 remove_dbg,
491 r#"fn foo() {
492let t = {
493 println!("Hello, world");
494 $0dbg!()
495};
496}"#,
497 r#"fn foo() {
498let t = {
499 println!("Hello, world");
500};
501}"#,
502 );
444 } 503 }
445} 504}