@@ -155,22 +155,29 @@ pub(crate) fn complete_postfix(
155
155
postfix_snippet ( "refm" , "&mut expr" , & format ! ( "&mut {receiver_text}" ) ) . add_to ( acc, ctx. db ) ;
156
156
postfix_snippet ( "deref" , "*expr" , & format ! ( "*{receiver_text}" ) ) . add_to ( acc, ctx. db ) ;
157
157
158
- let mut unsafe_should_be_wrapped = true ;
158
+ let mut block_should_be_wrapped = true ;
159
159
if dot_receiver. syntax ( ) . kind ( ) == BLOCK_EXPR {
160
- unsafe_should_be_wrapped = false ;
160
+ block_should_be_wrapped = false ;
161
161
if let Some ( parent) = dot_receiver. syntax ( ) . parent ( ) {
162
162
if matches ! ( parent. kind( ) , IF_EXPR | WHILE_EXPR | LOOP_EXPR | FOR_EXPR ) {
163
- unsafe_should_be_wrapped = true ;
163
+ block_should_be_wrapped = true ;
164
164
}
165
165
}
166
166
} ;
167
- let unsafe_completion_string = if unsafe_should_be_wrapped {
167
+ let unsafe_completion_string = if block_should_be_wrapped {
168
168
format ! ( "unsafe {{ {receiver_text} }}" )
169
169
} else {
170
170
format ! ( "unsafe {receiver_text}" )
171
171
} ;
172
172
postfix_snippet ( "unsafe" , "unsafe {}" , & unsafe_completion_string) . add_to ( acc, ctx. db ) ;
173
173
174
+ let const_completion_string = if block_should_be_wrapped {
175
+ format ! ( "const {{ {receiver_text} }}" )
176
+ } else {
177
+ format ! ( "const {receiver_text}" )
178
+ } ;
179
+ postfix_snippet ( "const" , "const {}" , & const_completion_string) . add_to ( acc, ctx. db ) ;
180
+
174
181
// The rest of the postfix completions create an expression that moves an argument,
175
182
// so it's better to consider references now to avoid breaking the compilation
176
183
@@ -430,6 +437,7 @@ fn main() {
430
437
expect ! [ [ r#"
431
438
sn box Box::new(expr)
432
439
sn call function(expr)
440
+ sn const const {}
433
441
sn dbg dbg!(expr)
434
442
sn dbgr dbg!(&expr)
435
443
sn deref *expr
@@ -463,6 +471,7 @@ fn main() {
463
471
expect ! [ [ r#"
464
472
sn box Box::new(expr)
465
473
sn call function(expr)
474
+ sn const const {}
466
475
sn dbg dbg!(expr)
467
476
sn dbgr dbg!(&expr)
468
477
sn deref *expr
@@ -490,6 +499,7 @@ fn main() {
490
499
expect ! [ [ r#"
491
500
sn box Box::new(expr)
492
501
sn call function(expr)
502
+ sn const const {}
493
503
sn dbg dbg!(expr)
494
504
sn dbgr dbg!(&expr)
495
505
sn deref *expr
@@ -516,6 +526,7 @@ fn main() {
516
526
expect ! [ [ r#"
517
527
sn box Box::new(expr)
518
528
sn call function(expr)
529
+ sn const const {}
519
530
sn dbg dbg!(expr)
520
531
sn dbgr dbg!(&expr)
521
532
sn deref *expr
@@ -653,44 +664,57 @@ fn main() {
653
664
654
665
#[ test]
655
666
fn postfix_completion_for_unsafe ( ) {
656
- check_edit ( "unsafe" , r#"fn main() { foo.$0 }"# , r#"fn main() { unsafe { foo } }"# ) ;
657
- check_edit ( "unsafe" , r#"fn main() { { foo }.$0 }"# , r#"fn main() { unsafe { foo } }"# ) ;
667
+ postfix_completion_for_block ( "unsafe" ) ;
668
+ }
669
+
670
+ #[ test]
671
+ fn postfix_completion_for_const ( ) {
672
+ postfix_completion_for_block ( "const" ) ;
673
+ }
674
+
675
+ fn postfix_completion_for_block ( kind : & str ) {
676
+ check_edit ( kind, r#"fn main() { foo.$0 }"# , & format ! ( "fn main() {{ {kind} {{ foo }} }}" ) ) ;
677
+ check_edit (
678
+ kind,
679
+ r#"fn main() { { foo }.$0 }"# ,
680
+ & format ! ( "fn main() {{ {kind} {{ foo }} }}" ) ,
681
+ ) ;
658
682
check_edit (
659
- "unsafe" ,
683
+ kind ,
660
684
r#"fn main() { if x { foo }.$0 }"# ,
661
- r# "fn main() { unsafe { if x { foo } } }"# ,
685
+ & format ! ( "fn main() {{ {kind} {{ if x {{ foo }} }} }}" ) ,
662
686
) ;
663
687
check_edit (
664
- "unsafe" ,
688
+ kind ,
665
689
r#"fn main() { loop { foo }.$0 }"# ,
666
- r# "fn main() { unsafe { loop { foo } } }"# ,
690
+ & format ! ( "fn main() {{ {kind} {{ loop {{ foo }} }} }}" ) ,
667
691
) ;
668
692
check_edit (
669
- "unsafe" ,
693
+ kind ,
670
694
r#"fn main() { if true {}.$0 }"# ,
671
- r# "fn main() { unsafe { if true {} } }"# ,
695
+ & format ! ( "fn main() {{ {kind} {{ if true {{}} }} }}" ) ,
672
696
) ;
673
697
check_edit (
674
- "unsafe" ,
698
+ kind ,
675
699
r#"fn main() { while true {}.$0 }"# ,
676
- r# "fn main() { unsafe { while true {} } }"# ,
700
+ & format ! ( "fn main() {{ {kind} {{ while true {{}} }} }}" ) ,
677
701
) ;
678
702
check_edit (
679
- "unsafe" ,
703
+ kind ,
680
704
r#"fn main() { for i in 0..10 {}.$0 }"# ,
681
- r# "fn main() { unsafe { for i in 0..10 {} } }"# ,
705
+ & format ! ( "fn main() {{ {kind} {{ for i in 0..10 {{}} }} }}" ) ,
682
706
) ;
683
707
check_edit (
684
- "unsafe" ,
708
+ kind ,
685
709
r#"fn main() { let x = if true {1} else {2}.$0 }"# ,
686
- r# "fn main() { let x = unsafe { if true {1} else {2} } }"# ,
710
+ & format ! ( "fn main() {{ let x = {kind} {{ if true {{1}} else {{2}} }} }}" ) ,
687
711
) ;
688
712
689
713
// completion will not be triggered
690
714
check_edit (
691
- "unsafe" ,
715
+ kind ,
692
716
r#"fn main() { let x = true else {panic!()}.$0}"# ,
693
- r# "fn main() { let x = true else {panic!()}.unsafe $0}"# ,
717
+ & format ! ( "fn main() {{ let x = true else {{ panic!()}}.{kind} $0}}" ) ,
694
718
) ;
695
719
}
696
720
0 commit comments