]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/handlers/introduce_named_lifetime.rs
Merge #8871
[rust.git] / crates / ide_assists / src / handlers / introduce_named_lifetime.rs
1 use rustc_hash::FxHashSet;
2 use syntax::{
3     ast::{self, edit_in_place::GenericParamsOwnerEdit, make, GenericParamsOwner},
4     ted::{self, Position},
5     AstNode, TextRange,
6 };
7
8 use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists};
9
10 static ASSIST_NAME: &str = "introduce_named_lifetime";
11 static ASSIST_LABEL: &str = "Introduce named lifetime";
12
13 // Assist: introduce_named_lifetime
14 //
15 // Change an anonymous lifetime to a named lifetime.
16 //
17 // ```
18 // impl Cursor<'_$0> {
19 //     fn node(self) -> &SyntaxNode {
20 //         match self {
21 //             Cursor::Replace(node) | Cursor::Before(node) => node,
22 //         }
23 //     }
24 // }
25 // ```
26 // ->
27 // ```
28 // impl<'a> Cursor<'a> {
29 //     fn node(self) -> &SyntaxNode {
30 //         match self {
31 //             Cursor::Replace(node) | Cursor::Before(node) => node,
32 //         }
33 //     }
34 // }
35 // ```
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<()> {
39     let lifetime =
40         ctx.find_node_at_offset::<ast::Lifetime>().filter(|lifetime| lifetime.text() == "'_")?;
41     let lifetime_loc = lifetime.lifetime_ident_token()?.text_range();
42
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)
47     } else {
48         None
49     }
50 }
51
52 /// Generate the assist for the fn def case
53 fn generate_fn_def_assist(
54     acc: &mut Assists,
55     fn_def: ast::Fn,
56     lifetime_loc: TextRange,
57     lifetime: ast::Lifetime,
58 ) -> Option<()> {
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())?;
61     let self_param =
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))
68     } else {
69         // otherwise, if there's a single reference parameter without a named liftime, use that
70         let fn_params_without_lifetime: Vec<_> = param_list
71             .params()
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))
75                 }
76                 _ => None,
77             })
78             .collect();
79         match fn_params_without_lifetime.len() {
80             1 => Some(fn_params_without_lifetime.into_iter().nth(0)?),
81             0 => None,
82             // multiple unnnamed is invalid. assist is not applicable
83             _ => return None,
84         }
85     };
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());
91
92         fn_def.get_or_create_generic_param_list().add_generic_param(
93             make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
94         );
95         ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
96         loc_needing_lifetime
97             .map(|position| ted::insert(position, new_lifetime_param.clone_for_update().syntax()));
98     })
99 }
100
101 /// Generate the assist for the impl def case
102 fn generate_impl_def_assist(
103     acc: &mut Assists,
104     impl_def: ast::Impl,
105     lifetime_loc: TextRange,
106     lifetime: ast::Lifetime,
107 ) -> Option<()> {
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);
112
113         impl_def.get_or_create_generic_param_list().add_generic_param(
114             make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
115         );
116         ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
117     })
118 }
119
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))
130         }
131         None => Some("'a".to_string()),
132     }
133     .map(|it| make::lifetime(&it))
134 }
135
136 enum NeedsLifetime {
137     SelfParam(ast::SelfParam),
138     RefType(ast::RefType),
139 }
140
141 impl NeedsLifetime {
142     fn make_mut(self, builder: &mut AssistBuilder) -> Self {
143         match self {
144             Self::SelfParam(it) => Self::SelfParam(builder.make_mut(it)),
145             Self::RefType(it) => Self::RefType(builder.make_mut(it)),
146         }
147     }
148
149     fn to_position(self) -> Option<Position> {
150         match self {
151             Self::SelfParam(it) => Some(Position::after(it.amp_token()?)),
152             Self::RefType(it) => Some(Position::after(it.amp_token()?)),
153         }
154     }
155 }
156
157 #[cfg(test)]
158 mod tests {
159     use super::*;
160     use crate::tests::{check_assist, check_assist_not_applicable};
161
162     #[test]
163     fn test_example_case() {
164         check_assist(
165             introduce_named_lifetime,
166             r#"impl Cursor<'_$0> {
167                 fn node(self) -> &SyntaxNode {
168                     match self {
169                         Cursor::Replace(node) | Cursor::Before(node) => node,
170                     }
171                 }
172             }"#,
173             r#"impl<'a> Cursor<'a> {
174                 fn node(self) -> &SyntaxNode {
175                     match self {
176                         Cursor::Replace(node) | Cursor::Before(node) => node,
177                     }
178                 }
179             }"#,
180         );
181     }
182
183     #[test]
184     fn test_example_case_simplified() {
185         check_assist(
186             introduce_named_lifetime,
187             r#"impl Cursor<'_$0> {"#,
188             r#"impl<'a> Cursor<'a> {"#,
189         );
190     }
191
192     #[test]
193     fn test_example_case_cursor_after_tick() {
194         check_assist(
195             introduce_named_lifetime,
196             r#"impl Cursor<'$0_> {"#,
197             r#"impl<'a> Cursor<'a> {"#,
198         );
199     }
200
201     #[test]
202     fn test_impl_with_other_type_param() {
203         check_assist(
204             introduce_named_lifetime,
205             "impl<I> fmt::Display for SepByBuilder<'_$0, I>
206         where
207             I: Iterator,
208             I::Item: fmt::Display,
209         {",
210             "impl<I, 'a> fmt::Display for SepByBuilder<'a, I>
211         where
212             I: Iterator,
213             I::Item: fmt::Display,
214         {",
215         )
216     }
217
218     #[test]
219     fn test_example_case_cursor_before_tick() {
220         check_assist(
221             introduce_named_lifetime,
222             r#"impl Cursor<$0'_> {"#,
223             r#"impl<'a> Cursor<'a> {"#,
224         );
225     }
226
227     #[test]
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<'_> {"#);
231     }
232
233     #[test]
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>"#);
237     }
238
239     #[test]
240     fn test_with_type_parameter() {
241         check_assist(
242             introduce_named_lifetime,
243             r#"impl<T> Cursor<T, '_$0>"#,
244             r#"impl<T, 'a> Cursor<T, 'a>"#,
245         );
246     }
247
248     #[test]
249     fn test_with_existing_lifetime_name_conflict() {
250         check_assist(
251             introduce_named_lifetime,
252             r#"impl<'a, 'b> Cursor<'a, 'b, '_$0>"#,
253             r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
254         );
255     }
256
257     #[test]
258     fn test_function_return_value_anon_lifetime_param() {
259         check_assist(
260             introduce_named_lifetime,
261             r#"fn my_fun() -> X<'_$0>"#,
262             r#"fn my_fun<'a>() -> X<'a>"#,
263         );
264     }
265
266     #[test]
267     fn test_function_return_value_anon_reference_lifetime() {
268         check_assist(
269             introduce_named_lifetime,
270             r#"fn my_fun() -> &'_$0 X"#,
271             r#"fn my_fun<'a>() -> &'a X"#,
272         );
273     }
274
275     #[test]
276     fn test_function_param_anon_lifetime() {
277         check_assist(
278             introduce_named_lifetime,
279             r#"fn my_fun(x: X<'_$0>)"#,
280             r#"fn my_fun<'a>(x: X<'a>)"#,
281         );
282     }
283
284     #[test]
285     fn test_function_add_lifetime_to_params() {
286         check_assist(
287             introduce_named_lifetime,
288             r#"fn my_fun(f: &Foo) -> X<'_$0>"#,
289             r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
290         );
291     }
292
293     #[test]
294     fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
295         check_assist(
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>"#,
299         );
300     }
301
302     #[test]
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>"#,
308         );
309     }
310
311     #[test]
312     fn test_function_add_lifetime_to_self_ref_param() {
313         check_assist(
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>"#,
317         );
318     }
319
320     #[test]
321     fn test_function_add_lifetime_to_param_with_non_ref_self() {
322         check_assist(
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>"#,
326         );
327     }
328
329     #[test]
330     fn test_function_add_lifetime_to_self_ref_mut() {
331         check_assist(
332             introduce_named_lifetime,
333             r#"fn foo(&mut self) -> &'_$0 ()"#,
334             r#"fn foo<'a>(&'a mut self) -> &'a ()"#,
335         );
336     }
337 }