2020-08-12 11:26:51 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
use syntax::{
|
2021-04-14 15:56:19 -05:00
|
|
|
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, GenericParamsOwner},
|
|
|
|
ted::{self, Position},
|
|
|
|
AstNode, TextRange,
|
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() == "'_")?;
|
2021-04-14 15:56:19 -05:00
|
|
|
let lifetime_loc = lifetime.lifetime_ident_token()?.text_range();
|
|
|
|
|
2020-12-15 12:23:51 -06:00
|
|
|
if let Some(fn_def) = lifetime.syntax().ancestors().find_map(ast::Fn::cast) {
|
2021-04-14 15:56:19 -05:00
|
|
|
generate_fn_def_assist(acc, fn_def, lifetime_loc, lifetime)
|
2020-12-15 12:23:51 -06:00
|
|
|
} else if let Some(impl_def) = lifetime.syntax().ancestors().find_map(ast::Impl::cast) {
|
2021-04-14 15:56:19 -05:00
|
|
|
generate_impl_def_assist(acc, impl_def, lifetime_loc, lifetime)
|
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,
|
2021-04-14 15:56:19 -05:00
|
|
|
fn_def: ast::Fn,
|
2020-05-28 14:20:26 -05:00
|
|
|
lifetime_loc: TextRange,
|
2021-04-14 15:56:19 -05:00
|
|
|
lifetime: ast::Lifetime,
|
2020-05-28 14:20:26 -05:00
|
|
|
) -> Option<()> {
|
2020-06-01 08:35:02 -05:00
|
|
|
let param_list: ast::ParamList = fn_def.param_list()?;
|
2021-04-14 15:56:19 -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 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
|
2021-04-14 15:56:19 -05:00
|
|
|
Some(NeedsLifetime::SelfParam(self_param))
|
2020-05-28 14:20:26 -05:00
|
|
|
} 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() => {
|
2021-04-14 15:56:19 -05:00
|
|
|
Some(NeedsLifetime::RefType(ascribed_type))
|
2020-05-28 14:20:26 -05:00
|
|
|
}
|
|
|
|
_ => 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| {
|
2021-05-16 06:18:49 -05:00
|
|
|
let fn_def = builder.make_mut(fn_def);
|
|
|
|
let lifetime = builder.make_mut(lifetime);
|
2021-04-14 15:56:19 -05:00
|
|
|
let loc_needing_lifetime =
|
|
|
|
loc_needing_lifetime.and_then(|it| it.make_mut(builder).to_position());
|
|
|
|
|
2021-05-09 11:11:42 -05:00
|
|
|
fn_def.get_or_create_generic_param_list().add_generic_param(
|
|
|
|
make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
|
2021-04-14 15:56:19 -05:00
|
|
|
);
|
2021-05-09 11:11:42 -05:00
|
|
|
ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
|
|
|
|
loc_needing_lifetime
|
|
|
|
.map(|position| ted::insert(position, new_lifetime_param.clone_for_update().syntax()));
|
2020-05-28 14:20:26 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate the assist for the impl def case
|
|
|
|
fn generate_impl_def_assist(
|
|
|
|
acc: &mut Assists,
|
2021-04-14 15:56:19 -05:00
|
|
|
impl_def: ast::Impl,
|
2020-05-28 14:20:26 -05:00
|
|
|
lifetime_loc: TextRange,
|
2021-04-14 15:56:19 -05:00
|
|
|
lifetime: ast::Lifetime,
|
2020-05-28 14:20:26 -05:00
|
|
|
) -> Option<()> {
|
2021-04-14 15:56:19 -05:00
|
|
|
let new_lifetime_param = generate_unique_lifetime_param_name(impl_def.generic_param_list())?;
|
2020-07-02 16:48:35 -05:00
|
|
|
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
|
2021-05-16 06:18:49 -05:00
|
|
|
let impl_def = builder.make_mut(impl_def);
|
|
|
|
let lifetime = builder.make_mut(lifetime);
|
2021-04-14 15:56:19 -05:00
|
|
|
|
2021-05-09 11:11:42 -05:00
|
|
|
impl_def.get_or_create_generic_param_list().add_generic_param(
|
|
|
|
make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
|
2021-04-14 15:56:19 -05:00
|
|
|
);
|
2021-05-09 11:11:42 -05:00
|
|
|
ted::replace(lifetime.syntax(), new_lifetime_param.clone_for_update().syntax());
|
2020-05-28 14:20:26 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a type parameter list, generate a unique lifetime parameter name
|
|
|
|
/// which is not in the list
|
|
|
|
fn generate_unique_lifetime_param_name(
|
2021-04-14 15:56:19 -05:00
|
|
|
existing_type_param_list: Option<ast::GenericParamList>,
|
2021-05-09 11:11:42 -05:00
|
|
|
) -> Option<ast::Lifetime> {
|
2020-05-28 14:20:26 -05:00
|
|
|
match existing_type_param_list {
|
2020-05-22 19:09:37 -05:00
|
|
|
Some(type_params) => {
|
2021-05-09 11:11:42 -05:00
|
|
|
let used_lifetime_params: FxHashSet<_> =
|
|
|
|
type_params.lifetime_params().map(|p| p.syntax().text().to_string()).collect();
|
|
|
|
('a'..='z').map(|it| format!("'{}", it)).find(|it| !used_lifetime_params.contains(it))
|
2020-05-22 19:09:37 -05:00
|
|
|
}
|
2021-05-09 11:11:42 -05:00
|
|
|
None => Some("'a".to_string()),
|
2020-05-28 14:20:26 -05:00
|
|
|
}
|
2021-05-09 11:11:42 -05:00
|
|
|
.map(|it| make::lifetime(&it))
|
2021-04-14 15:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum NeedsLifetime {
|
|
|
|
SelfParam(ast::SelfParam),
|
|
|
|
RefType(ast::RefType),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NeedsLifetime {
|
|
|
|
fn make_mut(self, builder: &mut AssistBuilder) -> Self {
|
|
|
|
match self {
|
2021-05-16 06:18:49 -05:00
|
|
|
Self::SelfParam(it) => Self::SelfParam(builder.make_mut(it)),
|
|
|
|
Self::RefType(it) => Self::RefType(builder.make_mut(it)),
|
2020-05-28 14:20:26 -05:00
|
|
|
}
|
2021-04-14 15:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_position(self) -> Option<Position> {
|
|
|
|
match self {
|
|
|
|
Self::SelfParam(it) => Some(Position::after(it.amp_token()?)),
|
|
|
|
Self::RefType(it) => Some(Position::after(it.amp_token()?)),
|
2020-05-28 14:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
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>"#,
|
|
|
|
);
|
|
|
|
}
|
2021-04-14 15:56:19 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_function_add_lifetime_to_self_ref_mut() {
|
|
|
|
check_assist(
|
|
|
|
introduce_named_lifetime,
|
|
|
|
r#"fn foo(&mut self) -> &'_$0 ()"#,
|
|
|
|
r#"fn foo<'a>(&'a mut self) -> &'a ()"#,
|
|
|
|
);
|
|
|
|
}
|
2020-05-20 18:37:09 -05:00
|
|
|
}
|