mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Auto merge of #13061 - ice1k:master, r=Veykril
feat: Improved inline_call to replace `Self` Fixes #13060
This commit is contained in:
commit
62c3107065
1 changed files with 34 additions and 1 deletions
|
@ -13,7 +13,7 @@ use ide_db::{
|
||||||
use itertools::{izip, Itertools};
|
use itertools::{izip, Itertools};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
|
ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
|
||||||
ted, AstNode,
|
ted, AstNode, NodeOrToken, SyntaxKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -311,6 +311,13 @@ fn inline(
|
||||||
} else {
|
} else {
|
||||||
fn_body.clone_for_update()
|
fn_body.clone_for_update()
|
||||||
};
|
};
|
||||||
|
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| {
|
let usages_for_locals = |local| {
|
||||||
Definition::Local(local)
|
Definition::Local(local)
|
||||||
.usages(sema)
|
.usages(sema)
|
||||||
|
@ -345,6 +352,7 @@ fn inline(
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if function.self_param(sema.db).is_some() {
|
if function.self_param(sema.db).is_some() {
|
||||||
let this = || make::name_ref("this").syntax().clone_for_update();
|
let this = || make::name_ref("this").syntax().clone_for_update();
|
||||||
if let Some(self_local) = params[0].2.as_local(sema.db) {
|
if let Some(self_local) = params[0].2.as_local(sema.db) {
|
||||||
|
@ -1188,6 +1196,31 @@ fn bar() -> u32 {
|
||||||
x
|
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);
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue