@@ -10,7 +10,7 @@ use ra_syntax::{
1010} ;
1111use rustc_hash:: { FxHashMap , FxHashSet } ;
1212
13- use crate :: { AssistContext , AssistId , Assists } ;
13+ use crate :: { utils :: render_snippet , AssistContext , AssistId , Assists } ;
1414
1515// Assist: add_function
1616//
@@ -33,7 +33,7 @@ use crate::{AssistContext, AssistId, Assists};
3333// }
3434//
3535// fn bar(arg: &str, baz: Baz) {
36- // todo!()
36+ // ${0: todo!()}
3737// }
3838//
3939// ```
@@ -58,18 +58,27 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
5858 let function_builder = FunctionBuilder :: from_call ( & ctx, & call, & path, target_module) ?;
5959
6060 let target = call. syntax ( ) . text_range ( ) ;
61- acc. add ( AssistId ( "add_function" ) , "Add function" , target, |edit | {
61+ acc. add ( AssistId ( "add_function" ) , "Add function" , target, |builder | {
6262 let function_template = function_builder. render ( ) ;
63- edit. set_file ( function_template. file ) ;
64- edit. set_cursor ( function_template. cursor_offset ) ;
65- edit. insert ( function_template. insert_offset , function_template. fn_def . to_string ( ) ) ;
63+ builder. set_file ( function_template. file ) ;
64+ match ctx. config . snippet_cap {
65+ Some ( cap) => {
66+ let snippet = render_snippet (
67+ function_template. fn_def . syntax ( ) ,
68+ function_template. placeholder_expr . syntax ( ) ,
69+ ) ;
70+ builder. insert_snippet ( cap, function_template. insert_offset , snippet)
71+ }
72+ None => builder
73+ . insert ( function_template. insert_offset , function_template. fn_def . to_string ( ) ) ,
74+ }
6675 } )
6776}
6877
6978struct FunctionTemplate {
7079 insert_offset : TextSize ,
71- cursor_offset : TextSize ,
7280 fn_def : ast:: SourceFile ,
81+ placeholder_expr : ast:: MacroCall ,
7382 file : FileId ,
7483}
7584
@@ -136,9 +145,7 @@ impl FunctionBuilder {
136145
137146 let placeholder_expr =
138147 fn_def. syntax ( ) . descendants ( ) . find_map ( ast:: MacroCall :: cast) . unwrap ( ) ;
139- let cursor_offset_from_fn_start = placeholder_expr. syntax ( ) . text_range ( ) . start ( ) ;
140- let cursor_offset = insert_offset + cursor_offset_from_fn_start;
141- FunctionTemplate { insert_offset, cursor_offset, fn_def, file : self . file }
148+ FunctionTemplate { insert_offset, placeholder_expr, fn_def, file : self . file }
142149 }
143150}
144151
@@ -316,7 +323,7 @@ fn foo() {
316323}
317324
318325fn bar() {
319- <|> todo!()
326+ ${0: todo!()}
320327}
321328" ,
322329 )
@@ -343,7 +350,7 @@ impl Foo {
343350}
344351
345352fn bar() {
346- <|> todo!()
353+ ${0: todo!()}
347354}
348355" ,
349356 )
@@ -367,7 +374,7 @@ fn foo1() {
367374}
368375
369376fn bar() {
370- <|> todo!()
377+ ${0: todo!()}
371378}
372379
373380fn foo2() {}
@@ -393,7 +400,7 @@ mod baz {
393400 }
394401
395402 fn bar() {
396- <|> todo!()
403+ ${0: todo!()}
397404 }
398405}
399406" ,
@@ -419,7 +426,7 @@ fn foo() {
419426}
420427
421428fn bar(baz: Baz) {
422- <|> todo!()
429+ ${0: todo!()}
423430}
424431" ,
425432 ) ;
@@ -452,7 +459,7 @@ impl Baz {
452459}
453460
454461fn bar(baz: Baz) {
455- <|> todo!()
462+ ${0: todo!()}
456463}
457464" ,
458465 )
@@ -473,7 +480,7 @@ fn foo() {
473480}
474481
475482fn bar(arg: &str) {
476- <|> todo!()
483+ ${0: todo!()}
477484}
478485"# ,
479486 )
@@ -494,7 +501,7 @@ fn foo() {
494501}
495502
496503fn bar(arg: char) {
497- <|> todo!()
504+ ${0: todo!()}
498505}
499506"# ,
500507 )
@@ -515,7 +522,7 @@ fn foo() {
515522}
516523
517524fn bar(arg: i32) {
518- <|> todo!()
525+ ${0: todo!()}
519526}
520527" ,
521528 )
@@ -536,7 +543,7 @@ fn foo() {
536543}
537544
538545fn bar(arg: u8) {
539- <|> todo!()
546+ ${0: todo!()}
540547}
541548" ,
542549 )
@@ -561,7 +568,7 @@ fn foo() {
561568}
562569
563570fn bar(x: u8) {
564- <|> todo!()
571+ ${0: todo!()}
565572}
566573" ,
567574 )
@@ -584,7 +591,7 @@ fn foo() {
584591}
585592
586593fn bar(worble: ()) {
587- <|> todo!()
594+ ${0: todo!()}
588595}
589596" ,
590597 )
@@ -613,7 +620,7 @@ fn baz() {
613620}
614621
615622fn bar(foo: impl Foo) {
616- <|> todo!()
623+ ${0: todo!()}
617624}
618625" ,
619626 )
@@ -640,7 +647,7 @@ fn foo() {
640647}
641648
642649fn bar(baz: &Baz) {
643- <|> todo!()
650+ ${0: todo!()}
644651}
645652" ,
646653 )
@@ -669,7 +676,7 @@ fn foo() {
669676}
670677
671678fn bar(baz: Baz::Bof) {
672- <|> todo!()
679+ ${0: todo!()}
673680}
674681" ,
675682 )
@@ -692,7 +699,7 @@ fn foo<T>(t: T) {
692699}
693700
694701fn bar<T>(t: T) {
695- <|> todo!()
702+ ${0: todo!()}
696703}
697704" ,
698705 )
@@ -723,7 +730,7 @@ fn foo() {
723730}
724731
725732fn bar(arg: fn() -> Baz) {
726- <|> todo!()
733+ ${0: todo!()}
727734}
728735" ,
729736 )
@@ -748,7 +755,7 @@ fn foo() {
748755}
749756
750757fn bar(closure: impl Fn(i64) -> i64) {
751- <|> todo!()
758+ ${0: todo!()}
752759}
753760" ,
754761 )
@@ -769,7 +776,7 @@ fn foo() {
769776}
770777
771778fn bar(baz: ()) {
772- <|> todo!()
779+ ${0: todo!()}
773780}
774781" ,
775782 )
@@ -794,7 +801,7 @@ fn foo() {
794801}
795802
796803fn bar(baz_1: Baz, baz_2: Baz) {
797- <|> todo!()
804+ ${0: todo!()}
798805}
799806" ,
800807 )
@@ -819,7 +826,7 @@ fn foo() {
819826}
820827
821828fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
822- <|> todo!()
829+ ${0: todo!()}
823830}
824831"# ,
825832 )
@@ -839,7 +846,7 @@ fn foo() {
839846 r"
840847mod bar {
841848 pub(crate) fn my_fn() {
842- <|> todo!()
849+ ${0: todo!()}
843850 }
844851}
845852
@@ -878,7 +885,7 @@ fn bar() {
878885}
879886
880887fn baz(foo: foo::Foo) {
881- <|> todo!()
888+ ${0: todo!()}
882889}
883890" ,
884891 )
@@ -902,7 +909,7 @@ mod bar {
902909 fn something_else() {}
903910
904911 pub(crate) fn my_fn() {
905- <|> todo!()
912+ ${0: todo!()}
906913 }
907914}
908915
@@ -930,7 +937,7 @@ fn foo() {
930937mod bar {
931938 mod baz {
932939 pub(crate) fn my_fn() {
933- <|> todo!()
940+ ${0: todo!()}
934941 }
935942 }
936943}
@@ -959,7 +966,7 @@ fn main() {
959966
960967
961968pub(crate) fn bar() {
962- <|> todo!()
969+ ${0: todo!()}
963970}" ,
964971 )
965972 }
0 commit comments