]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/handlers/introduce_named_lifetime.rs
Merge #8317
[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_ast_mut(fn_def);
88         let lifetime = builder.make_ast_mut(lifetime);
89         let loc_needing_lifetime =
90             loc_needing_lifetime.and_then(|it| it.make_mut(builder).to_position());
91
92         add_lifetime_param(fn_def.get_or_create_generic_param_list(), new_lifetime_param);
93         ted::replace(
94             lifetime.syntax(),
95             make_ast_lifetime(new_lifetime_param).clone_for_update().syntax(),
96         );
97         loc_needing_lifetime.map(|position| {
98             ted::insert(position, make_ast_lifetime(new_lifetime_param).clone_for_update().syntax())
99         });
100     })
101 }
102
103 /// Generate the assist for the impl def case
104 fn generate_impl_def_assist(
105     acc: &mut Assists,
106     impl_def: ast::Impl,
107     lifetime_loc: TextRange,
108     lifetime: ast::Lifetime,
109 ) -> Option<()> {
110     let new_lifetime_param = generate_unique_lifetime_param_name(impl_def.generic_param_list())?;
111     acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
112         let impl_def = builder.make_ast_mut(impl_def);
113         let lifetime = builder.make_ast_mut(lifetime);
114
115         add_lifetime_param(impl_def.get_or_create_generic_param_list(), new_lifetime_param);
116         ted::replace(
117             lifetime.syntax(),
118             make_ast_lifetime(new_lifetime_param).clone_for_update().syntax(),
119         );
120     })
121 }
122
123 /// Given a type parameter list, generate a unique lifetime parameter name
124 /// which is not in the list
125 fn generate_unique_lifetime_param_name(
126     existing_type_param_list: Option<ast::GenericParamList>,
127 ) -> Option<char> {
128     match existing_type_param_list {
129         Some(type_params) => {
130             let used_lifetime_params: FxHashSet<_> = type_params
131                 .lifetime_params()
132                 .map(|p| p.syntax().text().to_string()[1..].to_owned())
133                 .collect();
134             (b'a'..=b'z').map(char::from).find(|c| !used_lifetime_params.contains(&c.to_string()))
135         }
136         None => Some('a'),
137     }
138 }
139
140 fn add_lifetime_param(type_params: ast::GenericParamList, new_lifetime_param: char) {
141     let generic_param =
142         make::generic_param(format!("'{}", new_lifetime_param), None).clone_for_update();
143     type_params.add_generic_param(generic_param);
144 }
145
146 fn make_ast_lifetime(new_lifetime_param: char) -> ast::Lifetime {
147     make::generic_param(format!("'{}", new_lifetime_param), None)
148         .syntax()
149         .descendants()
150         .find_map(ast::Lifetime::cast)
151         .unwrap()
152 }
153
154 enum NeedsLifetime {
155     SelfParam(ast::SelfParam),
156     RefType(ast::RefType),
157 }
158
159 impl NeedsLifetime {
160     fn make_mut(self, builder: &mut AssistBuilder) -> Self {
161         match self {
162             Self::SelfParam(it) => Self::SelfParam(builder.make_ast_mut(it)),
163             Self::RefType(it) => Self::RefType(builder.make_ast_mut(it)),
164         }
165     }
166
167     fn to_position(self) -> Option<Position> {
168         match self {
169             Self::SelfParam(it) => Some(Position::after(it.amp_token()?)),
170             Self::RefType(it) => Some(Position::after(it.amp_token()?)),
171         }
172     }
173 }
174
175 #[cfg(test)]
176 mod tests {
177     use super::*;
178     use crate::tests::{check_assist, check_assist_not_applicable};
179
180     #[test]
181     fn test_example_case() {
182         check_assist(
183             introduce_named_lifetime,
184             r#"impl Cursor<'_$0> {
185                 fn node(self) -> &SyntaxNode {
186                     match self {
187                         Cursor::Replace(node) | Cursor::Before(node) => node,
188                     }
189                 }
190             }"#,
191             r#"impl<'a> Cursor<'a> {
192                 fn node(self) -> &SyntaxNode {
193                     match self {
194                         Cursor::Replace(node) | Cursor::Before(node) => node,
195                     }
196                 }
197             }"#,
198         );
199     }
200
201     #[test]
202     fn test_example_case_simplified() {
203         check_assist(
204             introduce_named_lifetime,
205             r#"impl Cursor<'_$0> {"#,
206             r#"impl<'a> Cursor<'a> {"#,
207         );
208     }
209
210     #[test]
211     fn test_example_case_cursor_after_tick() {
212         check_assist(
213             introduce_named_lifetime,
214             r#"impl Cursor<'$0_> {"#,
215             r#"impl<'a> Cursor<'a> {"#,
216         );
217     }
218
219     #[test]
220     fn test_impl_with_other_type_param() {
221         check_assist(
222             introduce_named_lifetime,
223             "impl<I> fmt::Display for SepByBuilder<'_$0, I>
224         where
225             I: Iterator,
226             I::Item: fmt::Display,
227         {",
228             "impl<I, 'a> fmt::Display for SepByBuilder<'a, I>
229         where
230             I: Iterator,
231             I::Item: fmt::Display,
232         {",
233         )
234     }
235
236     #[test]
237     fn test_example_case_cursor_before_tick() {
238         check_assist(
239             introduce_named_lifetime,
240             r#"impl Cursor<$0'_> {"#,
241             r#"impl<'a> Cursor<'a> {"#,
242         );
243     }
244
245     #[test]
246     fn test_not_applicable_cursor_position() {
247         check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'_>$0 {"#);
248         check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor$0<'_> {"#);
249     }
250
251     #[test]
252     fn test_not_applicable_lifetime_already_name() {
253         check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'a$0> {"#);
254         check_assist_not_applicable(introduce_named_lifetime, r#"fn my_fun<'a>() -> X<'a$0>"#);
255     }
256
257     #[test]
258     fn test_with_type_parameter() {
259         check_assist(
260             introduce_named_lifetime,
261             r#"impl<T> Cursor<T, '_$0>"#,
262             r#"impl<T, 'a> Cursor<T, 'a>"#,
263         );
264     }
265
266     #[test]
267     fn test_with_existing_lifetime_name_conflict() {
268         check_assist(
269             introduce_named_lifetime,
270             r#"impl<'a, 'b> Cursor<'a, 'b, '_$0>"#,
271             r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
272         );
273     }
274
275     #[test]
276     fn test_function_return_value_anon_lifetime_param() {
277         check_assist(
278             introduce_named_lifetime,
279             r#"fn my_fun() -> X<'_$0>"#,
280             r#"fn my_fun<'a>() -> X<'a>"#,
281         );
282     }
283
284     #[test]
285     fn test_function_return_value_anon_reference_lifetime() {
286         check_assist(
287             introduce_named_lifetime,
288             r#"fn my_fun() -> &'_$0 X"#,
289             r#"fn my_fun<'a>() -> &'a X"#,
290         );
291     }
292
293     #[test]
294     fn test_function_param_anon_lifetime() {
295         check_assist(
296             introduce_named_lifetime,
297             r#"fn my_fun(x: X<'_$0>)"#,
298             r#"fn my_fun<'a>(x: X<'a>)"#,
299         );
300     }
301
302     #[test]
303     fn test_function_add_lifetime_to_params() {
304         check_assist(
305             introduce_named_lifetime,
306             r#"fn my_fun(f: &Foo) -> X<'_$0>"#,
307             r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
308         );
309     }
310
311     #[test]
312     fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
313         check_assist(
314             introduce_named_lifetime,
315             r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_$0>"#,
316             r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#,
317         );
318     }
319
320     #[test]
321     fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() {
322         // this is not permitted under lifetime elision rules
323         check_assist_not_applicable(
324             introduce_named_lifetime,
325             r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_$0>"#,
326         );
327     }
328
329     #[test]
330     fn test_function_add_lifetime_to_self_ref_param() {
331         check_assist(
332             introduce_named_lifetime,
333             r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
334             r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#,
335         );
336     }
337
338     #[test]
339     fn test_function_add_lifetime_to_param_with_non_ref_self() {
340         check_assist(
341             introduce_named_lifetime,
342             r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
343             r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#,
344         );
345     }
346
347     #[test]
348     fn test_function_add_lifetime_to_self_ref_mut() {
349         check_assist(
350             introduce_named_lifetime,
351             r#"fn foo(&mut self) -> &'_$0 ()"#,
352             r#"fn foo<'a>(&'a mut self) -> &'a ()"#,
353         );
354     }
355 }