@@ -56,6 +56,7 @@ impl GenericParamsOwnerEdit for ast::Fn {
5656}
5757
5858impl ast:: Fn {
59+ /// Adds a new generic param to the function using `SyntaxEditor`
5960 pub fn syntax_editor_add_generic_param (
6061 & self ,
6162 editor : & mut SyntaxEditor ,
@@ -65,23 +66,44 @@ impl ast::Fn {
6566 Some ( generic_param_list) => match generic_param_list. generic_params ( ) . last ( ) {
6667 Some ( _last_param) => {
6768 // 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,
7772 ) ;
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+ } ;
78101 }
79102 None => {
80103 // There exists a generic param list but it's empty
81104 let position = crate :: syntax_editor:: Position :: after (
82105 generic_param_list. l_angle_token ( ) . unwrap ( ) ,
83106 ) ;
84-
85107 editor. insert ( position, new_param. syntax ( ) ) ;
86108 }
87109 } ,
@@ -96,9 +118,12 @@ impl ast::Fn {
96118 } else {
97119 crate :: syntax_editor:: Position :: last_child_of ( self . syntax ( ) )
98120 } ;
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) ;
102127 }
103128 }
104129 }
0 commit comments