@@ -7,11 +7,8 @@ use ide_db::{
77} ;
88use syntax:: {
99 AstNode ,
10- ast:: {
11- self , HasGenericParams , HasName , HasTypeBounds , Name , NameLike , PathType ,
12- make:: impl_trait_type,
13- } ,
14- match_ast, ted,
10+ ast:: { self , HasGenericParams , HasName , HasTypeBounds , Name , NameLike , PathType , make} ,
11+ match_ast,
1512} ;
1613
1714use crate :: { AssistContext , AssistId , Assists } ;
@@ -74,26 +71,31 @@ pub(crate) fn replace_named_generic_with_impl(
7471 "Replace named generic with impl trait" ,
7572 target,
7673 |edit| {
77- let type_param = edit. make_mut ( type_param) ;
78- let fn_ = edit. make_mut ( fn_) ;
79-
80- let path_types_to_replace = path_types_to_replace
81- . into_iter ( )
82- . map ( |param| edit. make_mut ( param) )
83- . collect :: < Vec < _ > > ( ) ;
74+ let mut editor = edit. make_editor ( type_param. syntax ( ) ) ;
8475
8576 // remove trait from generic param list
8677 if let Some ( generic_params) = fn_. generic_param_list ( ) {
87- generic_params. remove_generic_param ( ast:: GenericParam :: TypeParam ( type_param) ) ;
88- if generic_params. generic_params ( ) . count ( ) == 0 {
89- ted:: remove ( generic_params. syntax ( ) ) ;
78+ let params: Vec < ast:: GenericParam > = generic_params
79+ . clone ( )
80+ . generic_params ( )
81+ . filter ( |it| it. syntax ( ) != type_param. syntax ( ) )
82+ . collect ( ) ;
83+ if params. is_empty ( ) {
84+ editor. delete ( generic_params. syntax ( ) ) ;
85+ } else {
86+ let new_generic_param_list = make:: generic_param_list ( params) ;
87+ editor. replace (
88+ generic_params. syntax ( ) ,
89+ new_generic_param_list. syntax ( ) . clone_for_update ( ) ,
90+ ) ;
9091 }
9192 }
9293
93- let new_bounds = impl_trait_type ( type_bound_list) ;
94+ let new_bounds = make :: impl_trait_type ( type_bound_list) ;
9495 for path_type in path_types_to_replace. iter ( ) . rev ( ) {
95- ted :: replace ( path_type. syntax ( ) , new_bounds. clone_for_update ( ) . syntax ( ) ) ;
96+ editor . replace ( path_type. syntax ( ) , new_bounds. clone_for_update ( ) . syntax ( ) ) ;
9697 }
98+ edit. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
9799 } ,
98100 )
99101}
0 commit comments