@@ -7,11 +7,8 @@ use ide_db::{
7
7
} ;
8
8
use syntax:: {
9
9
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,
15
12
} ;
16
13
17
14
use crate :: { AssistContext , AssistId , Assists } ;
@@ -74,26 +71,31 @@ pub(crate) fn replace_named_generic_with_impl(
74
71
"Replace named generic with impl trait" ,
75
72
target,
76
73
|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 ( ) ) ;
84
75
85
76
// remove trait from generic param list
86
77
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
+ ) ;
90
91
}
91
92
}
92
93
93
- let new_bounds = impl_trait_type ( type_bound_list) ;
94
+ let new_bounds = make :: impl_trait_type ( type_bound_list) ;
94
95
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 ( ) ) ;
96
97
}
98
+ edit. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
97
99
} ,
98
100
)
99
101
}
0 commit comments