@@ -11,25 +11,25 @@ use syntax::{
1111
1212use crate :: { AssistContext , AssistId , Assists } ;
1313
14- // Assist: add_lifetime_to_type
14+ // Assist: add_missing_lifetime
1515//
16- // Adds a new lifetime to a struct, enum or union.
16+ // Adds missing lifetimes to a struct, enum or union.
1717//
1818// ```
19- // struct Point {
20- // x: &$0u32 ,
21- // y: u32,
19+ // struct $0Foo<T> {
20+ // x: &'a i32 ,
21+ // y: &T
2222// }
2323// ```
2424// ->
2525// ```
26- // struct Point< ${1:'l}> {
27- // x: &${0:'l} u32 ,
28- // y: u32,
26+ // struct Foo<'a, ${1:'l}, T > {
27+ // x: &'a i32 ,
28+ // y: &${0:'l} T
2929// }
3030// ```
3131
32- pub ( crate ) fn add_lifetime_to_type ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
32+ pub ( crate ) fn add_missing_lifetime ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
3333 let node = ctx. find_node_at_offset :: < ast:: Adt > ( ) ?;
3434 let all_inner_refs = fetch_all_refs ( & node) ?;
3535 let ( refs_without_lifetime, refs_with_lifetime) : ( Vec < _ > , Vec < _ > ) =
@@ -71,14 +71,13 @@ fn add_and_declare_lifetimes(
7171 let has_undeclared_lifetimes = !adt_undeclared_lifetimes. is_empty ( ) ;
7272
7373 let message = match ( has_refs_without_lifetime, has_undeclared_lifetimes) {
74- ( false , true ) => "Declare used lifetimes in type definition" ,
75- ( true , false ) => "Add lifetime to type" ,
76- ( true , true ) => "Declare used lifetimes and add new lifetime" ,
74+ ( false , true ) => "Declare used lifetimes in generic parameters" ,
75+ ( true , false ) | ( true , true ) => "Add missing lifetimes" ,
7776 _ => return None ,
7877 } ;
7978
8079 acc. add (
81- AssistId :: quick_fix ( "add_lifetime_to_type " ) ,
80+ AssistId :: quick_fix ( "add_missing_lifetime " ) ,
8281 message,
8382 node. syntax ( ) . text_range ( ) ,
8483 |builder| {
@@ -226,7 +225,7 @@ mod tests {
226225 #[ test]
227226 fn add_lifetime ( ) {
228227 check_assist (
229- add_lifetime_to_type ,
228+ add_missing_lifetime ,
230229 r#"
231230struct Foo {
232231 a: &$0i32,
@@ -240,7 +239,7 @@ struct Foo<${1:'l}> {
240239 ) ;
241240
242241 check_assist (
243- add_lifetime_to_type ,
242+ add_missing_lifetime ,
244243 r#"
245244enum Foo {
246245 Bar { a: i32 },
@@ -256,7 +255,7 @@ enum Foo<${1:'l}> {
256255 ) ;
257256
258257 check_assist (
259- add_lifetime_to_type ,
258+ add_missing_lifetime ,
260259 r#"
261260union Foo<T> {
262261 a: &$0T,
@@ -273,7 +272,7 @@ union Foo<${1:'l}, T> {
273272 #[ test]
274273 fn add_lifetime_to_struct ( ) {
275274 check_assist (
276- add_lifetime_to_type ,
275+ add_missing_lifetime ,
277276 r#"
278277struct Foo {
279278 a: &$0i32
@@ -285,7 +284,7 @@ struct Foo<${1:'l}> {
285284 ) ;
286285
287286 check_assist (
288- add_lifetime_to_type ,
287+ add_missing_lifetime ,
289288 r#"
290289struct Foo {
291290 a: &$0i32,
@@ -299,7 +298,7 @@ struct Foo<${1:'l}> {
299298 ) ;
300299
301300 check_assist (
302- add_lifetime_to_type ,
301+ add_missing_lifetime ,
303302 r#"
304303struct Foo {
305304 a: &$0i32,
@@ -313,7 +312,7 @@ struct Foo<${1:'l}> {
313312 ) ;
314313
315314 check_assist (
316- add_lifetime_to_type ,
315+ add_missing_lifetime ,
317316 r#"
318317struct Foo<T> {
319318 a: &$0T,
@@ -327,7 +326,7 @@ struct Foo<${1:'l}, T> {
327326 ) ;
328327
329328 check_assist (
330- add_lifetime_to_type ,
329+ add_missing_lifetime ,
331330 r#"
332331struct Foo {
333332 a: &'a$0 i32
@@ -338,13 +337,13 @@ struct Foo<'a> {
338337}"# ,
339338 ) ;
340339
341- check_assist_not_applicable ( add_lifetime_to_type , r#"struct Foo<'a> { a: &$0'a i32 }"# ) ;
340+ check_assist_not_applicable ( add_missing_lifetime , r#"struct Foo<'a> { a: &$0'a i32 }"# ) ;
342341 }
343342
344343 #[ test]
345344 fn add_lifetime_to_enum ( ) {
346345 check_assist (
347- add_lifetime_to_type ,
346+ add_missing_lifetime ,
348347 r#"
349348enum Foo {
350349 Bar { a: i32 },
@@ -360,7 +359,7 @@ enum Foo<${1:'l}> {
360359 ) ;
361360
362361 check_assist (
363- add_lifetime_to_type ,
362+ add_missing_lifetime ,
364363 r#"
365364enum Foo {
366365 Bar { a: &$0i32 }
@@ -372,7 +371,7 @@ enum Foo<${1:'l}> {
372371 ) ;
373372
374373 check_assist (
375- add_lifetime_to_type ,
374+ add_missing_lifetime ,
376375 r#"
377376enum Foo<T> {
378377 Bar {
@@ -390,16 +389,16 @@ enum Foo<${1:'l}, T> {
390389 ) ;
391390
392391 check_assist_not_applicable (
393- add_lifetime_to_type ,
392+ add_missing_lifetime ,
394393 r#"enum Foo<'a> { Bar { a: &$0'a i32 }}"# ,
395394 ) ;
396- check_assist_not_applicable ( add_lifetime_to_type , r#"enum Foo { Bar, $0Misc }"# ) ;
395+ check_assist_not_applicable ( add_missing_lifetime , r#"enum Foo { Bar, $0Misc }"# ) ;
397396 }
398397
399398 #[ test]
400399 fn add_lifetime_to_union ( ) {
401400 check_assist (
402- add_lifetime_to_type ,
401+ add_missing_lifetime ,
403402 r#"
404403union Foo {
405404 a: &$0i32
@@ -411,7 +410,7 @@ union Foo<${1:'l}> {
411410 ) ;
412411
413412 check_assist (
414- add_lifetime_to_type ,
413+ add_missing_lifetime ,
415414 r#"
416415union Foo {
417416 a: &$0i32,
@@ -425,7 +424,7 @@ union Foo<${1:'l}> {
425424 ) ;
426425
427426 check_assist (
428- add_lifetime_to_type ,
427+ add_missing_lifetime ,
429428 r#"
430429union Foo<T> {
431430 a: &$0T,
@@ -438,13 +437,13 @@ union Foo<${1:'l}, T> {
438437}"# ,
439438 ) ;
440439
441- check_assist_not_applicable ( add_lifetime_to_type , r#"struct Foo<'a> { a: &'a $0i32 }"# ) ;
440+ check_assist_not_applicable ( add_missing_lifetime , r#"struct Foo<'a> { a: &'a $0i32 }"# ) ;
442441 }
443442
444443 #[ test]
445444 fn declare_undeclared_lifetimes ( ) {
446445 check_assist (
447- add_lifetime_to_type ,
446+ add_missing_lifetime ,
448447 r#"
449448struct $0Foo {
450449 x: &'a i32
@@ -455,7 +454,7 @@ struct Foo<'a> {
455454}"# ,
456455 ) ;
457456 check_assist (
458- add_lifetime_to_type ,
457+ add_missing_lifetime ,
459458 r#"
460459struct $0Foo {
461460 x: &'a i32,
@@ -469,7 +468,7 @@ struct Foo<'a, 'b> {
469468 ) ;
470469
471470 check_assist (
472- add_lifetime_to_type ,
471+ add_missing_lifetime ,
473472 r#"
474473struct $0Foo<T> {
475474 x: &'a T
@@ -480,7 +479,7 @@ struct Foo<'a, T> {
480479}"# ,
481480 ) ;
482481 check_assist (
483- add_lifetime_to_type ,
482+ add_missing_lifetime ,
484483 r#"
485484enum $0Foo<T> {
486485 Bar {
@@ -501,7 +500,7 @@ enum Foo<'a, 'b, T> {
501500 #[ test]
502501 fn add_lifetime_with_existing_declared ( ) {
503502 check_assist (
504- add_lifetime_to_type ,
503+ add_missing_lifetime ,
505504 r#"
506505struct Foo<'a> {
507506 x: &'a i32,
@@ -515,7 +514,7 @@ struct Foo<${1:'l}, 'a> {
515514 ) ;
516515
517516 check_assist (
518- add_lifetime_to_type ,
517+ add_missing_lifetime ,
519518 r#"
520519enum Foo<'a> {
521520 Bar {
@@ -536,7 +535,7 @@ enum Foo<${1:'l}, 'a> {
536535 #[ test]
537536 fn declare_undeclared_and_add_new ( ) {
538537 check_assist (
539- add_lifetime_to_type ,
538+ add_missing_lifetime ,
540539 r#"
541540struct $0Foo {
542541 x: &'a i32,
@@ -549,7 +548,7 @@ struct Foo<'a, ${1:'l}> {
549548}"# ,
550549 ) ;
551550 check_assist (
552- add_lifetime_to_type ,
551+ add_missing_lifetime ,
553552 r#"
554553struct $0Foo<T> {
555554 x: &'a i32,
@@ -562,7 +561,7 @@ struct Foo<'a, ${1:'l}, T> {
562561}"# ,
563562 ) ;
564563 check_assist (
565- add_lifetime_to_type ,
564+ add_missing_lifetime ,
566565 r#"
567566enum $0Foo {
568567 Bar { x: &'a i32 },
@@ -578,13 +577,13 @@ enum Foo<'a, ${1:'l}> {
578577
579578 #[ test]
580579 fn not_applicable_when_all_correct ( ) {
581- check_assist_not_applicable ( add_lifetime_to_type , r#"struct $0Foo<'a> { x: &'a i32 }"# ) ;
580+ check_assist_not_applicable ( add_missing_lifetime , r#"struct $0Foo<'a> { x: &'a i32 }"# ) ;
582581 check_assist_not_applicable (
583- add_lifetime_to_type ,
582+ add_missing_lifetime ,
584583 r#"struct $0Foo<'a, 'b> { x: &'a i32, y: &'b u32 }"# ,
585584 ) ;
586585 check_assist_not_applicable (
587- add_lifetime_to_type ,
586+ add_missing_lifetime ,
588587 r#"enum $0Foo<'a> { Bar { x: &'a i32 } }"# ,
589588 ) ;
590589 }
0 commit comments