about summary refs log tree commit diff
diff options
context:
space:
mode:
authorA4-Tacks <wdsjxhno1001@163.com>2025-08-04 22:44:55 +0800
committerA4-Tacks <wdsjxhno1001@163.com>2025-08-04 22:47:43 +0800
commit0e44b50dbef4c8ac2520576543fcbfff7d6d99e6 (patch)
treeab7f541bed23c43a2586c8139bb696144d7b0407
parentd976ef8ae494cc8fd6672d43c49377e43c2876b5 (diff)
downloadrust-0e44b50dbef4c8ac2520576543fcbfff7d6d99e6.tar.gz
rust-0e44b50dbef4c8ac2520576543fcbfff7d6d99e6.zip
Add if..else completions in LetStmt and ArgList
Example
===
```rust
let x = $0;
```

Old completions:

```rust
let x = if $1 {
    $0
};
```

This PR current completions:

```rust
let x = if $1 {
    $2
} else {
    $0
};
```
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs9
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs116
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/context.rs1
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs2
4 files changed, 127 insertions, 1 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs
index 2133291b1de..a84927f6e2c 100644
--- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs
+++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs
@@ -61,6 +61,7 @@ pub(crate) fn complete_expr_path(
         after_if_expr,
         in_condition,
         incomplete_let,
+        in_value,
         ref ref_expr_parent,
         after_amp,
         ref is_func_update,
@@ -361,10 +362,16 @@ pub(crate) fn complete_expr_path(
                     add_keyword("loop", "loop {\n    $0\n}");
                     if in_match_guard {
                         add_keyword("if", "if $0");
+                    } else if in_value {
+                        add_keyword("if", "if $1 {\n    $2\n} else {\n    $0\n}");
                     } else {
                         add_keyword("if", "if $1 {\n    $0\n}");
                     }
-                    add_keyword("if let", "if let $1 = $2 {\n    $0\n}");
+                    if in_value {
+                        add_keyword("if let", "if let $1 = $2 {\n    $3\n} else {\n    $0\n}");
+                    } else {
+                        add_keyword("if let", "if let $1 = $2 {\n    $0\n}");
+                    }
                     add_keyword("for", "for $1 in $2 {\n    $0\n}");
                     add_keyword("true", "true");
                     add_keyword("false", "false");
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs
index 64bb1fce6ba..aea4e119f20 100644
--- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs
+++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs
@@ -238,6 +238,8 @@ fn main() {
             r#"
 fn main() {
     let x = if $1 {
+    $2
+} else {
     $0
 };
     let y = 92;
@@ -336,6 +338,120 @@ fn main() {
     }
 
     #[test]
+    fn if_completion_in_parameter() {
+        check_edit(
+            "if",
+            r"
+fn main() {
+    foo($0)
+}
+",
+            r"
+fn main() {
+    foo(if $1 {
+    $2
+} else {
+    $0
+})
+}
+",
+        );
+
+        check_edit(
+            "if",
+            r"
+fn main() {
+    foo($0, 2)
+}
+",
+            r"
+fn main() {
+    foo(if $1 {
+    $2
+} else {
+    $0
+}, 2)
+}
+",
+        );
+
+        check_edit(
+            "if",
+            r"
+fn main() {
+    foo(2, $0)
+}
+",
+            r"
+fn main() {
+    foo(2, if $1 {
+    $2
+} else {
+    $0
+})
+}
+",
+        );
+
+        check_edit(
+            "if let",
+            r"
+fn main() {
+    foo(2, $0)
+}
+",
+            r"
+fn main() {
+    foo(2, if let $1 = $2 {
+    $3
+} else {
+    $0
+})
+}
+",
+        );
+    }
+
+    #[test]
+    fn if_completion_in_let_statement() {
+        check_edit(
+            "if",
+            r"
+fn main() {
+    let x = $0;
+}
+",
+            r"
+fn main() {
+    let x = if $1 {
+    $2
+} else {
+    $0
+};
+}
+",
+        );
+
+        check_edit(
+            "if let",
+            r"
+fn main() {
+    let x = $0;
+}
+",
+            r"
+fn main() {
+    let x = if let $1 = $2 {
+    $3
+} else {
+    $0
+};
+}
+",
+        );
+    }
+
+    #[test]
     fn completes_let_in_block() {
         check_edit(
             "let",
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/context.rs b/src/tools/rust-analyzer/crates/ide-completion/src/context.rs
index cfd7f80d40b..bb6b13b8fb9 100644
--- a/src/tools/rust-analyzer/crates/ide-completion/src/context.rs
+++ b/src/tools/rust-analyzer/crates/ide-completion/src/context.rs
@@ -147,6 +147,7 @@ pub(crate) struct PathExprCtx<'db> {
     /// Whether this expression is the direct condition of an if or while expression
     pub(crate) in_condition: bool,
     pub(crate) incomplete_let: bool,
+    pub(crate) in_value: bool,
     pub(crate) ref_expr_parent: Option<ast::RefExpr>,
     pub(crate) after_amp: bool,
     /// The surrounding RecordExpression we are completing a functional update
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 ea5fb39338b..59077237e75 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
@@ -1245,6 +1245,7 @@ fn classify_name_ref<'db>(
             .parent()
             .and_then(ast::LetStmt::cast)
             .is_some_and(|it| it.semicolon_token().is_none());
+        let in_value = it.parent().and_then(Either::<ast::LetStmt, ast::ArgList>::cast).is_some();
         let impl_ = fetch_immediate_impl(sema, original_file, expr.syntax());
 
         let in_match_guard = match it.parent().and_then(ast::MatchArm::cast) {
@@ -1265,6 +1266,7 @@ fn classify_name_ref<'db>(
                 is_func_update,
                 innermost_ret_ty,
                 self_param,
+                in_value,
                 incomplete_let,
                 impl_,
                 in_match_guard,