11use itertools:: Itertools ;
22use syntax:: {
33 Edition , NodeOrToken , SyntaxNode , SyntaxToken , T ,
4- ast:: { self , AstNode , make } ,
4+ ast:: { self , AstNode , syntax_factory :: SyntaxFactory } ,
55 match_ast,
66 syntax_editor:: { Position , SyntaxEditor } ,
77} ;
@@ -24,6 +24,8 @@ use crate::{AssistContext, AssistId, Assists};
2424// }
2525// ```
2626pub ( crate ) fn remove_dbg ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
27+ let ( editor, _) = SyntaxEditor :: new ( ctx. source_file ( ) . syntax ( ) . clone ( ) ) ;
28+ let make = editor. make ( ) ;
2729 let macro_calls = if ctx. has_empty_selection ( ) {
2830 vec ! [ ctx. find_node_at_offset:: <ast:: MacroExpr >( ) ?]
2931 } else {
@@ -39,15 +41,16 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
3941 . collect ( )
4042 } ;
4143
42- let replacements =
43- macro_calls. into_iter ( ) . filter_map ( compute_dbg_replacement) . collect :: < Vec < _ > > ( ) ;
44+ let replacements = macro_calls
45+ . into_iter ( )
46+ . filter_map ( |macro_expr| compute_dbg_replacement ( macro_expr, make) )
47+ . collect :: < Vec < _ > > ( ) ;
4448 let target = replacements
4549 . iter ( )
4650 . flat_map ( |( node_or_token, _) | node_or_token. iter ( ) )
4751 . map ( |t| t. text_range ( ) )
4852 . reduce ( |acc, range| acc. cover ( range) ) ?;
4953 acc. add ( AssistId :: quick_fix ( "remove_dbg" ) , "Remove dbg!()" , target, |builder| {
50- let editor = builder. make_editor ( ctx. source_file ( ) . syntax ( ) ) ;
5154 for ( range, expr) in replacements {
5255 if let Some ( expr) = expr {
5356 editor. insert ( Position :: before ( range[ 0 ] . clone ( ) ) , expr. syntax ( ) ) ;
@@ -68,6 +71,7 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
6871/// Returns `Some(_, None)` when the macro call should just be removed.
6972fn compute_dbg_replacement (
7073 macro_expr : ast:: MacroExpr ,
74+ make : & SyntaxFactory ,
7175) -> Option < ( Vec < NodeOrToken < SyntaxNode , SyntaxToken > > , Option < ast:: Expr > ) > {
7276 let macro_call = macro_expr. macro_call ( ) ?;
7377 let tt = macro_call. token_tree ( ) ?;
@@ -110,7 +114,7 @@ fn compute_dbg_replacement(
110114 }
111115 ( replace, None )
112116 } ,
113- _ => ( vec![ macro_call. syntax( ) . clone( ) . into( ) ] , Some ( make:: ext :: expr_unit( ) ) ) ,
117+ _ => ( vec![ macro_call. syntax( ) . clone( ) . into( ) ] , Some ( make. expr_unit( ) ) ) ,
114118 }
115119 }
116120 }
@@ -162,14 +166,14 @@ fn compute_dbg_replacement(
162166 } ,
163167 None => false ,
164168 } ;
165- let expr = replace_nested_dbgs ( expr. clone ( ) ) ;
166- let expr = if wrap { make:: expr_paren ( expr) . into ( ) } else { expr } ;
169+ let expr = replace_nested_dbgs ( expr. clone ( ) , make ) ;
170+ let expr = if wrap { make. expr_paren ( expr) . into ( ) } else { expr } ;
167171 ( vec ! [ macro_call. syntax( ) . clone( ) . into( ) ] , Some ( expr) )
168172 }
169173 // dbg!(expr0, expr1, ...)
170174 exprs => {
171- let exprs = exprs. iter ( ) . cloned ( ) . map ( replace_nested_dbgs) ;
172- let expr = make:: expr_tuple ( exprs) ;
175+ let exprs = exprs. iter ( ) . cloned ( ) . map ( |expr| replace_nested_dbgs ( expr , make ) ) ;
176+ let expr = make. expr_tuple ( exprs) ;
173177 ( vec ! [ macro_call. syntax( ) . clone( ) . into( ) ] , Some ( expr. into ( ) ) )
174178 }
175179 } )
@@ -189,12 +193,12 @@ fn pure_expr(expr: &ast::Expr) -> bool {
189193 }
190194}
191195
192- fn replace_nested_dbgs ( expanded : ast:: Expr ) -> ast:: Expr {
196+ fn replace_nested_dbgs ( expanded : ast:: Expr , make : & SyntaxFactory ) -> ast:: Expr {
193197 if let ast:: Expr :: MacroExpr ( mac) = & expanded {
194198 // Special-case when `expanded` itself is `dbg!()` since we cannot replace the whole tree
195199 // with `ted`. It should be fairly rare as it means the user wrote `dbg!(dbg!(..))` but you
196200 // never know how code ends up being!
197- let replaced = if let Some ( ( _, expr_opt) ) = compute_dbg_replacement ( mac. clone ( ) ) {
201+ let replaced = if let Some ( ( _, expr_opt) ) = compute_dbg_replacement ( mac. clone ( ) , make ) {
198202 match expr_opt {
199203 Some ( expr) => expr,
200204 None => {
@@ -215,7 +219,7 @@ fn replace_nested_dbgs(expanded: ast::Expr) -> ast::Expr {
215219 expanded. syntax ( ) . descendants ( ) . filter_map ( ast:: MacroExpr :: cast) . collect ( ) ;
216220
217221 for mac in macro_exprs {
218- let expr_opt = match compute_dbg_replacement ( mac. clone ( ) ) {
222+ let expr_opt = match compute_dbg_replacement ( mac. clone ( ) , make ) {
219223 Some ( ( _, expr) ) => expr,
220224 None => continue ,
221225 } ;
0 commit comments