From b6fe46055bd9b16fd74df43cbbcb89e612a82b4d Mon Sep 17 00:00:00 2001 From: ice1000 Date: Fri, 19 Aug 2022 01:06:00 +0000 Subject: [PATCH 1/2] feat: Improved inline_call to replace `Self` --- .../ide-assists/src/handlers/inline_call.rs | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs index 80d3b92559..c78c5eaa9f 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) { @@ -1188,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); +} "#, ) } From 5a6c51ebb8549467abeeac69bc2e12494cde5b29 Mon Sep 17 00:00:00 2001 From: ice1000 Date: Fri, 19 Aug 2022 01:08:59 +0000 Subject: [PATCH 2/2] fix: use functional programming --- crates/ide-assists/src/handlers/inline_call.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs index c78c5eaa9f..b5d092e39b 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, SyntaxKind, + ted, AstNode, NodeOrToken, SyntaxKind, }; use crate::{ @@ -311,15 +311,12 @@ 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()); - } - } - } + if let Some(t) = body.syntax().ancestors().find_map(ast::Impl::cast).and_then(|i| i.self_ty()) { + body.syntax() + .descendants_with_tokens() + .filter_map(NodeOrToken::into_token) + .filter(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW) + .for_each(|tok| ted::replace(tok, t.syntax())); } let usages_for_locals = |local| { Definition::Local(local)