File tree Expand file tree Collapse file tree 4 files changed +127
-1
lines changed
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(
61
61
after_if_expr,
62
62
in_condition,
63
63
incomplete_let,
64
+ in_value,
64
65
ref ref_expr_parent,
65
66
after_amp,
66
67
ref is_func_update,
@@ -361,10 +362,16 @@ pub(crate) fn complete_expr_path(
361
362
add_keyword ( "loop" , "loop {\n $0\n }" ) ;
362
363
if in_match_guard {
363
364
add_keyword ( "if" , "if $0" ) ;
365
+ } else if in_value {
366
+ add_keyword ( "if" , "if $1 {\n $2\n } else {\n $0\n }" ) ;
364
367
} else {
365
368
add_keyword ( "if" , "if $1 {\n $0\n }" ) ;
366
369
}
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
+ }
368
375
add_keyword ( "for" , "for $1 in $2 {\n $0\n }" ) ;
369
376
add_keyword ( "true" , "true" ) ;
370
377
add_keyword ( "false" , "false" ) ;
Original file line number Diff line number Diff line change @@ -238,6 +238,8 @@ fn main() {
238
238
r#"
239
239
fn main() {
240
240
let x = if $1 {
241
+ $2
242
+ } else {
241
243
$0
242
244
};
243
245
let y = 92;
@@ -335,6 +337,120 @@ fn main() {
335
337
)
336
338
}
337
339
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
+
338
454
#[ test]
339
455
fn completes_let_in_block ( ) {
340
456
check_edit (
Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ pub(crate) struct PathExprCtx<'db> {
147
147
/// Whether this expression is the direct condition of an if or while expression
148
148
pub ( crate ) in_condition : bool ,
149
149
pub ( crate ) incomplete_let : bool ,
150
+ pub ( crate ) in_value : bool ,
150
151
pub ( crate ) ref_expr_parent : Option < ast:: RefExpr > ,
151
152
pub ( crate ) after_amp : bool ,
152
153
/// The surrounding RecordExpression we are completing a functional update
Original file line number Diff line number Diff line change @@ -1248,6 +1248,7 @@ fn classify_name_ref<'db>(
1248
1248
. parent ( )
1249
1249
. and_then ( ast:: LetStmt :: cast)
1250
1250
. is_some_and ( |it| it. semicolon_token ( ) . is_none ( ) ) ;
1251
+ let in_value = it. parent ( ) . and_then ( Either :: < ast:: LetStmt , ast:: ArgList > :: cast) . is_some ( ) ;
1251
1252
let impl_ = fetch_immediate_impl ( sema, original_file, expr. syntax ( ) ) ;
1252
1253
1253
1254
let in_match_guard = match it. parent ( ) . and_then ( ast:: MatchArm :: cast) {
@@ -1268,6 +1269,7 @@ fn classify_name_ref<'db>(
1268
1269
is_func_update,
1269
1270
innermost_ret_ty,
1270
1271
self_param,
1272
+ in_value,
1271
1273
incomplete_let,
1272
1274
impl_,
1273
1275
in_match_guard,
You can’t perform that action at this time.
0 commit comments