mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-29 06:23:25 +00:00
assist: autoderef in generate delegate methods
This commit is contained in:
parent
f070093462
commit
94e6a6642c
1 changed files with 48 additions and 7 deletions
|
@ -65,14 +65,17 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
|
|||
let sema_field_ty = ctx.sema.resolve_type(&field_ty)?;
|
||||
let krate = sema_field_ty.krate(ctx.db());
|
||||
let mut methods = vec![];
|
||||
sema_field_ty.iterate_assoc_items(ctx.db(), krate, |item| {
|
||||
if let hir::AssocItem::Function(f) = item {
|
||||
if f.self_param(ctx.db()).is_some() && f.is_visible_from(ctx.db(), current_module) {
|
||||
methods.push(f)
|
||||
|
||||
for ty in sema_field_ty.autoderef(ctx.db()) {
|
||||
ty.iterate_assoc_items(ctx.db(), krate, |item| {
|
||||
if let hir::AssocItem::Function(f) = item {
|
||||
if f.self_param(ctx.db()).is_some() && f.is_visible_from(ctx.db(), current_module) {
|
||||
methods.push(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
Option::<()>::None
|
||||
});
|
||||
Option::<()>::None
|
||||
});
|
||||
}
|
||||
|
||||
for method in methods {
|
||||
let adt = ast::Adt::Struct(strukt.clone());
|
||||
|
@ -314,6 +317,44 @@ impl<T> Person<T> {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generates_delegate_autoderef() {
|
||||
check_assist(
|
||||
generate_delegate_methods,
|
||||
r#"
|
||||
//- minicore: deref
|
||||
struct Age(u8);
|
||||
impl Age {
|
||||
fn age(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
struct AgeDeref(Age);
|
||||
impl core::ops::Deref for AgeDeref { type Target = Age; }
|
||||
struct Person {
|
||||
ag$0e: AgeDeref,
|
||||
}
|
||||
impl Person {}"#,
|
||||
r#"
|
||||
struct Age(u8);
|
||||
impl Age {
|
||||
fn age(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
struct AgeDeref(Age);
|
||||
impl core::ops::Deref for AgeDeref { type Target = Age; }
|
||||
struct Person {
|
||||
age: AgeDeref,
|
||||
}
|
||||
impl Person {
|
||||
$0fn age(&self) -> u8 {
|
||||
self.age.age()
|
||||
}
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_delegate_visibility() {
|
||||
check_assist_not_applicable(
|
||||
|
|
Loading…
Reference in a new issue