3 ast::{self, edit_in_place::GenericParamsOwnerEdit, make, HasGenericParams},
8 use crate::{assist_context::SourceChangeBuilder, 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 pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
37 // FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
38 // FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo
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().next()?),
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());
96 if let Some(position) = loc_needing_lifetime {
97 ted::insert(position, new_lifetime_param.clone_for_update().syntax());
102 /// Generate the assist for the impl def case
103 fn generate_impl_def_assist(
106 lifetime_loc: TextRange,
107 lifetime: ast::Lifetime,
109 let new_lifetime_param = generate_unique_lifetime_param_name(impl_def.generic_param_list())?;
110 acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
111 let impl_def = builder.make_mut(impl_def);
112 let lifetime = builder.make_mut(lifetime);
114 impl_def.get_or_create_generic_param_list().add_generic_param(
115 make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
117 ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
121 /// Given a type parameter list, generate a unique lifetime parameter name
122 /// which is not in the list
123 fn generate_unique_lifetime_param_name(
124 existing_type_param_list: Option<ast::GenericParamList>,
125 ) -> Option<ast::Lifetime> {
126 match existing_type_param_list {
127 Some(type_params) => {
128 let used_lifetime_params: FxHashSet<_> =
129 type_params.lifetime_params().map(|p| p.syntax().text().to_string()).collect();
130 ('a'..='z').map(|it| format!("'{}", it)).find(|it| !used_lifetime_params.contains(it))
132 None => Some("'a".to_string()),
134 .map(|it| make::lifetime(&it))
138 SelfParam(ast::SelfParam),
139 RefType(ast::RefType),
143 fn make_mut(self, builder: &mut SourceChangeBuilder) -> Self {
145 Self::SelfParam(it) => Self::SelfParam(builder.make_mut(it)),
146 Self::RefType(it) => Self::RefType(builder.make_mut(it)),
150 fn to_position(self) -> Option<Position> {
152 Self::SelfParam(it) => Some(Position::after(it.amp_token()?)),
153 Self::RefType(it) => Some(Position::after(it.amp_token()?)),
161 use crate::tests::{check_assist, check_assist_not_applicable};
164 fn test_example_case() {
166 introduce_named_lifetime,
167 r#"impl Cursor<'_$0> {
168 fn node(self) -> &SyntaxNode {
170 Cursor::Replace(node) | Cursor::Before(node) => node,
174 r#"impl<'a> Cursor<'a> {
175 fn node(self) -> &SyntaxNode {
177 Cursor::Replace(node) | Cursor::Before(node) => node,
185 fn test_example_case_simplified() {
187 introduce_named_lifetime,
188 r#"impl Cursor<'_$0> {"#,
189 r#"impl<'a> Cursor<'a> {"#,
194 fn test_example_case_cursor_after_tick() {
196 introduce_named_lifetime,
197 r#"impl Cursor<'$0_> {"#,
198 r#"impl<'a> Cursor<'a> {"#,
203 fn test_impl_with_other_type_param() {
205 introduce_named_lifetime,
206 "impl<I> fmt::Display for SepByBuilder<'_$0, I>
209 I::Item: fmt::Display,
211 "impl<I, 'a> fmt::Display for SepByBuilder<'a, I>
214 I::Item: fmt::Display,
220 fn test_example_case_cursor_before_tick() {
222 introduce_named_lifetime,
223 r#"impl Cursor<$0'_> {"#,
224 r#"impl<'a> Cursor<'a> {"#,
229 fn test_not_applicable_cursor_position() {
230 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'_>$0 {"#);
231 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor$0<'_> {"#);
235 fn test_not_applicable_lifetime_already_name() {
236 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'a$0> {"#);
237 check_assist_not_applicable(introduce_named_lifetime, r#"fn my_fun<'a>() -> X<'a$0>"#);
241 fn test_with_type_parameter() {
243 introduce_named_lifetime,
244 r#"impl<T> Cursor<T, '_$0>"#,
245 r#"impl<T, 'a> Cursor<T, 'a>"#,
250 fn test_with_existing_lifetime_name_conflict() {
252 introduce_named_lifetime,
253 r#"impl<'a, 'b> Cursor<'a, 'b, '_$0>"#,
254 r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
259 fn test_function_return_value_anon_lifetime_param() {
261 introduce_named_lifetime,
262 r#"fn my_fun() -> X<'_$0>"#,
263 r#"fn my_fun<'a>() -> X<'a>"#,
268 fn test_function_return_value_anon_reference_lifetime() {
270 introduce_named_lifetime,
271 r#"fn my_fun() -> &'_$0 X"#,
272 r#"fn my_fun<'a>() -> &'a X"#,
277 fn test_function_param_anon_lifetime() {
279 introduce_named_lifetime,
280 r#"fn my_fun(x: X<'_$0>)"#,
281 r#"fn my_fun<'a>(x: X<'a>)"#,
286 fn test_function_add_lifetime_to_params() {
288 introduce_named_lifetime,
289 r#"fn my_fun(f: &Foo) -> X<'_$0>"#,
290 r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
295 fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
297 introduce_named_lifetime,
298 r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_$0>"#,
299 r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#,
304 fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() {
305 // this is not permitted under lifetime elision rules
306 check_assist_not_applicable(
307 introduce_named_lifetime,
308 r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_$0>"#,
313 fn test_function_add_lifetime_to_self_ref_param() {
315 introduce_named_lifetime,
316 r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
317 r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#,
322 fn test_function_add_lifetime_to_param_with_non_ref_self() {
324 introduce_named_lifetime,
325 r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
326 r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#,
331 fn test_function_add_lifetime_to_self_ref_mut() {
333 introduce_named_lifetime,
334 r#"fn foo(&mut self) -> &'_$0 ()"#,
335 r#"fn foo<'a>(&'a mut self) -> &'a ()"#,