@@ -3,7 +3,7 @@ use ra_syntax::{
3
3
ast:: {
4
4
self , AstNode , NameOwner , StructKind , TypeAscriptionOwner , TypeParamsOwner , VisibilityOwner ,
5
5
} ,
6
- TextSize , T ,
6
+ T ,
7
7
} ;
8
8
use stdx:: { format_to, SepBy } ;
9
9
@@ -25,7 +25,7 @@ use crate::{AssistContext, AssistId, Assists};
25
25
// }
26
26
//
27
27
// impl<T: Clone> Ctx<T> {
28
- // fn new (data: T) -> Self { Self { data } }
28
+ // fn $0new (data: T) -> Self { Self { data } }
29
29
// }
30
30
//
31
31
// ```
@@ -42,31 +42,26 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
42
42
let impl_def = find_struct_impl ( & ctx, & strukt) ?;
43
43
44
44
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 | {
46
46
let mut buf = String :: with_capacity ( 512 ) ;
47
47
48
48
if impl_def. is_some ( ) {
49
49
buf. push ( '\n' ) ;
50
50
}
51
51
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) ) ;
54
53
55
54
let params = field_list
56
55
. fields ( )
57
56
. 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( ) ) )
63
58
} )
64
59
. sep_by ( ", " ) ;
65
60
let fields = field_list. fields ( ) . filter_map ( |f| f. name ( ) ) . sep_by ( ", " ) ;
66
61
67
62
format_to ! ( buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}" , vis, params, fields) ;
68
63
69
- let ( start_offset, end_offset ) = impl_def
64
+ let start_offset = impl_def
70
65
. and_then ( |impl_def| {
71
66
buf. push ( '\n' ) ;
72
67
let start = impl_def
@@ -76,17 +71,20 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
76
71
. text_range ( )
77
72
. end ( ) ;
78
73
79
- Some ( ( start, TextSize :: of ( " \n " ) ) )
74
+ Some ( start)
80
75
} )
81
76
. unwrap_or_else ( || {
82
77
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 ( )
86
79
} ) ;
87
80
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
+ }
90
88
} )
91
89
}
92
90
@@ -191,7 +189,7 @@ mod tests {
191
189
"struct Foo {}
192
190
193
191
impl Foo {
194
- fn new () -> Self { Self { } }<|>
192
+ fn $0new () -> Self { Self { } }
195
193
}
196
194
" ,
197
195
) ;
@@ -201,7 +199,7 @@ impl Foo {
201
199
"struct Foo<T: Clone> {}
202
200
203
201
impl<T: Clone> Foo<T> {
204
- fn new () -> Self { Self { } }<|>
202
+ fn $0new () -> Self { Self { } }
205
203
}
206
204
" ,
207
205
) ;
@@ -211,7 +209,7 @@ impl<T: Clone> Foo<T> {
211
209
"struct Foo<'a, T: Foo<'a>> {}
212
210
213
211
impl<'a, T: Foo<'a>> Foo<'a, T> {
214
- fn new () -> Self { Self { } }<|>
212
+ fn $0new () -> Self { Self { } }
215
213
}
216
214
" ,
217
215
) ;
@@ -221,7 +219,7 @@ impl<'a, T: Foo<'a>> Foo<'a, T> {
221
219
"struct Foo { baz: String }
222
220
223
221
impl Foo {
224
- fn new (baz: String) -> Self { Self { baz } }<|>
222
+ fn $0new (baz: String) -> Self { Self { baz } }
225
223
}
226
224
" ,
227
225
) ;
@@ -231,7 +229,7 @@ impl Foo {
231
229
"struct Foo { baz: String, qux: Vec<i32> }
232
230
233
231
impl 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 } }
235
233
}
236
234
" ,
237
235
) ;
@@ -243,7 +241,7 @@ impl Foo {
243
241
"struct Foo { pub baz: String, pub qux: Vec<i32> }
244
242
245
243
impl 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 } }
247
245
}
248
246
" ,
249
247
) ;
@@ -258,7 +256,7 @@ impl Foo {}
258
256
"struct Foo {}
259
257
260
258
impl Foo {
261
- fn new () -> Self { Self { } }<|>
259
+ fn $0new () -> Self { Self { } }
262
260
}
263
261
" ,
264
262
) ;
@@ -273,7 +271,7 @@ impl Foo {
273
271
"struct Foo {}
274
272
275
273
impl Foo {
276
- fn new () -> Self { Self { } }<|>
274
+ fn $0new () -> Self { Self { } }
277
275
278
276
fn qux(&self) {}
279
277
}
@@ -294,7 +292,7 @@ impl Foo {
294
292
"struct Foo {}
295
293
296
294
impl Foo {
297
- fn new () -> Self { Self { } }<|>
295
+ fn $0new () -> Self { Self { } }
298
296
299
297
fn qux(&self) {}
300
298
fn baz() -> i32 {
@@ -311,7 +309,7 @@ impl Foo {
311
309
"pub struct Foo {}
312
310
313
311
impl Foo {
314
- pub fn new () -> Self { Self { } }<|>
312
+ pub fn $0new () -> Self { Self { } }
315
313
}
316
314
" ,
317
315
) ;
@@ -321,7 +319,7 @@ impl Foo {
321
319
"pub(crate) struct Foo {}
322
320
323
321
impl Foo {
324
- pub(crate) fn new () -> Self { Self { } }<|>
322
+ pub(crate) fn $0new () -> Self { Self { } }
325
323
}
326
324
" ,
327
325
) ;
@@ -414,7 +412,7 @@ pub struct Source<T> {
414
412
}
415
413
416
414
impl<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 } }
418
416
419
417
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
420
418
Source { file_id: self.file_id, ast: f(self.ast) }
0 commit comments