diff options
| author | A4-Tacks <wdsjxhno1001@163.com> | 2025-08-22 21:58:49 +0800 |
|---|---|---|
| committer | A4-Tacks <wdsjxhno1001@163.com> | 2025-08-22 21:58:49 +0800 |
| commit | c9fbcdcfcdc5290639c2a76929bec1ed07c08f8e (patch) | |
| tree | 5ae3efbdc9ebca7fb1b84558031f0697fe3562ee /src | |
| parent | 8e7cca83344e511e263e91f9539b8d341b2dd2bb (diff) | |
| download | rust-c9fbcdcfcdc5290639c2a76929bec1ed07c08f8e.tar.gz rust-c9fbcdcfcdc5290639c2a76929bec1ed07c08f8e.zip | |
Add let in let-chain completion support
Example
---
```rust
fn f() {
if true && $0 {}
}
```
->
```rust
fn f() {
if true && let $1 = $0 {}
}
```
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs | 10 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs | 10 |
2 files changed, 17 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs b/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs index 6eb1727b331..7d6fa553001 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs @@ -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(); diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs index 33f729f0166..07eaeea97e5 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs @@ -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 {} }"#); +} |
