Add let in let-chain completion support

Example
---
```rust
fn f() {
    if true && $0 {}
}
```
->
```rust
fn f() {
    if true && let $1 = $0 {}
}
```
This commit is contained in:
A4-Tacks 2025-08-22 21:58:49 +08:00
parent 8e7cca8334
commit c9fbcdcfcd
No known key found for this signature in database
GPG key ID: 86AC1F526BA06668
2 changed files with 17 additions and 3 deletions

View file

@ -1171,19 +1171,23 @@ fn classify_name_ref<'db>(
Some(res)
};
let is_in_condition = |it: &ast::Expr| {
fn is_in_condition(it: &ast::Expr) -> bool {
(|| {
let parent = it.syntax().parent()?;
if let Some(expr) = ast::WhileExpr::cast(parent.clone()) {
Some(expr.condition()? == *it)
} else if let Some(expr) = ast::IfExpr::cast(parent) {
} else if let Some(expr) = ast::IfExpr::cast(parent.clone()) {
Some(expr.condition()? == *it)
} else if let Some(expr) = ast::BinExpr::cast(parent)
&& expr.op_token()?.kind() == T![&&]
{
Some(is_in_condition(&expr.into()))
} else {
None
}
})()
.unwrap_or(false)
};
}
let make_path_kind_expr = |expr: ast::Expr| {
let it = expr.syntax();

View file

@ -2275,3 +2275,13 @@ fn foo() {
"#]],
);
}
#[test]
fn let_in_condition() {
check_edit("let", r#"fn f() { if $0 {} }"#, r#"fn f() { if let $1 = $0 {} }"#);
}
#[test]
fn let_in_let_chain() {
check_edit("let", r#"fn f() { if true && $0 {} }"#, r#"fn f() { if true && let $1 = $0 {} }"#);
}