]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide-assists/src/handlers/inline_call.rs
feat: Improved inline_call to replace `Self`
[rust.git] / crates / ide-assists / src / handlers / inline_call.rs
index 8b2f6ac75a28eacaf812c3045e809b6496a212d7..c78c5eaa9f42d0d1a685ce5d5b32d6c992d35411 100644 (file)
@@ -13,7 +13,7 @@
 use itertools::{izip, Itertools};
 use syntax::{
     ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
-    ted, AstNode,
+    ted, AstNode, SyntaxKind,
 };
 
 use crate::{
@@ -306,13 +306,21 @@ fn inline(
         if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
             body
         } else {
-            // FIXME(zachs18): I believe this should be unreachable,
-            // since insert_ws_into shouldn't change the kind of the SyntaxNode.
             fn_body.clone_for_update()
         }
     } else {
         fn_body.clone_for_update()
     };
+    // TODO: use if-let chains - https://github.com/rust-lang/rust/pull/94927
+    if let Some(i) = body.syntax().ancestors().find_map(ast::Impl::cast) {
+        if let Some(st) = i.self_ty() {
+            for tok in body.syntax().descendants_with_tokens().filter_map(|t| t.into_token()) {
+                if tok.kind() == SyntaxKind::SELF_TYPE_KW {
+                    ted::replace(tok, st.syntax());
+                }
+            }
+        }
+    }
     let usages_for_locals = |local| {
         Definition::Local(local)
             .usages(sema)
@@ -347,6 +355,7 @@ fn inline(
             }
         })
         .collect();
+
     if function.self_param(sema.db).is_some() {
         let this = || make::name_ref("this").syntax().clone_for_update();
         if let Some(self_local) = params[0].2.as_local(sema.db) {
@@ -1190,6 +1199,31 @@ fn bar() -> u32 {
       x
     }
 }
+"#,
+        )
+    }
+
+    #[test]
+    fn inline_call_with_self_type() {
+        check_assist(
+            inline_call,
+            r#"
+struct A(u32);
+impl A {
+    fn f() -> Self { Self(114514) }
+}
+fn main() {
+    A::f$0();
+}
+"#,
+            r#"
+struct A(u32);
+impl A {
+    fn f() -> Self { Self(114514) }
+}
+fn main() {
+    A(114514);
+}
 "#,
         )
     }