1 use rustc_hash::FxHashSet;
3 ast::{self, edit_in_place::GenericParamsOwnerEdit, make, GenericParamsOwner},
8 use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists};
10 static ASSIST_NAME: &str = "introduce_named_lifetime";
11 static ASSIST_LABEL: &str = "Introduce named lifetime";
13 // Assist: introduce_named_lifetime
15 // Change an anonymous lifetime to a named lifetime.
18 // impl Cursor<'_$0> {
19 // fn node(self) -> &SyntaxNode {
21 // Cursor::Replace(node) | Cursor::Before(node) => node,
28 // impl<'a> Cursor<'a> {
29 // fn node(self) -> &SyntaxNode {
31 // Cursor::Replace(node) | Cursor::Before(node) => node,
36 // FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
37 // FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo
38 pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
40 ctx.find_node_at_offset::<ast::Lifetime>().filter(|lifetime| lifetime.text() == "'_")?;
41 let lifetime_loc = lifetime.lifetime_ident_token()?.text_range();
43 if let Some(fn_def) = lifetime.syntax().ancestors().find_map(ast::Fn::cast) {
44 generate_fn_def_assist(acc, fn_def, lifetime_loc, lifetime)
45 } else if let Some(impl_def) = lifetime.syntax().ancestors().find_map(ast::Impl::cast) {
46 generate_impl_def_assist(acc, impl_def, lifetime_loc, lifetime)
52 /// Generate the assist for the fn def case
53 fn generate_fn_def_assist(
56 lifetime_loc: TextRange,
57 lifetime: ast::Lifetime,
59 let param_list: ast::ParamList = fn_def.param_list()?;
60 let new_lifetime_param = generate_unique_lifetime_param_name(fn_def.generic_param_list())?;
62 // use the self if it's a reference and has no explicit lifetime
63 param_list.self_param().filter(|p| p.lifetime().is_none() && p.amp_token().is_some());
64 // compute the location which implicitly has the same lifetime as the anonymous lifetime
65 let loc_needing_lifetime = if let Some(self_param) = self_param {
66 // if we have a self reference, use that
67 Some(NeedsLifetime::SelfParam(self_param))
69 // otherwise, if there's a single reference parameter without a named liftime, use that
70 let fn_params_without_lifetime: Vec<_> = param_list
72 .filter_map(|param| match param.ty() {
73 Some(ast::Type::RefType(ascribed_type)) if ascribed_type.lifetime().is_none() => {
74 Some(NeedsLifetime::RefType(ascribed_type))
79 match fn_params_without_lifetime.len() {
80 1 => Some(fn_params_without_lifetime.into_iter().nth(0)?),
82 // multiple unnnamed is invalid. assist is not applicable
86 acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
87 let fn_def = builder.make_mut(fn_def);
88 let lifetime = builder.make_mut(lifetime);
89 let loc_needing_lifetime =
90 loc_needing_lifetime.and_then(|it| it.make_mut(builder).to_position());
92 fn_def.get_or_create_generic_param_list().add_generic_param(
93 make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
95 ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
97 .map(|position| ted::insert(position, new_lifetime_param.clone_for_update().syntax()));
101 /// Generate the assist for the impl def case
102 fn generate_impl_def_assist(
105 lifetime_loc: TextRange,
106 lifetime: ast::Lifetime,
108 let new_lifetime_param = generate_unique_lifetime_param_name(impl_def.generic_param_list())?;
109 acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
110 let impl_def = builder.make_mut(impl_def);
111 let lifetime = builder.make_mut(lifetime);
113 impl_def.get_or_create_generic_param_list().add_generic_param(
114 make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
116 ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
120 /// Given a type parameter list, generate a unique lifetime parameter name
121 /// which is not in the list
122 fn generate_unique_lifetime_param_name(
123 existing_type_param_list: Option<ast::GenericParamList>,
124 ) -> Option<ast::Lifetime> {
125 match existing_type_param_list {
126 Some(type_params) => {
127 let used_lifetime_params: FxHashSet<_> =
128 type_params.lifetime_params().map(|p| p.syntax().text().to_string()).collect();
129 ('a'..='z').map(|it| format!("'{}", it)).find(|it| !used_lifetime_params.contains(it))
131 None => Some("'a".to_string()),
133 .map(|it| make::lifetime(&it))
137 SelfParam(ast::SelfParam),
138 RefType(ast::RefType),
142 fn make_mut(self, builder: &mut AssistBuilder) -> Self {
144 Self::SelfParam(it) => Self::SelfParam(builder.make_mut(it)),
145 Self::RefType(it) => Self::RefType(builder.make_mut(it)),
149 fn to_position(self) -> Option<Position> {
151 Self::SelfParam(it) => Some(Position::after(it.amp_token()?)),
152 Self::RefType(it) => Some(Position::after(it.amp_token()?)),
160 use crate::tests::{check_assist, check_assist_not_applicable};
163 fn test_example_case() {
165 introduce_named_lifetime,
166 r#"impl Cursor<'_$0> {
167 fn node(self) -> &SyntaxNode {
169 Cursor::Replace(node) | Cursor::Before(node) => node,
173 r#"impl<'a> Cursor<'a> {
174 fn node(self) -> &SyntaxNode {
176 Cursor::Replace(node) | Cursor::Before(node) => node,
184 fn test_example_case_simplified() {
186 introduce_named_lifetime,
187 r#"impl Cursor<'_$0> {"#,
188 r#"impl<'a> Cursor<'a> {"#,
193 fn test_example_case_cursor_after_tick() {
195 introduce_named_lifetime,
196 r#"impl Cursor<'$0_> {"#,
197 r#"impl<'a> Cursor<'a> {"#,
202 fn test_impl_with_other_type_param() {
204 introduce_named_lifetime,
205 "impl<I> fmt::Display for SepByBuilder<'_$0, I>
208 I::Item: fmt::Display,
210 "impl<I, 'a> fmt::Display for SepByBuilder<'a, I>
213 I::Item: fmt::Display,
219 fn test_example_case_cursor_before_tick() {
221 introduce_named_lifetime,
222 r#"impl Cursor<$0'_> {"#,
223 r#"impl<'a> Cursor<'a> {"#,
228 fn test_not_applicable_cursor_position() {
229 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'_>$0 {"#);
230 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor$0<'_> {"#);
234 fn test_not_applicable_lifetime_already_name() {
235 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'a$0> {"#);
236 check_assist_not_applicable(introduce_named_lifetime, r#"fn my_fun<'a>() -> X<'a$0>"#);
240 fn test_with_type_parameter() {
242 introduce_named_lifetime,
243 r#"impl<T> Cursor<T, '_$0>"#,
244 r#"impl<T, 'a> Cursor<T, 'a>"#,
249 fn test_with_existing_lifetime_name_conflict() {
251 introduce_named_lifetime,
252 r#"impl<'a, 'b> Cursor<'a, 'b, '_$0>"#,
253 r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
258 fn test_function_return_value_anon_lifetime_param() {
260 introduce_named_lifetime,
261 r#"fn my_fun() -> X<'_$0>"#,
262 r#"fn my_fun<'a>() -> X<'a>"#,
267 fn test_function_return_value_anon_reference_lifetime() {
269 introduce_named_lifetime,
270 r#"fn my_fun() -> &'_$0 X"#,
271 r#"fn my_fun<'a>() -> &'a X"#,
276 fn test_function_param_anon_lifetime() {
278 introduce_named_lifetime,
279 r#"fn my_fun(x: X<'_$0>)"#,
280 r#"fn my_fun<'a>(x: X<'a>)"#,
285 fn test_function_add_lifetime_to_params() {
287 introduce_named_lifetime,
288 r#"fn my_fun(f: &Foo) -> X<'_$0>"#,
289 r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
294 fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
296 introduce_named_lifetime,
297 r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_$0>"#,
298 r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#,
303 fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() {
304 // this is not permitted under lifetime elision rules
305 check_assist_not_applicable(
306 introduce_named_lifetime,
307 r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_$0>"#,
312 fn test_function_add_lifetime_to_self_ref_param() {
314 introduce_named_lifetime,
315 r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
316 r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#,
321 fn test_function_add_lifetime_to_param_with_non_ref_self() {
323 introduce_named_lifetime,
324 r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
325 r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#,
330 fn test_function_add_lifetime_to_self_ref_mut() {
332 introduce_named_lifetime,
333 r#"fn foo(&mut self) -> &'_$0 ()"#,
334 r#"fn foo<'a>(&'a mut self) -> &'a ()"#,