File tree Expand file tree Collapse file tree 4 files changed +127
-1
lines changed
src/tools/rust-analyzer/crates/ide-completion/src Expand file tree Collapse file tree 4 files changed +127
-1
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,7 @@ pub(crate) fn complete_expr_path(
6161 after_if_expr,
6262 in_condition,
6363 incomplete_let,
64+ in_value,
6465 ref ref_expr_parent,
6566 after_amp,
6667 ref is_func_update,
@@ -361,10 +362,16 @@ pub(crate) fn complete_expr_path(
361362 add_keyword ( "loop" , "loop {\n $0\n }" ) ;
362363 if in_match_guard {
363364 add_keyword ( "if" , "if $0" ) ;
365+ } else if in_value {
366+ add_keyword ( "if" , "if $1 {\n $2\n } else {\n $0\n }" ) ;
364367 } else {
365368 add_keyword ( "if" , "if $1 {\n $0\n }" ) ;
366369 }
367- add_keyword ( "if let" , "if let $1 = $2 {\n $0\n }" ) ;
370+ if in_value {
371+ add_keyword ( "if let" , "if let $1 = $2 {\n $3\n } else {\n $0\n }" ) ;
372+ } else {
373+ add_keyword ( "if let" , "if let $1 = $2 {\n $0\n }" ) ;
374+ }
368375 add_keyword ( "for" , "for $1 in $2 {\n $0\n }" ) ;
369376 add_keyword ( "true" , "true" ) ;
370377 add_keyword ( "false" , "false" ) ;
Original file line number Diff line number Diff line change @@ -238,6 +238,8 @@ fn main() {
238238 r#"
239239fn main() {
240240 let x = if $1 {
241+ $2
242+ } else {
241243 $0
242244};
243245 let y = 92;
@@ -335,6 +337,120 @@ fn main() {
335337 )
336338 }
337339
340+ #[ test]
341+ fn if_completion_in_parameter ( ) {
342+ check_edit (
343+ "if" ,
344+ r"
345+ fn main() {
346+ foo($0)
347+ }
348+ " ,
349+ r"
350+ fn main() {
351+ foo(if $1 {
352+ $2
353+ } else {
354+ $0
355+ })
356+ }
357+ " ,
358+ ) ;
359+
360+ check_edit (
361+ "if" ,
362+ r"
363+ fn main() {
364+ foo($0, 2)
365+ }
366+ " ,
367+ r"
368+ fn main() {
369+ foo(if $1 {
370+ $2
371+ } else {
372+ $0
373+ }, 2)
374+ }
375+ " ,
376+ ) ;
377+
378+ check_edit (
379+ "if" ,
380+ r"
381+ fn main() {
382+ foo(2, $0)
383+ }
384+ " ,
385+ r"
386+ fn main() {
387+ foo(2, if $1 {
388+ $2
389+ } else {
390+ $0
391+ })
392+ }
393+ " ,
394+ ) ;
395+
396+ check_edit (
397+ "if let" ,
398+ r"
399+ fn main() {
400+ foo(2, $0)
401+ }
402+ " ,
403+ r"
404+ fn main() {
405+ foo(2, if let $1 = $2 {
406+ $3
407+ } else {
408+ $0
409+ })
410+ }
411+ " ,
412+ ) ;
413+ }
414+
415+ #[ test]
416+ fn if_completion_in_let_statement ( ) {
417+ check_edit (
418+ "if" ,
419+ r"
420+ fn main() {
421+ let x = $0;
422+ }
423+ " ,
424+ r"
425+ fn main() {
426+ let x = if $1 {
427+ $2
428+ } else {
429+ $0
430+ };
431+ }
432+ " ,
433+ ) ;
434+
435+ check_edit (
436+ "if let" ,
437+ r"
438+ fn main() {
439+ let x = $0;
440+ }
441+ " ,
442+ r"
443+ fn main() {
444+ let x = if let $1 = $2 {
445+ $3
446+ } else {
447+ $0
448+ };
449+ }
450+ " ,
451+ ) ;
452+ }
453+
338454 #[ test]
339455 fn completes_let_in_block ( ) {
340456 check_edit (
Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ pub(crate) struct PathExprCtx<'db> {
147147 /// Whether this expression is the direct condition of an if or while expression
148148 pub ( crate ) in_condition : bool ,
149149 pub ( crate ) incomplete_let : bool ,
150+ pub ( crate ) in_value : bool ,
150151 pub ( crate ) ref_expr_parent : Option < ast:: RefExpr > ,
151152 pub ( crate ) after_amp : bool ,
152153 /// The surrounding RecordExpression we are completing a functional update
Original file line number Diff line number Diff line change @@ -1245,6 +1245,7 @@ fn classify_name_ref<'db>(
12451245 . parent ( )
12461246 . and_then ( ast:: LetStmt :: cast)
12471247 . is_some_and ( |it| it. semicolon_token ( ) . is_none ( ) ) ;
1248+ let in_value = it. parent ( ) . and_then ( Either :: < ast:: LetStmt , ast:: ArgList > :: cast) . is_some ( ) ;
12481249 let impl_ = fetch_immediate_impl ( sema, original_file, expr. syntax ( ) ) ;
12491250
12501251 let in_match_guard = match it. parent ( ) . and_then ( ast:: MatchArm :: cast) {
@@ -1265,6 +1266,7 @@ fn classify_name_ref<'db>(
12651266 is_func_update,
12661267 innermost_ret_ty,
12671268 self_param,
1269+ in_value,
12681270 incomplete_let,
12691271 impl_,
12701272 in_match_guard,
You can’t perform that action at this time.
0 commit comments