@@ -44,7 +44,7 @@ use syntax::{
4444// };
4545// }
4646//
47- // trait NewTrait <const N: usize> {
47+ // trait ${0:TraitName} <const N: usize> {
4848// // Used as an associated constant.
4949// const CONST_ASSOC: usize = N * 4;
5050//
@@ -53,7 +53,7 @@ use syntax::{
5353// const_maker! {i32, 7}
5454// }
5555//
56- // impl<const N: usize> NewTrait <N> for Foo<N> {
56+ // impl<const N: usize> ${0:TraitName} <N> for Foo<N> {
5757// // Used as an associated constant.
5858// const CONST_ASSOC: usize = N * 4;
5959//
@@ -126,23 +126,41 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
126126 "" . to_string ( )
127127 } ;
128128
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+ }
134162
135163 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- ) ;
146164 } ,
147165 ) ;
148166
@@ -193,7 +211,7 @@ fn strip_body(item: &ast::AssocItem) {
193211#[ cfg( test) ]
194212mod tests {
195213 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} ;
197215
198216 #[ test]
199217 fn test_trigger_when_cursor_on_header ( ) {
@@ -212,7 +230,7 @@ impl Foo { $0
212230
213231 #[ test]
214232 fn test_assoc_item_fn ( ) {
215- check_assist (
233+ check_assist_no_snippet_cap (
216234 generate_trait_from_impl,
217235 r#"
218236struct Foo(f64);
@@ -239,7 +257,7 @@ impl NewTrait for Foo {
239257
240258 #[ test]
241259 fn test_assoc_item_macro ( ) {
242- check_assist (
260+ check_assist_no_snippet_cap (
243261 generate_trait_from_impl,
244262 r#"
245263struct Foo;
@@ -274,7 +292,7 @@ impl NewTrait for Foo {
274292
275293 #[ test]
276294 fn test_assoc_item_const ( ) {
277- check_assist (
295+ check_assist_no_snippet_cap (
278296 generate_trait_from_impl,
279297 r#"
280298struct Foo;
@@ -297,7 +315,7 @@ impl NewTrait for Foo {
297315
298316 #[ test]
299317 fn test_impl_with_generics ( ) {
300- check_assist (
318+ check_assist_no_snippet_cap (
301319 generate_trait_from_impl,
302320 r#"
303321struct Foo<const N: usize>([i32; N]);
@@ -325,7 +343,7 @@ impl<const N: usize> NewTrait<N> for Foo<N> {
325343
326344 #[ test]
327345 fn test_trait_items_should_not_have_vis ( ) {
328- check_assist (
346+ check_assist_no_snippet_cap (
329347 generate_trait_from_impl,
330348 r#"
331349struct Foo;
@@ -362,7 +380,7 @@ impl Emp$0tyImpl{}
362380
363381 #[ test]
364382 fn test_not_top_level_impl ( ) {
365- check_assist (
383+ check_assist_no_snippet_cap (
366384 generate_trait_from_impl,
367385 r#"
368386mod a {
@@ -382,4 +400,32 @@ mod a {
382400}"# ,
383401 )
384402 }
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+ }
385431}
0 commit comments