aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/completions/postfix.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src/completions/postfix.rs')
-rw-r--r--crates/completion/src/completions/postfix.rs74
1 files changed, 45 insertions, 29 deletions
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs
index 4888f518a..9c34ed0b6 100644
--- a/crates/completion/src/completions/postfix.rs
+++ b/crates/completion/src/completions/postfix.rs
@@ -1,4 +1,4 @@
1//! Postfix completions, like `Ok(10).ifl<|>` => `if let Ok() = Ok(10) { <|> }`. 1//! Postfix completions, like `Ok(10).ifl$0` => `if let Ok() = Ok(10) { $0 }`.
2 2
3mod format_like; 3mod format_like;
4 4
@@ -35,11 +35,14 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
35 None => return, 35 None => return,
36 }; 36 };
37 37
38 let ref_removed_ty =
39 std::iter::successors(Some(receiver_ty.clone()), |ty| ty.remove_ref()).last().unwrap();
40
38 let cap = match ctx.config.snippet_cap { 41 let cap = match ctx.config.snippet_cap {
39 Some(it) => it, 42 Some(it) => it,
40 None => return, 43 None => return,
41 }; 44 };
42 let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty); 45 let try_enum = TryEnum::from_ty(&ctx.sema, &ref_removed_ty);
43 if let Some(try_enum) = &try_enum { 46 if let Some(try_enum) = &try_enum {
44 match try_enum { 47 match try_enum {
45 TryEnum::Result => { 48 TryEnum::Result => {
@@ -310,7 +313,7 @@ mod tests {
310 r#" 313 r#"
311fn main() { 314fn main() {
312 let bar = true; 315 let bar = true;
313 bar.<|> 316 bar.$0
314} 317}
315"#, 318"#,
316 expect![[r#" 319 expect![[r#"
@@ -342,7 +345,7 @@ fn foo(elt: bool) -> bool {
342 345
343fn main() { 346fn main() {
344 let bar = true; 347 let bar = true;
345 foo(bar.<|>) 348 foo(bar.$0)
346} 349}
347"#, 350"#,
348 expect![[r#" 351 expect![[r#"
@@ -368,7 +371,7 @@ fn main() {
368 r#" 371 r#"
369fn main() { 372fn main() {
370 let bar: u8 = 12; 373 let bar: u8 = 12;
371 bar.<|> 374 bar.$0
372} 375}
373"#, 376"#,
374 expect![[r#" 377 expect![[r#"
@@ -392,7 +395,7 @@ fn main() {
392 check( 395 check(
393 r#" 396 r#"
394fn main() { 397fn main() {
395 baz.l<|> 398 baz.l$0
396 res 399 res
397} 400}
398"#, 401"#,
@@ -424,7 +427,7 @@ enum Option<T> { Some(T), None }
424 427
425fn main() { 428fn main() {
426 let bar = Option::Some(true); 429 let bar = Option::Some(true);
427 bar.<|> 430 bar.$0
428} 431}
429"#, 432"#,
430 r#" 433 r#"
@@ -449,7 +452,7 @@ enum Result<T, E> { Ok(T), Err(E) }
449 452
450fn main() { 453fn main() {
451 let bar = Result::Ok(true); 454 let bar = Result::Ok(true);
452 bar.<|> 455 bar.$0
453} 456}
454"#, 457"#,
455 r#" 458 r#"
@@ -468,7 +471,7 @@ fn main() {
468 471
469 #[test] 472 #[test]
470 fn postfix_completion_works_for_ambiguous_float_literal() { 473 fn postfix_completion_works_for_ambiguous_float_literal() {
471 check_edit("refm", r#"fn main() { 42.<|> }"#, r#"fn main() { &mut 42 }"#) 474 check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
472 } 475 }
473 476
474 #[test] 477 #[test]
@@ -479,7 +482,7 @@ fn main() {
479macro_rules! m { ($e:expr) => { $e } } 482macro_rules! m { ($e:expr) => { $e } }
480fn main() { 483fn main() {
481 let bar: u8 = 12; 484 let bar: u8 = 12;
482 m!(bar.d<|>) 485 m!(bar.d$0)
483} 486}
484"#, 487"#,
485 r#" 488 r#"
@@ -494,55 +497,68 @@ fn main() {
494 497
495 #[test] 498 #[test]
496 fn postfix_completion_for_references() { 499 fn postfix_completion_for_references() {
497 check_edit("dbg", r#"fn main() { &&42.<|> }"#, r#"fn main() { dbg!(&&42) }"#); 500 check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
498 check_edit("refm", r#"fn main() { &&42.<|> }"#, r#"fn main() { &&&mut 42 }"#); 501 check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
502 check_edit(
503 "ifl",
504 r#"
505enum Option<T> { Some(T), None }
506
507fn main() {
508 let bar = &Option::Some(true);
509 bar.$0
510}
511"#,
512 r#"
513enum Option<T> { Some(T), None }
514
515fn main() {
516 let bar = &Option::Some(true);
517 if let Some($1) = bar {
518 $0
519}
520}
521"#,
522 )
499 } 523 }
500 524
501 #[test] 525 #[test]
502 fn postfix_completion_for_format_like_strings() { 526 fn postfix_completion_for_format_like_strings() {
503 check_edit( 527 check_edit(
504 "format", 528 "format",
505 r#"fn main() { "{some_var:?}".<|> }"#, 529 r#"fn main() { "{some_var:?}".$0 }"#,
506 r#"fn main() { format!("{:?}", some_var) }"#, 530 r#"fn main() { format!("{:?}", some_var) }"#,
507 ); 531 );
508 check_edit( 532 check_edit(
509 "panic", 533 "panic",
510 r#"fn main() { "Panic with {a}".<|> }"#, 534 r#"fn main() { "Panic with {a}".$0 }"#,
511 r#"fn main() { panic!("Panic with {}", a) }"#, 535 r#"fn main() { panic!("Panic with {}", a) }"#,
512 ); 536 );
513 check_edit( 537 check_edit(
514 "println", 538 "println",
515 r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".<|> }"#, 539 r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
516 r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#, 540 r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
517 ); 541 );
518 check_edit( 542 check_edit(
519 "loge", 543 "loge",
520 r#"fn main() { "{2+2}".<|> }"#, 544 r#"fn main() { "{2+2}".$0 }"#,
521 r#"fn main() { log::error!("{}", 2+2) }"#, 545 r#"fn main() { log::error!("{}", 2+2) }"#,
522 ); 546 );
523 check_edit( 547 check_edit(
524 "logt", 548 "logt",
525 r#"fn main() { "{2+2}".<|> }"#, 549 r#"fn main() { "{2+2}".$0 }"#,
526 r#"fn main() { log::trace!("{}", 2+2) }"#, 550 r#"fn main() { log::trace!("{}", 2+2) }"#,
527 ); 551 );
528 check_edit( 552 check_edit(
529 "logd", 553 "logd",
530 r#"fn main() { "{2+2}".<|> }"#, 554 r#"fn main() { "{2+2}".$0 }"#,
531 r#"fn main() { log::debug!("{}", 2+2) }"#, 555 r#"fn main() { log::debug!("{}", 2+2) }"#,
532 ); 556 );
533 check_edit( 557 check_edit("logi", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::info!("{}", 2+2) }"#);
534 "logi", 558 check_edit("logw", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::warn!("{}", 2+2) }"#);
535 r#"fn main() { "{2+2}".<|> }"#,
536 r#"fn main() { log::info!("{}", 2+2) }"#,
537 );
538 check_edit(
539 "logw",
540 r#"fn main() { "{2+2}".<|> }"#,
541 r#"fn main() { log::warn!("{}", 2+2) }"#,
542 );
543 check_edit( 559 check_edit(
544 "loge", 560 "loge",
545 r#"fn main() { "{2+2}".<|> }"#, 561 r#"fn main() { "{2+2}".$0 }"#,
546 r#"fn main() { log::error!("{}", 2+2) }"#, 562 r#"fn main() { log::error!("{}", 2+2) }"#,
547 ); 563 );
548 } 564 }