about summary refs log tree commit diff
diff options
context:
space:
mode:
authorice1000 <ice1000kotlin@foxmail.com>2022-08-19 01:06:00 +0000
committerice1000 <ice1000kotlin@foxmail.com>2022-08-19 01:06:00 +0000
commitb6fe46055bd9b16fd74df43cbbcb89e612a82b4d (patch)
treec3250cedcb158d70e56eb9bd5424045886196125
parent917bd68b37de4e60e7203061a0a9c23b74d2b5c2 (diff)
downloadrust-b6fe46055bd9b16fd74df43cbbcb89e612a82b4d.tar.gz
rust-b6fe46055bd9b16fd74df43cbbcb89e612a82b4d.zip
feat: Improved inline_call to replace `Self`
-rw-r--r--crates/ide-assists/src/handlers/inline_call.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs
index 80d3b925593..c78c5eaa9f4 100644
--- a/crates/ide-assists/src/handlers/inline_call.rs
+++ b/crates/ide-assists/src/handlers/inline_call.rs
@@ -13,7 +13,7 @@ use ide_db::{
 use itertools::{izip, Itertools};
 use syntax::{
     ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
-    ted, AstNode,
+    ted, AstNode, SyntaxKind,
 };
 
 use crate::{
@@ -311,6 +311,16 @@ fn inline(
     } 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)
@@ -345,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) {
@@ -1191,4 +1202,29 @@ fn bar() -> u32 {
 "#,
         )
     }
+
+    #[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);
+}
+"#,
+        )
+    }
 }