@@ -56,6 +56,7 @@ impl GenericParamsOwnerEdit for ast::Fn {
56
56
}
57
57
58
58
impl ast:: Fn {
59
+ /// Adds a new generic param to the function using `SyntaxEditor`
59
60
pub fn syntax_editor_add_generic_param (
60
61
& self ,
61
62
editor : & mut SyntaxEditor ,
@@ -65,23 +66,44 @@ impl ast::Fn {
65
66
Some ( generic_param_list) => match generic_param_list. generic_params ( ) . last ( ) {
66
67
Some ( _last_param) => {
67
68
// 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 ( ) ,
69
+ let position = generic_param_list. r_angle_token ( ) . map_or_else (
70
+ || crate :: syntax_editor:: Position :: last_child_of ( self . syntax ( ) ) ,
71
+ crate :: syntax_editor:: Position :: before,
77
72
) ;
73
+
74
+ if let Some ( last_param) = generic_param_list. generic_params ( ) . last ( ) {
75
+ if last_param
76
+ . syntax ( )
77
+ . next_sibling_or_token ( )
78
+ . map_or ( false , |it| it. kind ( ) == SyntaxKind :: COMMA )
79
+ {
80
+ editor. insert (
81
+ crate :: syntax_editor:: Position :: after ( last_param. syntax ( ) ) ,
82
+ new_param. syntax ( ) . clone ( ) ,
83
+ ) ;
84
+ editor. insert (
85
+ crate :: syntax_editor:: Position :: after ( last_param. syntax ( ) ) ,
86
+ make:: token ( SyntaxKind :: WHITESPACE ) ,
87
+ ) ;
88
+ editor. insert (
89
+ crate :: syntax_editor:: Position :: after ( last_param. syntax ( ) ) ,
90
+ make:: token ( SyntaxKind :: COMMA ) ,
91
+ ) ;
92
+ } else {
93
+ let elements = vec ! [
94
+ make:: token( SyntaxKind :: COMMA ) . into( ) ,
95
+ make:: token( SyntaxKind :: WHITESPACE ) . into( ) ,
96
+ new_param. syntax( ) . clone( ) . into( ) ,
97
+ ] ;
98
+ editor. insert_all ( position, elements) ;
99
+ }
100
+ } ;
78
101
}
79
102
None => {
80
103
// There exists a generic param list but it's empty
81
104
let position = crate :: syntax_editor:: Position :: after (
82
105
generic_param_list. l_angle_token ( ) . unwrap ( ) ,
83
106
) ;
84
-
85
107
editor. insert ( position, new_param. syntax ( ) ) ;
86
108
}
87
109
} ,
@@ -96,9 +118,12 @@ impl ast::Fn {
96
118
} else {
97
119
crate :: syntax_editor:: Position :: last_child_of ( self . syntax ( ) )
98
120
} ;
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 ( ) ) ;
121
+ let elements = vec ! [
122
+ make:: token( SyntaxKind :: L_ANGLE ) . into( ) ,
123
+ new_param. syntax( ) . clone( ) . into( ) ,
124
+ make:: token( T ![ >] ) . into( ) ,
125
+ ] ;
126
+ editor. insert_all ( position, elements) ;
102
127
}
103
128
}
104
129
}
0 commit comments