about summary refs log tree commit diff
diff options
context:
space:
mode:
authorA4-Tacks <wdsjxhno1001@163.com>2025-06-28 15:43:04 +0800
committerA4-Tacks <wdsjxhno1001@163.com>2025-07-18 06:18:29 +0800
commite0dddfc75321dd196b5f67da4eb7f22d1f276992 (patch)
tree1a72cab9707dace3d5ad32904f39b902dc72bf3b
parentf345b76895b5c2a232d8412e3844e10871432f38 (diff)
downloadrust-e0dddfc75321dd196b5f67da4eb7f22d1f276992.tar.gz
rust-e0dddfc75321dd196b5f67da4eb7f22d1f276992.zip
Change tabstop to method tail_expr
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_impl.rs37
-rw-r--r--src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs4
2 files changed, 25 insertions, 16 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_impl.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_impl.rs
index ba95fe5d92a..e72062bf995 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_impl.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_impl.rs
@@ -141,8 +141,8 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
 // }
 //
 // impl Foo for ${1:_} {
-//     $0fn foo(&self) -> i32 {
-//         todo!()
+//     fn foo(&self) -> i32 {
+//         $0todo!()
 //     }
 // }
 // ```
@@ -206,8 +206,10 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
                     edit.add_placeholder_snippet(cap, ty);
                 }
 
-                if let Some(item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().next()) {
-                    edit.add_tabstop_before(cap, item);
+                if let Some(expr) =
+                    impl_.assoc_item_list().and_then(|it| it.assoc_items().find_map(extract_expr))
+                {
+                    edit.add_tabstop_before(cap, expr);
                 } else if let Some(l_curly) =
                     impl_.assoc_item_list().and_then(|it| it.l_curly_token())
                 {
@@ -220,6 +222,13 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) ->
     )
 }
 
+fn extract_expr(item: ast::AssocItem) -> Option<ast::Expr> {
+    let ast::AssocItem::Fn(f) = item else {
+        return None;
+    };
+    f.body()?.tail_expr()
+}
+
 #[cfg(test)]
 mod tests {
     use crate::tests::{check_assist, check_assist_target};
@@ -616,8 +625,8 @@ mod tests {
                 }
 
                 impl Foo for ${1:_} {
-                    $0fn foo(&self) -> i32 {
-                        todo!()
+                    fn foo(&self) -> i32 {
+                        $0todo!()
                     }
                 }
             "#,
@@ -647,8 +656,8 @@ mod tests {
                 }
 
                 impl Foo<${1:_}> for ${2:_} {
-                    $0fn foo(&self) -> _ {
-                        todo!()
+                    fn foo(&self) -> _ {
+                        $0todo!()
                     }
                 }
             "#,
@@ -674,8 +683,8 @@ mod tests {
                 }
 
                 impl Foo<${1:_}, ${2:_}> for ${3:_} {
-                    $0fn foo(&self) -> _ {
-                        todo!()
+                    fn foo(&self) -> _ {
+                        $0todo!()
                     }
                 }
             "#,
@@ -709,8 +718,8 @@ mod tests {
                 }
 
                 impl Foo for ${1:_} {
-                    $0fn foo(&self) -> i32 {
-                        todo!()
+                    fn foo(&self) -> i32 {
+                        $0todo!()
                     }
                 }
             "#,
@@ -736,10 +745,10 @@ mod tests {
                 }
 
                 impl Foo for ${1:_} {
-                    $0type Output;
+                    type Output;
 
                     fn foo(&self) -> Self::Output {
-                        todo!()
+                        $0todo!()
                     }
                 }
             "#,
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs b/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs
index 7c88677be1a..b63571ca6a1 100644
--- a/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs
+++ b/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs
@@ -1895,8 +1895,8 @@ trait Foo {
 }
 
 impl Foo for ${1:_} {
-    $0fn foo(&self) -> i32 {
-        todo!()
+    fn foo(&self) -> i32 {
+        $0todo!()
     }
 }
 "#####,