@@ -12,7 +12,6 @@ use ide_db::{
12
12
use stdx:: never;
13
13
use syntax:: {
14
14
ast:: { self , make, AstNode , AstToken } ,
15
- format_smolstr,
16
15
SyntaxKind :: { BLOCK_EXPR , EXPR_STMT , FOR_EXPR , IF_EXPR , LOOP_EXPR , STMT_LIST , WHILE_EXPR } ,
17
16
TextRange , TextSize ,
18
17
} ;
@@ -56,11 +55,10 @@ pub(crate) fn complete_postfix(
56
55
None => return ,
57
56
} ;
58
57
59
- let postfix_snippet =
60
- match build_postfix_snippet_builder ( ctx, cap, dot_receiver, & receiver_text) {
61
- Some ( it) => it,
62
- None => return ,
63
- } ;
58
+ let postfix_snippet = match build_postfix_snippet_builder ( ctx, cap, dot_receiver) {
59
+ Some ( it) => it,
60
+ None => return ,
61
+ } ;
64
62
65
63
if let Some ( drop_trait) = ctx. famous_defs ( ) . core_ops_Drop ( ) {
66
64
if receiver_ty. impls_trait ( ctx. db , drop_trait, & [ ] ) {
@@ -175,11 +173,10 @@ pub(crate) fn complete_postfix(
175
173
let ( dot_receiver, node_to_replace_with) = include_references ( dot_receiver) ;
176
174
let receiver_text =
177
175
get_receiver_text ( & node_to_replace_with, receiver_is_ambiguous_float_literal) ;
178
- let postfix_snippet =
179
- match build_postfix_snippet_builder ( ctx, cap, & dot_receiver, & receiver_text) {
180
- Some ( it) => it,
181
- None => return ,
182
- } ;
176
+ let postfix_snippet = match build_postfix_snippet_builder ( ctx, cap, & dot_receiver) {
177
+ Some ( it) => it,
178
+ None => return ,
179
+ } ;
183
180
184
181
if !ctx. config . snippets . is_empty ( ) {
185
182
add_custom_postfix_completions ( acc, ctx, & postfix_snippet, & receiver_text) ;
@@ -320,7 +317,6 @@ fn build_postfix_snippet_builder<'ctx>(
320
317
ctx : & ' ctx CompletionContext < ' _ > ,
321
318
cap : SnippetCap ,
322
319
receiver : & ' ctx ast:: Expr ,
323
- receiver_text : & ' ctx str ,
324
320
) -> Option < impl Fn ( & str , & str , & str ) -> Builder + ' ctx > {
325
321
let receiver_range = ctx. sema . original_range_opt ( receiver. syntax ( ) ) ?. range ;
326
322
if ctx. source_range ( ) . end ( ) < receiver_range. start ( ) {
@@ -336,16 +332,13 @@ fn build_postfix_snippet_builder<'ctx>(
336
332
fn build < ' ctx > (
337
333
ctx : & ' ctx CompletionContext < ' _ > ,
338
334
cap : SnippetCap ,
339
- receiver_text : & ' ctx str ,
340
335
delete_range : TextRange ,
341
336
) -> impl Fn ( & str , & str , & str ) -> Builder + ' ctx {
342
337
move |label, detail, snippet| {
343
338
let edit = TextEdit :: replace ( delete_range, snippet. to_owned ( ) ) ;
344
- let mut item = CompletionItem :: new ( CompletionItemKind :: Snippet , delete_range, label) ;
339
+ let mut item =
340
+ CompletionItem :: new ( CompletionItemKind :: Snippet , ctx. source_range ( ) , label) ;
345
341
item. detail ( detail) . snippet_edit ( cap, edit) ;
346
- // Editors may filter completion item with the text within delete_range, so we need to
347
- // include the receiver text in the lookup for editors to find the completion item.
348
- item. lookup_by ( format_smolstr ! ( "{}.{}" , receiver_text, label) ) ;
349
342
let postfix_match = if ctx. original_token . text ( ) == label {
350
343
cov_mark:: hit!( postfix_exact_match_is_high_priority) ;
351
344
Some ( CompletionRelevancePostfixMatch :: Exact )
@@ -358,7 +351,7 @@ fn build_postfix_snippet_builder<'ctx>(
358
351
item
359
352
}
360
353
}
361
- Some ( build ( ctx, cap, receiver_text , delete_range) )
354
+ Some ( build ( ctx, cap, delete_range) )
362
355
}
363
356
364
357
fn add_custom_postfix_completions (
@@ -519,7 +512,7 @@ fn main() {
519
512
#[ test]
520
513
fn option_iflet ( ) {
521
514
check_edit (
522
- "bar. ifl" ,
515
+ "ifl" ,
523
516
r#"
524
517
//- minicore: option
525
518
fn main() {
@@ -541,7 +534,7 @@ fn main() {
541
534
#[ test]
542
535
fn option_letelse ( ) {
543
536
check_edit (
544
- "bar. lete" ,
537
+ "lete" ,
545
538
r#"
546
539
//- minicore: option
547
540
fn main() {
564
557
#[ test]
565
558
fn result_match ( ) {
566
559
check_edit (
567
- "bar. match" ,
560
+ "match" ,
568
561
r#"
569
562
//- minicore: result
570
563
fn main() {
@@ -586,13 +579,13 @@ fn main() {
586
579
587
580
#[ test]
588
581
fn postfix_completion_works_for_ambiguous_float_literal ( ) {
589
- check_edit ( "42. refm" , r#"fn main() { 42.$0 }"# , r#"fn main() { &mut 42 }"# )
582
+ check_edit ( "refm" , r#"fn main() { 42.$0 }"# , r#"fn main() { &mut 42 }"# )
590
583
}
591
584
592
585
#[ test]
593
586
fn works_in_simple_macro ( ) {
594
587
check_edit (
595
- "bar. dbg" ,
588
+ "dbg" ,
596
589
r#"
597
590
macro_rules! m { ($e:expr) => { $e } }
598
591
fn main() {
@@ -612,10 +605,10 @@ fn main() {
612
605
613
606
#[ test]
614
607
fn postfix_completion_for_references ( ) {
615
- check_edit ( "&&42. dbg" , r#"fn main() { &&42.$0 }"# , r#"fn main() { dbg!(&&42) }"# ) ;
616
- check_edit ( "42. refm" , r#"fn main() { &&42.$0 }"# , r#"fn main() { &&&mut 42 }"# ) ;
608
+ check_edit ( "dbg" , r#"fn main() { &&42.$0 }"# , r#"fn main() { dbg!(&&42) }"# ) ;
609
+ check_edit ( "refm" , r#"fn main() { &&42.$0 }"# , r#"fn main() { &&&mut 42 }"# ) ;
617
610
check_edit (
618
- "bar. ifl" ,
611
+ "ifl" ,
619
612
r#"
620
613
//- minicore: option
621
614
fn main() {
@@ -636,39 +629,35 @@ fn main() {
636
629
637
630
#[ test]
638
631
fn postfix_completion_for_unsafe ( ) {
639
- check_edit ( "foo.unsafe" , r#"fn main() { foo.$0 }"# , r#"fn main() { unsafe { foo } }"# ) ;
640
- check_edit (
641
- "{ foo }.unsafe" ,
642
- r#"fn main() { { foo }.$0 }"# ,
643
- r#"fn main() { unsafe { foo } }"# ,
644
- ) ;
632
+ check_edit ( "unsafe" , r#"fn main() { foo.$0 }"# , r#"fn main() { unsafe { foo } }"# ) ;
633
+ check_edit ( "unsafe" , r#"fn main() { { foo }.$0 }"# , r#"fn main() { unsafe { foo } }"# ) ;
645
634
check_edit (
646
- "if x { foo }. unsafe" ,
635
+ "unsafe" ,
647
636
r#"fn main() { if x { foo }.$0 }"# ,
648
637
r#"fn main() { unsafe { if x { foo } } }"# ,
649
638
) ;
650
639
check_edit (
651
- "loop { foo }. unsafe" ,
640
+ "unsafe" ,
652
641
r#"fn main() { loop { foo }.$0 }"# ,
653
642
r#"fn main() { unsafe { loop { foo } } }"# ,
654
643
) ;
655
644
check_edit (
656
- "if true {}. unsafe" ,
645
+ "unsafe" ,
657
646
r#"fn main() { if true {}.$0 }"# ,
658
647
r#"fn main() { unsafe { if true {} } }"# ,
659
648
) ;
660
649
check_edit (
661
- "while true {}. unsafe" ,
650
+ "unsafe" ,
662
651
r#"fn main() { while true {}.$0 }"# ,
663
652
r#"fn main() { unsafe { while true {} } }"# ,
664
653
) ;
665
654
check_edit (
666
- "for i in 0..10 {}. unsafe" ,
655
+ "unsafe" ,
667
656
r#"fn main() { for i in 0..10 {}.$0 }"# ,
668
657
r#"fn main() { unsafe { for i in 0..10 {} } }"# ,
669
658
) ;
670
659
check_edit (
671
- "if true {1} else {2}. unsafe" ,
660
+ "unsafe" ,
672
661
r#"fn main() { let x = if true {1} else {2}.$0 }"# ,
673
662
r#"fn main() { let x = unsafe { if true {1} else {2} } }"# ,
674
663
) ;
@@ -698,7 +687,7 @@ fn main() {
698
687
699
688
check_edit_with_config (
700
689
config. clone ( ) ,
701
- "42. break" ,
690
+ "break" ,
702
691
r#"
703
692
//- minicore: try
704
693
fn main() { 42.$0 }
@@ -717,7 +706,7 @@ fn main() { ControlFlow::Break(42) }
717
706
// what users would see. Unescaping happens thereafter.
718
707
check_edit_with_config (
719
708
config. clone ( ) ,
720
- r#"'\\\\'. break"# ,
709
+ " break",
721
710
r#"
722
711
//- minicore: try
723
712
fn main() { '\\'.$0 }
@@ -731,10 +720,7 @@ fn main() { ControlFlow::Break('\\\\') }
731
720
732
721
check_edit_with_config (
733
722
config,
734
- r#"match true {
735
- true => "\${1:placeholder}",
736
- false => "\\\$",
737
- }.break"# ,
723
+ "break" ,
738
724
r#"
739
725
//- minicore: try
740
726
fn main() {
@@ -760,47 +746,39 @@ fn main() {
760
746
#[ test]
761
747
fn postfix_completion_for_format_like_strings ( ) {
762
748
check_edit (
763
- r#""{some_var:?}". format"# ,
749
+ " format",
764
750
r#"fn main() { "{some_var:?}".$0 }"# ,
765
751
r#"fn main() { format!("{some_var:?}") }"# ,
766
752
) ;
767
753
check_edit (
768
- r#""Panic with {a}". panic"# ,
754
+ " panic",
769
755
r#"fn main() { "Panic with {a}".$0 }"# ,
770
756
r#"fn main() { panic!("Panic with {a}") }"# ,
771
757
) ;
772
758
check_edit (
773
- r#""{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}". println"# ,
759
+ " println",
774
760
r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"# ,
775
761
r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"# ,
776
762
) ;
777
763
check_edit (
778
- r#""{2+2}". loge"# ,
764
+ " loge",
779
765
r#"fn main() { "{2+2}".$0 }"# ,
780
766
r#"fn main() { log::error!("{}", 2+2) }"# ,
781
767
) ;
782
768
check_edit (
783
- r#""{2+2}". logt"# ,
769
+ " logt",
784
770
r#"fn main() { "{2+2}".$0 }"# ,
785
771
r#"fn main() { log::trace!("{}", 2+2) }"# ,
786
772
) ;
787
773
check_edit (
788
- r#""{2+2}". logd"# ,
774
+ " logd",
789
775
r#"fn main() { "{2+2}".$0 }"# ,
790
776
r#"fn main() { log::debug!("{}", 2+2) }"# ,
791
777
) ;
778
+ check_edit ( "logi" , r#"fn main() { "{2+2}".$0 }"# , r#"fn main() { log::info!("{}", 2+2) }"# ) ;
779
+ check_edit ( "logw" , r#"fn main() { "{2+2}".$0 }"# , r#"fn main() { log::warn!("{}", 2+2) }"# ) ;
792
780
check_edit (
793
- r#""{2+2}".logi"# ,
794
- r#"fn main() { "{2+2}".$0 }"# ,
795
- r#"fn main() { log::info!("{}", 2+2) }"# ,
796
- ) ;
797
- check_edit (
798
- r#""{2+2}".logw"# ,
799
- r#"fn main() { "{2+2}".$0 }"# ,
800
- r#"fn main() { log::warn!("{}", 2+2) }"# ,
801
- ) ;
802
- check_edit (
803
- r#""{2+2}".loge"# ,
781
+ "loge" ,
804
782
r#"fn main() { "{2+2}".$0 }"# ,
805
783
r#"fn main() { log::error!("{}", 2+2) }"# ,
806
784
) ;
@@ -822,21 +800,21 @@ fn main() {
822
800
823
801
check_edit_with_config (
824
802
CompletionConfig { snippets : vec ! [ snippet. clone( ) ] , ..TEST_CONFIG } ,
825
- "&&42. ok" ,
803
+ "ok" ,
826
804
r#"fn main() { &&42.o$0 }"# ,
827
805
r#"fn main() { Ok(&&42) }"# ,
828
806
) ;
829
807
830
808
check_edit_with_config (
831
809
CompletionConfig { snippets : vec ! [ snippet. clone( ) ] , ..TEST_CONFIG } ,
832
- "&&42. ok" ,
810
+ "ok" ,
833
811
r#"fn main() { &&42.$0 }"# ,
834
812
r#"fn main() { Ok(&&42) }"# ,
835
813
) ;
836
814
837
815
check_edit_with_config (
838
816
CompletionConfig { snippets : vec ! [ snippet] , ..TEST_CONFIG } ,
839
- "&a.a. ok" ,
817
+ "ok" ,
840
818
r#"
841
819
struct A {
842
820
a: i32,
0 commit comments