Skip to content

Commit ea9b42d

Browse files
committed
fix: offer on path_type for add_lifetime_to_type
Example --- ```rust struct Foo { a: &'_ i32, b: Foo<'_$0>, } ``` **Before this PR** Assist not applicable **After this PR** ```rust struct Foo<'a> { a: &'a i32, b: Foo<'a>, } ```
1 parent e708e55 commit ea9b42d

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ use crate::{AssistContext, AssistId, Assists};
2323
// }
2424
// ```
2525
pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Option<()> {
26-
let ref_type_focused = ctx.find_node_at_offset::<ast::RefType>()?;
27-
if ref_type_focused.lifetime().is_some_and(|lifetime| lifetime.text() != "'_") {
26+
if !trigger_assist(ctx) {
2827
return None;
2928
}
30-
3129
let node = ctx.find_node_at_offset::<ast::Adt>()?;
3230
let has_lifetime = node
3331
.generic_param_list()
@@ -67,6 +65,16 @@ pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext<'_, '_
6765
})
6866
}
6967

68+
fn trigger_assist(ctx: &AssistContext<'_, '_>) -> bool {
69+
ctx.find_node_at_offset::<ast::RefType>()
70+
.is_some_and(|it| it.lifetime().is_none_or(|it| it.text() == "'_"))
71+
|| ctx
72+
.find_node_at_offset::<ast::PathType>()
73+
.map(ast::Type::from)
74+
.and_then(|it| it.generic_arg_list()?.lifetime_args().next()?.lifetime())
75+
.is_some_and(|it| it.text() == "'_")
76+
}
77+
7078
fn fetch_borrowed_types(node: &ast::Adt) -> Option<Vec<Change>> {
7179
let ref_types: Vec<_> = match node {
7280
ast::Adt::Enum(enum_) => {
@@ -183,6 +191,18 @@ mod tests {
183191
r#"struct Foo { a: &'_ $0i32, b: &'_ (&'_ i32, fn(&str) -> &str) }"#,
184192
r#"struct Foo<'a> { a: &'a i32, b: &'a (&'a i32, fn(&str) -> &str) }"#,
185193
);
194+
195+
check_assist(
196+
add_lifetime_to_type,
197+
r#"struct Foo { a: &'_ $0i32, b: Foo<'_> }"#,
198+
r#"struct Foo<'a> { a: &'a i32, b: Foo<'a> }"#,
199+
);
200+
201+
check_assist(
202+
add_lifetime_to_type,
203+
r#"struct Foo { a: &'_ i32, b: Foo<'_$0> }"#,
204+
r#"struct Foo<'a> { a: &'a i32, b: Foo<'a> }"#,
205+
);
186206
}
187207

188208
#[test]

0 commit comments

Comments
 (0)