@@ -24,18 +24,35 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2424 let macro_call = ctx. find_node_at_offset :: < ast:: MacroCall > ( ) ?;
2525 let new_contents = adjusted_macro_contents ( & macro_call) ?;
2626
27- let macro_text_range = if new_contents. is_empty ( ) {
28- let parent = macro_call. syntax ( ) . parent ( ) ?;
29-
30- let start = parent
31- . prev_sibling_or_token ( )
32- . and_then ( |el| {
33- Some ( el. into_token ( ) . and_then ( ast:: Whitespace :: cast) ?. syntax ( ) . text_range ( ) . start ( ) )
34- } )
35- . unwrap_or ( parent. text_range ( ) . start ( ) ) ;
36- let end = parent. text_range ( ) . end ( ) ;
37-
38- TextRange :: new ( start, end)
27+ let parent = macro_call. syntax ( ) . parent ( ) ;
28+
29+ let macro_text_range = if let Some ( it) = parent. as_ref ( ) {
30+ if new_contents. is_empty ( ) {
31+ match_ast ! {
32+ match it {
33+ ast:: BlockExpr ( it) => {
34+ macro_call. syntax( )
35+ . prev_sibling_or_token( )
36+ . and_then( whitespace_start)
37+ . map( |start| TextRange :: new( start, macro_call. syntax( ) . text_range( ) . 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+ }
3956 } else {
4057 macro_call. syntax ( ) . text_range ( )
4158 } ;
@@ -51,11 +68,22 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
5168 "Remove dbg!()" ,
5269 macro_text_range,
5370 |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+ ) ;
5579 } ,
5680 )
5781}
5882
83+ fn whitespace_start ( it : SyntaxElement ) -> Option < TextSize > {
84+ Some ( it. into_token ( ) . and_then ( ast:: Whitespace :: cast) ?. syntax ( ) . text_range ( ) . start ( ) )
85+ }
86+
5987fn adjusted_macro_contents ( macro_call : & ast:: MacroCall ) -> Option < String > {
6088 let contents = get_valid_macrocall_contents ( & macro_call, "dbg" ) ?;
6189 let macro_text_with_brackets = macro_call. token_tree ( ) ?. syntax ( ) . text ( ) ;
@@ -439,6 +467,37 @@ fn main() {
439467$0dbg!();
440468}"# ,
441469 r#"fn foo() {
470+ }"# ,
471+ ) ;
472+ check_assist (
473+ remove_dbg,
474+ r#"fn foo() {
475+ let test = $0dbg!();
476+ }"# ,
477+ r#"fn foo() {
478+ let 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() {
492+ let t = {
493+ println!("Hello, world");
494+ $0dbg!()
495+ };
496+ }"# ,
497+ r#"fn foo() {
498+ let t = {
499+ println!("Hello, world");
500+ };
442501}"# ,
443502 ) ;
444503 }
0 commit comments