@@ -3,7 +3,7 @@ use ra_syntax::{
33 ast:: {
44 self , AstNode , NameOwner , StructKind , TypeAscriptionOwner , TypeParamsOwner , VisibilityOwner ,
55 } ,
6- TextSize , T ,
6+ T ,
77} ;
88use stdx:: { format_to, SepBy } ;
99
@@ -25,7 +25,7 @@ use crate::{AssistContext, AssistId, Assists};
2525// }
2626//
2727// impl<T: Clone> Ctx<T> {
28- // fn new (data: T) -> Self { Self { data } }
28+ // fn $0new (data: T) -> Self { Self { data } }
2929// }
3030//
3131// ```
@@ -42,31 +42,26 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4242 let impl_def = find_struct_impl ( & ctx, & strukt) ?;
4343
4444 let target = strukt. syntax ( ) . text_range ( ) ;
45- acc. add ( AssistId ( "add_new" ) , "Add default constructor" , target, |edit | {
45+ acc. add ( AssistId ( "add_new" ) , "Add default constructor" , target, |builder | {
4646 let mut buf = String :: with_capacity ( 512 ) ;
4747
4848 if impl_def. is_some ( ) {
4949 buf. push ( '\n' ) ;
5050 }
5151
52- let vis = strukt. visibility ( ) . map ( |v| format ! ( "{} " , v) ) ;
53- let vis = vis. as_deref ( ) . unwrap_or ( "" ) ;
52+ let vis = strukt. visibility ( ) . map_or ( String :: new ( ) , |v| format ! ( "{} " , v) ) ;
5453
5554 let params = field_list
5655 . fields ( )
5756 . filter_map ( |f| {
58- Some ( format ! (
59- "{}: {}" ,
60- f. name( ) ?. syntax( ) . text( ) ,
61- f. ascribed_type( ) ?. syntax( ) . text( )
62- ) )
57+ Some ( format ! ( "{}: {}" , f. name( ) ?. syntax( ) , f. ascribed_type( ) ?. syntax( ) ) )
6358 } )
6459 . sep_by ( ", " ) ;
6560 let fields = field_list. fields ( ) . filter_map ( |f| f. name ( ) ) . sep_by ( ", " ) ;
6661
6762 format_to ! ( buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}" , vis, params, fields) ;
6863
69- let ( start_offset, end_offset ) = impl_def
64+ let start_offset = impl_def
7065 . and_then ( |impl_def| {
7166 buf. push ( '\n' ) ;
7267 let start = impl_def
@@ -76,17 +71,20 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
7671 . text_range ( )
7772 . end ( ) ;
7873
79- Some ( ( start, TextSize :: of ( " \n " ) ) )
74+ Some ( start)
8075 } )
8176 . unwrap_or_else ( || {
8277 buf = generate_impl_text ( & strukt, & buf) ;
83- let start = strukt. syntax ( ) . text_range ( ) . end ( ) ;
84-
85- ( start, TextSize :: of ( "\n }\n " ) )
78+ strukt. syntax ( ) . text_range ( ) . end ( )
8679 } ) ;
8780
88- edit. set_cursor ( start_offset + TextSize :: of ( & buf) - end_offset) ;
89- edit. insert ( start_offset, buf) ;
81+ match ctx. config . snippet_cap {
82+ None => builder. insert ( start_offset, buf) ,
83+ Some ( cap) => {
84+ buf = buf. replace ( "fn new" , "fn $0new" ) ;
85+ builder. insert_snippet ( cap, start_offset, buf) ;
86+ }
87+ }
9088 } )
9189}
9290
@@ -191,7 +189,7 @@ mod tests {
191189"struct Foo {}
192190
193191impl Foo {
194- fn new () -> Self { Self { } }<|>
192+ fn $0new () -> Self { Self { } }
195193}
196194" ,
197195 ) ;
@@ -201,7 +199,7 @@ impl Foo {
201199"struct Foo<T: Clone> {}
202200
203201impl<T: Clone> Foo<T> {
204- fn new () -> Self { Self { } }<|>
202+ fn $0new () -> Self { Self { } }
205203}
206204" ,
207205 ) ;
@@ -211,7 +209,7 @@ impl<T: Clone> Foo<T> {
211209"struct Foo<'a, T: Foo<'a>> {}
212210
213211impl<'a, T: Foo<'a>> Foo<'a, T> {
214- fn new () -> Self { Self { } }<|>
212+ fn $0new () -> Self { Self { } }
215213}
216214" ,
217215 ) ;
@@ -221,7 +219,7 @@ impl<'a, T: Foo<'a>> Foo<'a, T> {
221219"struct Foo { baz: String }
222220
223221impl Foo {
224- fn new (baz: String) -> Self { Self { baz } }<|>
222+ fn $0new (baz: String) -> Self { Self { baz } }
225223}
226224" ,
227225 ) ;
@@ -231,7 +229,7 @@ impl Foo {
231229"struct Foo { baz: String, qux: Vec<i32> }
232230
233231impl Foo {
234- fn new (baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }<|>
232+ fn $0new (baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }
235233}
236234" ,
237235 ) ;
@@ -243,7 +241,7 @@ impl Foo {
243241"struct Foo { pub baz: String, pub qux: Vec<i32> }
244242
245243impl Foo {
246- fn new (baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }<|>
244+ fn $0new (baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }
247245}
248246" ,
249247 ) ;
@@ -258,7 +256,7 @@ impl Foo {}
258256"struct Foo {}
259257
260258impl Foo {
261- fn new () -> Self { Self { } }<|>
259+ fn $0new () -> Self { Self { } }
262260}
263261" ,
264262 ) ;
@@ -273,7 +271,7 @@ impl Foo {
273271"struct Foo {}
274272
275273impl Foo {
276- fn new () -> Self { Self { } }<|>
274+ fn $0new () -> Self { Self { } }
277275
278276 fn qux(&self) {}
279277}
@@ -294,7 +292,7 @@ impl Foo {
294292"struct Foo {}
295293
296294impl Foo {
297- fn new () -> Self { Self { } }<|>
295+ fn $0new () -> Self { Self { } }
298296
299297 fn qux(&self) {}
300298 fn baz() -> i32 {
@@ -311,7 +309,7 @@ impl Foo {
311309"pub struct Foo {}
312310
313311impl Foo {
314- pub fn new () -> Self { Self { } }<|>
312+ pub fn $0new () -> Self { Self { } }
315313}
316314" ,
317315 ) ;
@@ -321,7 +319,7 @@ impl Foo {
321319"pub(crate) struct Foo {}
322320
323321impl Foo {
324- pub(crate) fn new () -> Self { Self { } }<|>
322+ pub(crate) fn $0new () -> Self { Self { } }
325323}
326324" ,
327325 ) ;
@@ -414,7 +412,7 @@ pub struct Source<T> {
414412}
415413
416414impl<T> Source<T> {
417- pub fn new (file_id: HirFileId, ast: T) -> Self { Self { file_id, ast } }<|>
415+ pub fn $0new (file_id: HirFileId, ast: T) -> Self { Self { file_id, ast } }
418416
419417 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
420418 Source { file_id: self.file_id, ast: f(self.ast) }
0 commit comments