Rename assist

This commit is contained in:
Aleksey Kladov 2020-06-01 15:36:51 +02:00
parent d08232b10d
commit 285717de33
4 changed files with 78 additions and 81 deletions

View file

@ -6,10 +6,10 @@ use rustc_hash::FxHashSet;
use crate::{assist_context::AssistBuilder, AssistContext, AssistId, Assists};
static ASSIST_NAME: &str = "change_lifetime_anon_to_named";
static ASSIST_LABEL: &str = "Give anonymous lifetime a name";
static ASSIST_NAME: &str = "introduce_named_lifetime";
static ASSIST_LABEL: &str = "Introduce named lifetime";
// Assist: change_lifetime_anon_to_named
// Assist: introduce_named_lifetime
//
// Change an anonymous lifetime to a named lifetime.
//
@ -34,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() == "'_")?;
@ -154,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 {
@ -175,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> {"#,
);
@ -184,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> {"#,
);
@ -193,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> {"#,
);
@ -201,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<T> Cursor<T, '_<|>>"#,
r#"impl<T, 'a> Cursor<T, 'a>"#,
);
@ -226,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>"#,
);
@ -235,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>"#,
);
@ -244,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"#,
);
@ -253,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>)"#,
);
@ -262,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>"#,
);
@ -271,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>"#,
);
@ -281,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<'_<|>>"#,
);
}
@ -289,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>"#,
);
@ -298,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>"#,
);

View file

@ -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,

View file

@ -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(

View file

@ -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]