@@ -56,13 +56,37 @@ impl GenericParamsOwnerEdit for ast::Fn {
56
56
}
57
57
58
58
impl ast:: Fn {
59
- pub fn syntax_editor_get_or_create_generic_param_list (
59
+ pub fn syntax_editor_add_generic_param (
60
60
& self ,
61
61
editor : & mut SyntaxEditor ,
62
- ) -> ast:: GenericParamList {
62
+ new_param : GenericParam ,
63
+ ) {
63
64
match self . generic_param_list ( ) {
64
- Some ( it) => it,
65
+ Some ( generic_param_list) => match generic_param_list. generic_params ( ) . last ( ) {
66
+ Some ( _last_param) => {
67
+ // There exists a generic param list and it's not empty
68
+ let mut params = generic_param_list
69
+ . generic_params ( )
70
+ . map ( |param| param. clone ( ) )
71
+ . collect :: < Vec < _ > > ( ) ;
72
+ params. push ( new_param. into ( ) ) ;
73
+ let new_param_list = make:: generic_param_list ( params) ;
74
+ editor. replace (
75
+ generic_param_list. syntax ( ) ,
76
+ new_param_list. syntax ( ) . clone_for_update ( ) ,
77
+ ) ;
78
+ }
79
+ None => {
80
+ // There exists a generic param list but it's empty
81
+ let position = crate :: syntax_editor:: Position :: after (
82
+ generic_param_list. l_angle_token ( ) . unwrap ( ) ,
83
+ ) ;
84
+
85
+ editor. insert ( position, new_param. syntax ( ) ) ;
86
+ }
87
+ } ,
65
88
None => {
89
+ // There was no generic param list
66
90
let position = if let Some ( name) = self . name ( ) {
67
91
crate :: syntax_editor:: Position :: after ( name. syntax )
68
92
} else if let Some ( fn_token) = self . fn_token ( ) {
@@ -72,7 +96,9 @@ impl ast::Fn {
72
96
} else {
73
97
crate :: syntax_editor:: Position :: last_child_of ( self . syntax ( ) )
74
98
} ;
75
- syntax_editor_create_generic_param_list ( editor, position)
99
+
100
+ let new_param_list = make:: generic_param_list ( once ( new_param. clone ( ) ) ) ;
101
+ editor. insert ( position, new_param_list. syntax ( ) . clone_for_update ( ) ) ;
76
102
}
77
103
}
78
104
}
@@ -214,15 +240,6 @@ fn create_generic_param_list(position: Position) -> ast::GenericParamList {
214
240
gpl
215
241
}
216
242
217
- fn syntax_editor_create_generic_param_list (
218
- editor : & mut SyntaxEditor ,
219
- position : crate :: syntax_editor:: Position ,
220
- ) -> ast:: GenericParamList {
221
- let gpl = make:: generic_param_list ( empty ( ) ) . clone_for_update ( ) ;
222
- editor. insert ( position, gpl. syntax ( ) ) ;
223
- gpl
224
- }
225
-
226
243
pub trait AttrsOwnerEdit : ast:: HasAttrs {
227
244
fn remove_attrs_and_docs ( & self ) {
228
245
remove_attrs_and_docs ( self . syntax ( ) ) ;
@@ -290,28 +307,6 @@ impl ast::GenericParamList {
290
307
}
291
308
}
292
309
293
- pub fn syntax_editor_add_generic_param (
294
- & self ,
295
- editor : & mut SyntaxEditor ,
296
- new_param : ast:: GenericParam ,
297
- ) {
298
- match self . generic_params ( ) . last ( ) {
299
- Some ( _) => {
300
- let mut params =
301
- self . generic_params ( ) . map ( |param| param. clone ( ) ) . collect :: < Vec < _ > > ( ) ;
302
- params. push ( new_param. into ( ) ) ;
303
- let new_param_list = make:: generic_param_list ( params) ;
304
-
305
- editor. replace ( self . syntax ( ) , new_param_list. syntax ( ) ) ;
306
- }
307
- None => {
308
- let position = crate :: syntax_editor:: Position :: after ( self . l_angle_token ( ) . unwrap ( ) ) ;
309
- let new_param_list = make:: generic_param_list ( once ( new_param. clone ( ) ) ) ;
310
- editor. insert ( position, new_param_list. syntax ( ) ) ;
311
- }
312
- }
313
- }
314
-
315
310
/// Removes the existing generic param
316
311
pub fn remove_generic_param ( & self , generic_param : ast:: GenericParam ) {
317
312
if let Some ( previous) = generic_param. syntax ( ) . prev_sibling ( ) {
0 commit comments