Skip to content

Commit b06ce60

Browse files
Merge pull request #20390 from A4-Tacks/if-else-comp-in-args-or-let
Add if..else completions in LetStmt and ArgList
2 parents c4e73f7 + 5704431 commit b06ce60

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

crates/ide-completion/src/completions/expr.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff 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");

crates/ide-completion/src/completions/keyword.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ fn main() {
238238
r#"
239239
fn 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(

crates/ide-completion/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

crates/ide-completion/src/context/analysis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ fn classify_name_ref<'db>(
12481248
.parent()
12491249
.and_then(ast::LetStmt::cast)
12501250
.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();
12511252
let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax());
12521253

12531254
let in_match_guard = match it.parent().and_then(ast::MatchArm::cast) {
@@ -1268,6 +1269,7 @@ fn classify_name_ref<'db>(
12681269
is_func_update,
12691270
innermost_ret_ty,
12701271
self_param,
1272+
in_value,
12711273
incomplete_let,
12721274
impl_,
12731275
in_match_guard,

0 commit comments

Comments
 (0)