Skip to content

Commit 94e6a66

Browse files
committed
assist: autoderef in generate delegate methods
1 parent f070093 commit 94e6a66

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

crates/ide-assists/src/handlers/generate_delegate_methods.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
6565
let sema_field_ty = ctx.sema.resolve_type(&field_ty)?;
6666
let krate = sema_field_ty.krate(ctx.db());
6767
let mut methods = vec![];
68-
sema_field_ty.iterate_assoc_items(ctx.db(), krate, |item| {
69-
if let hir::AssocItem::Function(f) = item {
70-
if f.self_param(ctx.db()).is_some() && f.is_visible_from(ctx.db(), current_module) {
71-
methods.push(f)
68+
69+
for ty in sema_field_ty.autoderef(ctx.db()) {
70+
ty.iterate_assoc_items(ctx.db(), krate, |item| {
71+
if let hir::AssocItem::Function(f) = item {
72+
if f.self_param(ctx.db()).is_some() && f.is_visible_from(ctx.db(), current_module) {
73+
methods.push(f)
74+
}
7275
}
73-
}
74-
Option::<()>::None
75-
});
76+
Option::<()>::None
77+
});
78+
}
7679

7780
for method in methods {
7881
let adt = ast::Adt::Struct(strukt.clone());
@@ -314,6 +317,44 @@ impl<T> Person<T> {
314317
);
315318
}
316319

320+
#[test]
321+
fn test_generates_delegate_autoderef() {
322+
check_assist(
323+
generate_delegate_methods,
324+
r#"
325+
//- minicore: deref
326+
struct Age(u8);
327+
impl Age {
328+
fn age(&self) -> u8 {
329+
self.0
330+
}
331+
}
332+
struct AgeDeref(Age);
333+
impl core::ops::Deref for AgeDeref { type Target = Age; }
334+
struct Person {
335+
ag$0e: AgeDeref,
336+
}
337+
impl Person {}"#,
338+
r#"
339+
struct Age(u8);
340+
impl Age {
341+
fn age(&self) -> u8 {
342+
self.0
343+
}
344+
}
345+
struct AgeDeref(Age);
346+
impl core::ops::Deref for AgeDeref { type Target = Age; }
347+
struct Person {
348+
age: AgeDeref,
349+
}
350+
impl Person {
351+
$0fn age(&self) -> u8 {
352+
self.age.age()
353+
}
354+
}"#,
355+
);
356+
}
357+
317358
#[test]
318359
fn test_generate_delegate_visibility() {
319360
check_assist_not_applicable(

0 commit comments

Comments
 (0)