Skip to content

Commit 6a7a0fa

Browse files
committed
Add let in let-chain completion support
Example --- ```rust fn f() { if true && $0 {} } ``` -> ```rust fn f() { if true && let $1 = $0 {} } ```
1 parent e10fa93 commit 6a7a0fa

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,19 +1171,23 @@ fn classify_name_ref<'db>(
11711171
Some(res)
11721172
};
11731173

1174-
let is_in_condition = |it: &ast::Expr| {
1174+
fn is_in_condition(it: &ast::Expr) -> bool {
11751175
(|| {
11761176
let parent = it.syntax().parent()?;
11771177
if let Some(expr) = ast::WhileExpr::cast(parent.clone()) {
11781178
Some(expr.condition()? == *it)
1179-
} else if let Some(expr) = ast::IfExpr::cast(parent) {
1179+
} else if let Some(expr) = ast::IfExpr::cast(parent.clone()) {
11801180
Some(expr.condition()? == *it)
1181+
} else if let Some(expr) = ast::BinExpr::cast(parent)
1182+
&& expr.op_token()?.kind() == T![&&]
1183+
{
1184+
Some(is_in_condition(&expr.into()))
11811185
} else {
11821186
None
11831187
}
11841188
})()
11851189
.unwrap_or(false)
1186-
};
1190+
}
11871191

11881192
let make_path_kind_expr = |expr: ast::Expr| {
11891193
let it = expr.syntax();

crates/ide-completion/src/tests/expression.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,3 +2275,13 @@ fn foo() {
22752275
"#]],
22762276
);
22772277
}
2278+
2279+
#[test]
2280+
fn let_in_condition() {
2281+
check_edit("let", r#"fn f() { if $0 {} }"#, r#"fn f() { if let $1 = $0 {} }"#);
2282+
}
2283+
2284+
#[test]
2285+
fn let_in_let_chain() {
2286+
check_edit("let", r#"fn f() { if true && $0 {} }"#, r#"fn f() { if true && let $1 = $0 {} }"#);
2287+
}

0 commit comments

Comments
 (0)