Skip to content

Commit 4967b81

Browse files
committed
tweak syntax
1 parent d42fd8e commit 4967b81

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,57 +37,48 @@ pub(crate) fn change_lifetime_anon_to_named(acc: &mut Assists, ctx: &AssistConte
3737
// only allow naming the last anonymous lifetime
3838
return None;
3939
}
40-
match lifetime_arg.syntax().ancestors().find_map(ast::ImplDef::cast) {
41-
Some(impl_def) => {
42-
// get the `impl` keyword so we know where to add the lifetime argument
43-
let impl_kw = impl_def.syntax().first_child_or_token()?.into_token()?;
44-
if impl_kw.kind() != SyntaxKind::IMPL_KW {
45-
return None;
46-
}
47-
let new_lifetime_param = match impl_def.type_param_list() {
40+
let impl_def = lifetime_arg.syntax().ancestors().find_map(ast::ImplDef::cast)?;
41+
// 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+
if impl_kw.kind() != SyntaxKind::IMPL_KW {
44+
return None;
45+
}
46+
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() {
4868
Some(type_params) => {
49-
let used_lifetime_params: HashSet<_> = type_params
50-
.lifetime_params()
51-
.map(|p| {
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),
69+
builder.insert(
70+
(u32::from(type_params.syntax().text_range().end()) - 1).into(),
71+
format!(", '{}", new_lifetime_param),
8572
);
86-
},
87-
)
88-
}
89-
_ => None,
90-
}
73+
}
74+
None => {
75+
builder
76+
.insert(impl_kw.text_range().end(), format!("<'{}>", new_lifetime_param));
77+
}
78+
}
79+
builder.replace(lifetime_arg.syntax().text_range(), format!("'{}", new_lifetime_param));
80+
},
81+
)
9182
}
9283

9384
#[cfg(test)]

0 commit comments

Comments
 (0)