2020-08-12 11:26:51 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
use syntax::{
|
2020-07-30 13:51:43 -05:00
|
|
|
ast::{self, GenericParamsOwner, NameOwner},
|
2020-12-15 12:23:51 -06:00
|
|
|
AstNode, TextRange, TextSize,
|
2020-06-01 08:35:02 -05:00
|
|
|
};
|
2020-05-28 14:20:26 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists};
|
2020-06-01 08:35:02 -05:00
|
|
|
|
2020-06-01 08:36:51 -05:00
|
|
|
static ASSIST_NAME: &str = "introduce_named_lifetime";
|
|
|
|
static ASSIST_LABEL: &str = "Introduce named lifetime";
|
2020-05-20 18:37:09 -05:00
|
|
|
|
2020-06-01 08:36:51 -05:00
|
|
|
// Assist: introduce_named_lifetime
|
2020-05-22 09:25:55 -05:00
|
|
|
//
|
|
|
|
// Change an anonymous lifetime to a named lifetime.
|
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// impl Cursor<'_$0> {
|
2020-05-22 09:25:55 -05:00
|
|
|
// fn node(self) -> &SyntaxNode {
|
|
|
|
// match self {
|
|
|
|
// Cursor::Replace(node) | Cursor::Before(node) => node,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// impl<'a> Cursor<'a> {
|
|
|
|
// fn node(self) -> &SyntaxNode {
|
|
|
|
// match self {
|
|
|
|
// Cursor::Replace(node) | Cursor::Before(node) => node,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2020-05-22 08:51:37 -05:00
|
|
|
// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
|
2021-01-06 14:15:48 -06:00
|
|
|
// FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo
|
2020-06-01 08:36:51 -05:00
|
|
|
pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-12-15 12:23:51 -06:00
|
|
|
let lifetime =
|
|
|
|
ctx.find_node_at_offset::<ast::Lifetime>().filter(|lifetime| lifetime.text() == "'_")?;
|
|
|
|
if let Some(fn_def) = lifetime.syntax().ancestors().find_map(ast::Fn::cast) {
|
|
|
|
generate_fn_def_assist(acc, &fn_def, lifetime.lifetime_ident_token()?.text_range())
|
|
|
|
} else if let Some(impl_def) = lifetime.syntax().ancestors().find_map(ast::Impl::cast) {
|
|
|
|
generate_impl_def_assist(acc, &impl_def, lifetime.lifetime_ident_token()?.text_range())
|
2020-05-28 14:20:26 -05:00
|
|
|
} else {
|
|
|
|
None
|
2020-05-22 19:09:37 -05:00
|
|
|
}
|
2020-05-28 14:20:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate the assist for the fn def case
|
|
|
|
fn generate_fn_def_assist(
|
|
|
|
acc: &mut Assists,
|
2020-07-30 07:51:08 -05:00
|
|
|
fn_def: &ast::Fn,
|
2020-05-28 14:20:26 -05:00
|
|
|
lifetime_loc: TextRange,
|
|
|
|
) -> Option<()> {
|
2020-06-01 08:35:02 -05:00
|
|
|
let param_list: ast::ParamList = fn_def.param_list()?;
|
2020-07-30 08:36:21 -05:00
|
|
|
let new_lifetime_param = generate_unique_lifetime_param_name(&fn_def.generic_param_list())?;
|
2020-05-28 14:20:26 -05:00
|
|
|
let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end();
|
|
|
|
let self_param =
|
|
|
|
// use the self if it's a reference and has no explicit lifetime
|
2020-12-15 12:23:51 -06:00
|
|
|
param_list.self_param().filter(|p| p.lifetime().is_none() && p.amp_token().is_some());
|
2020-05-28 14:20:26 -05:00
|
|
|
// compute the location which implicitly has the same lifetime as the anonymous lifetime
|
|
|
|
let loc_needing_lifetime = if let Some(self_param) = self_param {
|
|
|
|
// if we have a self reference, use that
|
|
|
|
Some(self_param.self_token()?.text_range().start())
|
|
|
|
} else {
|
|
|
|
// otherwise, if there's a single reference parameter without a named liftime, use that
|
|
|
|
let fn_params_without_lifetime: Vec<_> = param_list
|
|
|
|
.params()
|
2020-07-30 13:51:43 -05:00
|
|
|
.filter_map(|param| match param.ty() {
|
2020-12-15 12:23:51 -06:00
|
|
|
Some(ast::Type::RefType(ascribed_type)) if ascribed_type.lifetime().is_none() => {
|
2020-05-28 14:20:26 -05:00
|
|
|
Some(ascribed_type.amp_token()?.text_range().end())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
match fn_params_without_lifetime.len() {
|
|
|
|
1 => Some(fn_params_without_lifetime.into_iter().nth(0)?),
|
|
|
|
0 => None,
|
|
|
|
// multiple unnnamed is invalid. assist is not applicable
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
2020-07-02 16:48:35 -05:00
|
|
|
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
|
2020-05-28 14:20:26 -05:00
|
|
|
add_lifetime_param(fn_def, builder, end_of_fn_ident, new_lifetime_param);
|
|
|
|
builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
|
|
|
|
loc_needing_lifetime.map(|loc| builder.insert(loc, format!("'{} ", new_lifetime_param)));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate the assist for the impl def case
|
|
|
|
fn generate_impl_def_assist(
|
|
|
|
acc: &mut Assists,
|
2020-07-30 11:28:28 -05:00
|
|
|
impl_def: &ast::Impl,
|
2020-05-28 14:20:26 -05:00
|
|
|
lifetime_loc: TextRange,
|
|
|
|
) -> Option<()> {
|
2020-07-30 08:36:21 -05:00
|
|
|
let new_lifetime_param = generate_unique_lifetime_param_name(&impl_def.generic_param_list())?;
|
2020-05-28 14:20:26 -05:00
|
|
|
let end_of_impl_kw = impl_def.impl_token()?.text_range().end();
|
2020-07-02 16:48:35 -05:00
|
|
|
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
|
2020-05-28 14:20:26 -05:00
|
|
|
add_lifetime_param(impl_def, builder, end_of_impl_kw, new_lifetime_param);
|
|
|
|
builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a type parameter list, generate a unique lifetime parameter name
|
|
|
|
/// which is not in the list
|
|
|
|
fn generate_unique_lifetime_param_name(
|
2020-07-30 08:36:21 -05:00
|
|
|
existing_type_param_list: &Option<ast::GenericParamList>,
|
2020-05-28 14:20:26 -05:00
|
|
|
) -> Option<char> {
|
|
|
|
match existing_type_param_list {
|
2020-05-22 19:09:37 -05:00
|
|
|
Some(type_params) => {
|
2020-05-28 14:20:26 -05:00
|
|
|
let used_lifetime_params: FxHashSet<_> = type_params
|
2020-05-22 19:09:37 -05:00
|
|
|
.lifetime_params()
|
2020-05-28 14:20:26 -05:00
|
|
|
.map(|p| p.syntax().text().to_string()[1..].to_owned())
|
2020-05-22 19:09:37 -05:00
|
|
|
.collect();
|
2020-05-28 14:20:26 -05:00
|
|
|
(b'a'..=b'z').map(char::from).find(|c| !used_lifetime_params.contains(&c.to_string()))
|
2020-05-22 19:09:37 -05:00
|
|
|
}
|
2020-05-28 14:20:26 -05:00
|
|
|
None => Some('a'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the lifetime param to `builder`. If there are type parameters in `type_params_owner`, add it to the end. Otherwise
|
|
|
|
/// add new type params brackets with the lifetime parameter at `new_type_params_loc`.
|
2020-07-30 08:36:21 -05:00
|
|
|
fn add_lifetime_param<TypeParamsOwner: ast::GenericParamsOwner>(
|
2020-05-28 14:20:26 -05:00
|
|
|
type_params_owner: &TypeParamsOwner,
|
|
|
|
builder: &mut AssistBuilder,
|
|
|
|
new_type_params_loc: TextSize,
|
|
|
|
new_lifetime_param: char,
|
|
|
|
) {
|
2020-07-30 08:36:21 -05:00
|
|
|
match type_params_owner.generic_param_list() {
|
2020-05-28 14:20:26 -05:00
|
|
|
// add the new lifetime parameter to an existing type param list
|
|
|
|
Some(type_params) => {
|
|
|
|
builder.insert(
|
|
|
|
(u32::from(type_params.syntax().text_range().end()) - 1).into(),
|
|
|
|
format!(", '{}", new_lifetime_param),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// create a new type param list containing only the new lifetime parameter
|
|
|
|
None => {
|
|
|
|
builder.insert(new_type_params_loc, format!("<'{}>", new_lifetime_param));
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 18:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_example_case() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"impl Cursor<'_$0> {
|
2020-05-20 18:37:09 -05:00
|
|
|
fn node(self) -> &SyntaxNode {
|
|
|
|
match self {
|
|
|
|
Cursor::Replace(node) | Cursor::Before(node) => node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"impl<'a> Cursor<'a> {
|
|
|
|
fn node(self) -> &SyntaxNode {
|
|
|
|
match self {
|
|
|
|
Cursor::Replace(node) | Cursor::Before(node) => node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_example_case_simplified() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"impl Cursor<'_$0> {"#,
|
2020-05-20 18:37:09 -05:00
|
|
|
r#"impl<'a> Cursor<'a> {"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-28 14:20:26 -05:00
|
|
|
fn test_example_case_cursor_after_tick() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"impl Cursor<'$0_> {"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"impl<'a> Cursor<'a> {"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-03 17:54:23 -05:00
|
|
|
#[test]
|
|
|
|
fn test_impl_with_other_type_param() {
|
|
|
|
check_assist(
|
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
"impl<I> fmt::Display for SepByBuilder<'_$0, I>
|
2020-06-03 17:54:23 -05:00
|
|
|
where
|
|
|
|
I: Iterator,
|
|
|
|
I::Item: fmt::Display,
|
|
|
|
{",
|
|
|
|
"impl<I, 'a> fmt::Display for SepByBuilder<'a, I>
|
|
|
|
where
|
|
|
|
I: Iterator,
|
|
|
|
I::Item: fmt::Display,
|
|
|
|
{",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:20:26 -05:00
|
|
|
#[test]
|
|
|
|
fn test_example_case_cursor_before_tick() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"impl Cursor<$0'_> {"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"impl<'a> Cursor<'a> {"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_applicable_cursor_position() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'_>$0 {"#);
|
|
|
|
check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor$0<'_> {"#);
|
2020-05-28 14:20:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_applicable_lifetime_already_name() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'a$0> {"#);
|
|
|
|
check_assist_not_applicable(introduce_named_lifetime, r#"fn my_fun<'a>() -> X<'a$0>"#);
|
2020-05-20 18:37:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_type_parameter() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"impl<T> Cursor<T, '_$0>"#,
|
2020-05-20 18:37:09 -05:00
|
|
|
r#"impl<T, 'a> Cursor<T, 'a>"#,
|
|
|
|
);
|
|
|
|
}
|
2020-05-22 08:51:37 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_existing_lifetime_name_conflict() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"impl<'a, 'b> Cursor<'a, 'b, '_$0>"#,
|
2020-05-22 08:51:37 -05:00
|
|
|
r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
|
|
|
|
);
|
|
|
|
}
|
2020-05-28 14:20:26 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_return_value_anon_lifetime_param() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun() -> X<'_$0>"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'a>() -> X<'a>"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_return_value_anon_reference_lifetime() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun() -> &'_$0 X"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'a>() -> &'a X"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_param_anon_lifetime() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun(x: X<'_$0>)"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'a>(x: X<'a>)"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_add_lifetime_to_params() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun(f: &Foo) -> X<'_$0>"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_$0>"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() {
|
|
|
|
// this is not permitted under lifetime elision rules
|
|
|
|
check_assist_not_applicable(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_$0>"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_add_lifetime_to_self_ref_param() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_add_lifetime_to_param_with_non_ref_self() {
|
|
|
|
check_assist(
|
2020-06-01 08:36:51 -05:00
|
|
|
introduce_named_lifetime,
|
2021-01-06 14:15:48 -06:00
|
|
|
r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_$0>"#,
|
2020-05-28 14:20:26 -05:00
|
|
|
r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#,
|
|
|
|
);
|
|
|
|
}
|
2020-05-20 18:37:09 -05:00
|
|
|
}
|