diff --git a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs b/crates/ra_assists/src/handlers/introduce_named_lifetime.rs similarity index 84% rename from crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs rename to crates/ra_assists/src/handlers/introduce_named_lifetime.rs index 999aec421b..beb5b7366d 100644 --- a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs +++ b/crates/ra_assists/src/handlers/introduce_named_lifetime.rs @@ -1,12 +1,15 @@ -use crate::{assist_context::AssistBuilder, AssistContext, AssistId, Assists}; -use ast::{NameOwner, ParamList, TypeAscriptionOwner, TypeParamList, TypeRef}; -use ra_syntax::{ast, ast::TypeParamsOwner, AstNode, SyntaxKind, TextRange, TextSize}; +use ra_syntax::{ + ast::{self, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, + AstNode, SyntaxKind, TextRange, TextSize, +}; use rustc_hash::FxHashSet; -static ASSIST_NAME: &str = "change_lifetime_anon_to_named"; -static ASSIST_LABEL: &str = "Give anonymous lifetime a name"; +use crate::{assist_context::AssistBuilder, AssistContext, AssistId, Assists}; -// Assist: change_lifetime_anon_to_named +static ASSIST_NAME: &str = "introduce_named_lifetime"; +static ASSIST_LABEL: &str = "Introduce named lifetime"; + +// Assist: introduce_named_lifetime // // Change an anonymous lifetime to a named lifetime. // @@ -31,7 +34,7 @@ static ASSIST_LABEL: &str = "Give anonymous lifetime a name"; // ``` // FIXME: How can we handle renaming any one of multiple anonymous lifetimes? // FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo -pub(crate) fn change_lifetime_anon_to_named(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { +pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let lifetime_token = ctx .find_token_at_offset(SyntaxKind::LIFETIME) .filter(|lifetime| lifetime.text() == "'_")?; @@ -52,7 +55,7 @@ fn generate_fn_def_assist( fn_def: &ast::FnDef, lifetime_loc: TextRange, ) -> Option<()> { - let param_list: ParamList = fn_def.param_list()?; + let param_list: ast::ParamList = fn_def.param_list()?; let new_lifetime_param = generate_unique_lifetime_param_name(&fn_def.type_param_list())?; let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end(); let self_param = @@ -67,7 +70,7 @@ fn generate_fn_def_assist( let fn_params_without_lifetime: Vec<_> = param_list .params() .filter_map(|param| match param.ascribed_type() { - Some(TypeRef::ReferenceType(ascribed_type)) + Some(ast::TypeRef::ReferenceType(ascribed_type)) if ascribed_type.lifetime_token() == None => { Some(ascribed_type.amp_token()?.text_range().end()) @@ -106,7 +109,7 @@ fn generate_impl_def_assist( /// Given a type parameter list, generate a unique lifetime parameter name /// which is not in the list fn generate_unique_lifetime_param_name( - existing_type_param_list: &Option, + existing_type_param_list: &Option, ) -> Option { match existing_type_param_list { Some(type_params) => { @@ -151,7 +154,7 @@ mod tests { #[test] fn test_example_case() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"impl Cursor<'_<|>> { fn node(self) -> &SyntaxNode { match self { @@ -172,7 +175,7 @@ mod tests { #[test] fn test_example_case_simplified() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"impl Cursor<'_<|>> {"#, r#"impl<'a> Cursor<'a> {"#, ); @@ -181,7 +184,7 @@ mod tests { #[test] fn test_example_case_cursor_after_tick() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"impl Cursor<'<|>_> {"#, r#"impl<'a> Cursor<'a> {"#, ); @@ -190,7 +193,7 @@ mod tests { #[test] fn test_example_case_cursor_before_tick() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"impl Cursor<<|>'_> {"#, r#"impl<'a> Cursor<'a> {"#, ); @@ -198,23 +201,20 @@ mod tests { #[test] fn test_not_applicable_cursor_position() { - check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'_><|> {"#); - check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<|><'_> {"#); + check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'_><|> {"#); + check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<|><'_> {"#); } #[test] fn test_not_applicable_lifetime_already_name() { - check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'a<|>> {"#); - check_assist_not_applicable( - change_lifetime_anon_to_named, - r#"fn my_fun<'a>() -> X<'a<|>>"#, - ); + check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'a<|>> {"#); + check_assist_not_applicable(introduce_named_lifetime, r#"fn my_fun<'a>() -> X<'a<|>>"#); } #[test] fn test_with_type_parameter() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"impl Cursor>"#, r#"impl Cursor"#, ); @@ -223,7 +223,7 @@ mod tests { #[test] fn test_with_existing_lifetime_name_conflict() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"impl<'a, 'b> Cursor<'a, 'b, '_<|>>"#, r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#, ); @@ -232,7 +232,7 @@ mod tests { #[test] fn test_function_return_value_anon_lifetime_param() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun() -> X<'_<|>>"#, r#"fn my_fun<'a>() -> X<'a>"#, ); @@ -241,7 +241,7 @@ mod tests { #[test] fn test_function_return_value_anon_reference_lifetime() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun() -> &'_<|> X"#, r#"fn my_fun<'a>() -> &'a X"#, ); @@ -250,7 +250,7 @@ mod tests { #[test] fn test_function_param_anon_lifetime() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun(x: X<'_<|>>)"#, r#"fn my_fun<'a>(x: X<'a>)"#, ); @@ -259,7 +259,7 @@ mod tests { #[test] fn test_function_add_lifetime_to_params() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun(f: &Foo) -> X<'_<|>>"#, r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#, ); @@ -268,7 +268,7 @@ mod tests { #[test] fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_<|>>"#, r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#, ); @@ -278,7 +278,7 @@ mod tests { fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() { // this is not permitted under lifetime elision rules check_assist_not_applicable( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_<|>>"#, ); } @@ -286,7 +286,7 @@ mod tests { #[test] fn test_function_add_lifetime_to_self_ref_param() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#, r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#, ); @@ -295,7 +295,7 @@ mod tests { #[test] fn test_function_add_lifetime_to_param_with_non_ref_self() { check_assist( - change_lifetime_anon_to_named, + introduce_named_lifetime, r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#, r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#, ); diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 3f8f7ffbfc..fb5d59a87b 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -112,7 +112,6 @@ mod handlers { mod add_turbo_fish; mod apply_demorgan; mod auto_import; - mod change_lifetime_anon_to_named; mod change_return_type_to_result; mod change_visibility; mod early_return; @@ -122,6 +121,7 @@ mod handlers { mod flip_comma; mod flip_trait_bound; mod inline_local_variable; + mod introduce_named_lifetime; mod introduce_variable; mod invert_if; mod merge_imports; @@ -152,7 +152,6 @@ mod handlers { add_turbo_fish::add_turbo_fish, apply_demorgan::apply_demorgan, auto_import::auto_import, - change_lifetime_anon_to_named::change_lifetime_anon_to_named, change_return_type_to_result::change_return_type_to_result, change_visibility::change_visibility, early_return::convert_to_guarded_return, @@ -162,6 +161,7 @@ mod handlers { flip_comma::flip_comma, flip_trait_bound::flip_trait_bound, inline_local_variable::inline_local_variable, + introduce_named_lifetime::introduce_named_lifetime, introduce_variable::introduce_variable, invert_if::invert_if, merge_imports::merge_imports, diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 73d43283d4..d17504529f 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs @@ -287,31 +287,6 @@ pub mod std { pub mod collections { pub struct HashMap { } } } ) } -#[test] -fn doctest_change_lifetime_anon_to_named() { - check_doc_test( - "change_lifetime_anon_to_named", - r#####" -impl Cursor<'_<|>> { - 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 doctest_change_return_type_to_result() { check_doc_test( @@ -476,6 +451,31 @@ fn main() { ) } +#[test] +fn doctest_introduce_named_lifetime() { + check_doc_test( + "introduce_named_lifetime", + r#####" +impl Cursor<'_<|>> { + 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 doctest_introduce_variable() { check_doc_test( diff --git a/docs/user/generated_assists.adoc b/docs/user/generated_assists.adoc index 580ab4358e..4d2fb31d48 100644 --- a/docs/user/generated_assists.adoc +++ b/docs/user/generated_assists.adoc @@ -337,35 +337,6 @@ fn main() { ``` -[discrete] -=== `change_lifetime_anon_to_named` -**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs#L9[change_lifetime_anon_to_named.rs] - -Change an anonymous lifetime to a named lifetime. - -.Before -```rust -impl Cursor<'_┃> { - fn node(self) -> &SyntaxNode { - match self { - Cursor::Replace(node) | Cursor::Before(node) => node, - } - } -} -``` - -.After -```rust -impl<'a> Cursor<'a> { - fn node(self) -> &SyntaxNode { - match self { - Cursor::Replace(node) | Cursor::Before(node) => node, - } - } -} -``` - - [discrete] === `change_return_type_to_result` **Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/change_return_type_to_result.rs#L8[change_return_type_to_result.rs] @@ -566,6 +537,35 @@ fn main() { ``` +[discrete] +=== `introduce_named_lifetime` +**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/introduce_named_lifetime.rs#L12[introduce_named_lifetime.rs] + +Change an anonymous lifetime to a named lifetime. + +.Before +```rust +impl Cursor<'_┃> { + fn node(self) -> &SyntaxNode { + match self { + Cursor::Replace(node) | Cursor::Before(node) => node, + } + } +} +``` + +.After +```rust +impl<'a> Cursor<'a> { + fn node(self) -> &SyntaxNode { + match self { + Cursor::Replace(node) | Cursor::Before(node) => node, + } + } +} +``` + + [discrete] === `introduce_variable` **Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/introduce_variable.rs#L14[introduce_variable.rs]