@@ -44,7 +44,7 @@ use syntax::{
44
44
// };
45
45
// }
46
46
//
47
- // trait NewTrait <const N: usize> {
47
+ // trait ${0:TraitName} <const N: usize> {
48
48
// // Used as an associated constant.
49
49
// const CONST_ASSOC: usize = N * 4;
50
50
//
@@ -53,7 +53,7 @@ use syntax::{
53
53
// const_maker! {i32, 7}
54
54
// }
55
55
//
56
- // impl<const N: usize> NewTrait <N> for Foo<N> {
56
+ // impl<const N: usize> ${0:TraitName} <N> for Foo<N> {
57
57
// // Used as an associated constant.
58
58
// const CONST_ASSOC: usize = N * 4;
59
59
//
@@ -126,23 +126,41 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
126
126
"" . to_string ( )
127
127
} ;
128
128
129
- // // Then replace
130
- builder. replace (
131
- impl_name. syntax ( ) . text_range ( ) ,
132
- format ! ( "NewTrait{} for {}" , arg_list, impl_name. to_string( ) ) ,
133
- ) ;
129
+ if let Some ( snippet_cap) = ctx. config . snippet_cap {
130
+ builder. replace_snippet (
131
+ snippet_cap,
132
+ impl_name. syntax ( ) . text_range ( ) ,
133
+ format ! ( "${{0:TraitName}}{} for {}" , arg_list, impl_name. to_string( ) ) ,
134
+ ) ;
135
+
136
+ // Insert trait before TraitImpl
137
+ builder. insert_snippet (
138
+ snippet_cap,
139
+ impl_ast. syntax ( ) . text_range ( ) . start ( ) ,
140
+ format ! (
141
+ "{}\n \n {}" ,
142
+ trait_ast. to_string( ) . replace( "NewTrait" , "${0:TraitName}" ) ,
143
+ IndentLevel :: from_node( impl_ast. syntax( ) )
144
+ ) ,
145
+ ) ;
146
+ } else {
147
+ builder. replace (
148
+ impl_name. syntax ( ) . text_range ( ) ,
149
+ format ! ( "NewTrait{} for {}" , arg_list, impl_name. to_string( ) ) ,
150
+ ) ;
151
+
152
+ // Insert trait before TraitImpl
153
+ builder. insert (
154
+ impl_ast. syntax ( ) . text_range ( ) . start ( ) ,
155
+ format ! (
156
+ "{}\n \n {}" ,
157
+ trait_ast. to_string( ) ,
158
+ IndentLevel :: from_node( impl_ast. syntax( ) )
159
+ ) ,
160
+ ) ;
161
+ }
134
162
135
163
builder. replace ( assoc_items. syntax ( ) . text_range ( ) , impl_items. to_string ( ) ) ;
136
-
137
- // Insert trait before TraitImpl
138
- builder. insert (
139
- impl_ast. syntax ( ) . text_range ( ) . start ( ) ,
140
- format ! (
141
- "{}\n \n {}" ,
142
- trait_ast. to_string( ) ,
143
- IndentLevel :: from_node( impl_ast. syntax( ) )
144
- ) ,
145
- ) ;
146
164
} ,
147
165
) ;
148
166
@@ -193,7 +211,7 @@ fn strip_body(item: &ast::AssocItem) {
193
211
#[ cfg( test) ]
194
212
mod tests {
195
213
use super :: * ;
196
- use crate :: tests:: { check_assist, check_assist_not_applicable} ;
214
+ use crate :: tests:: { check_assist, check_assist_no_snippet_cap , check_assist_not_applicable} ;
197
215
198
216
#[ test]
199
217
fn test_trigger_when_cursor_on_header ( ) {
@@ -212,7 +230,7 @@ impl Foo { $0
212
230
213
231
#[ test]
214
232
fn test_assoc_item_fn ( ) {
215
- check_assist (
233
+ check_assist_no_snippet_cap (
216
234
generate_trait_from_impl,
217
235
r#"
218
236
struct Foo(f64);
@@ -239,7 +257,7 @@ impl NewTrait for Foo {
239
257
240
258
#[ test]
241
259
fn test_assoc_item_macro ( ) {
242
- check_assist (
260
+ check_assist_no_snippet_cap (
243
261
generate_trait_from_impl,
244
262
r#"
245
263
struct Foo;
@@ -274,7 +292,7 @@ impl NewTrait for Foo {
274
292
275
293
#[ test]
276
294
fn test_assoc_item_const ( ) {
277
- check_assist (
295
+ check_assist_no_snippet_cap (
278
296
generate_trait_from_impl,
279
297
r#"
280
298
struct Foo;
@@ -297,7 +315,7 @@ impl NewTrait for Foo {
297
315
298
316
#[ test]
299
317
fn test_impl_with_generics ( ) {
300
- check_assist (
318
+ check_assist_no_snippet_cap (
301
319
generate_trait_from_impl,
302
320
r#"
303
321
struct Foo<const N: usize>([i32; N]);
@@ -325,7 +343,7 @@ impl<const N: usize> NewTrait<N> for Foo<N> {
325
343
326
344
#[ test]
327
345
fn test_trait_items_should_not_have_vis ( ) {
328
- check_assist (
346
+ check_assist_no_snippet_cap (
329
347
generate_trait_from_impl,
330
348
r#"
331
349
struct Foo;
@@ -362,7 +380,7 @@ impl Emp$0tyImpl{}
362
380
363
381
#[ test]
364
382
fn test_not_top_level_impl ( ) {
365
- check_assist (
383
+ check_assist_no_snippet_cap (
366
384
generate_trait_from_impl,
367
385
r#"
368
386
mod a {
@@ -382,4 +400,32 @@ mod a {
382
400
}"# ,
383
401
)
384
402
}
403
+
404
+ #[ test]
405
+ fn test_snippet_cap_is_some ( ) {
406
+ check_assist (
407
+ generate_trait_from_impl,
408
+ r#"
409
+ struct Foo<const N: usize>([i32; N]);
410
+
411
+ impl<const N: usize> F$0oo<N> {
412
+ // Used as an associated constant.
413
+ const CONST: usize = N * 4;
414
+ }
415
+ "# ,
416
+ r#"
417
+ struct Foo<const N: usize>([i32; N]);
418
+
419
+ trait ${0:TraitName}<const N: usize> {
420
+ // Used as an associated constant.
421
+ const CONST: usize = N * 4;
422
+ }
423
+
424
+ impl<const N: usize> ${0:TraitName}<N> for Foo<N> {
425
+ // Used as an associated constant.
426
+ const CONST: usize = N * 4;
427
+ }
428
+ "# ,
429
+ )
430
+ }
385
431
}
0 commit comments