aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs89
1 files changed, 40 insertions, 49 deletions
diff --git a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs b/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
index 67d50cfda..922213607 100644
--- a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
+++ b/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
@@ -37,57 +37,48 @@ pub(crate) fn change_lifetime_anon_to_named(acc: &mut Assists, ctx: &AssistConte
37 // only allow naming the last anonymous lifetime 37 // only allow naming the last anonymous lifetime
38 return None; 38 return None;
39 } 39 }
40 match lifetime_arg.syntax().ancestors().find_map(ast::ImplDef::cast) { 40 let impl_def = lifetime_arg.syntax().ancestors().find_map(ast::ImplDef::cast)?;
41 Some(impl_def) => { 41 // get the `impl` keyword so we know where to add the lifetime argument
42 // get the `impl` keyword so we know where to add the lifetime argument 42 let impl_kw = impl_def.syntax().first_child_or_token()?.into_token()?;
43 let impl_kw = impl_def.syntax().first_child_or_token()?.into_token()?; 43 if impl_kw.kind() != SyntaxKind::IMPL_KW {
44 if impl_kw.kind() != SyntaxKind::IMPL_KW { 44 return None;
45 return None; 45 }
46 } 46 let new_lifetime_param = match impl_def.type_param_list() {
47 let new_lifetime_param = match impl_def.type_param_list() { 47 Some(type_params) => {
48 let used_lifetime_params: HashSet<_> = type_params
49 .lifetime_params()
50 .map(|p| {
51 let mut param_name = p.syntax().text().to_string();
52 param_name.remove(0);
53 param_name
54 })
55 .collect();
56 (b'a'..=b'z')
57 .map(char::from)
58 .find(|c| !used_lifetime_params.contains(&c.to_string()))?
59 }
60 None => 'a',
61 };
62 acc.add(
63 AssistId("change_lifetime_anon_to_named"),
64 "Give anonymous lifetime a name",
65 lifetime_arg.syntax().text_range(),
66 |builder| {
67 match impl_def.type_param_list() {
48 Some(type_params) => { 68 Some(type_params) => {
49 let used_lifetime_params: HashSet<_> = type_params 69 builder.insert(
50 .lifetime_params() 70 (u32::from(type_params.syntax().text_range().end()) - 1).into(),
51 .map(|p| { 71 format!(", '{}", new_lifetime_param),
52 let mut param_name = p.syntax().text().to_string();
53 param_name.remove(0);
54 param_name
55 })
56 .collect();
57 (b'a'..=b'z')
58 .map(char::from)
59 .find(|c| !used_lifetime_params.contains(&c.to_string()))?
60 }
61 None => 'a',
62 };
63 acc.add(
64 AssistId("change_lifetime_anon_to_named"),
65 "Give anonymous lifetime a name",
66 lifetime_arg.syntax().text_range(),
67 |builder| {
68 match impl_def.type_param_list() {
69 Some(type_params) => {
70 builder.insert(
71 (u32::from(type_params.syntax().text_range().end()) - 1).into(),
72 format!(", '{}", new_lifetime_param),
73 );
74 }
75 None => {
76 builder.insert(
77 impl_kw.text_range().end(),
78 format!("<'{}>", new_lifetime_param),
79 );
80 }
81 }
82 builder.replace(
83 lifetime_arg.syntax().text_range(),
84 format!("'{}", new_lifetime_param),
85 ); 72 );
86 }, 73 }
87 ) 74 None => {
88 } 75 builder
89 _ => None, 76 .insert(impl_kw.text_range().end(), format!("<'{}>", new_lifetime_param));
90 } 77 }
78 }
79 builder.replace(lifetime_arg.syntax().text_range(), format!("'{}", new_lifetime_param));
80 },
81 )
91} 82}
92 83
93#[cfg(test)] 84#[cfg(test)]