From 1a0fe58d81b5703082c06711bfb08c7563dc1701 Mon Sep 17 00:00:00 2001 From: Young-Flash <871946895@qq.com> Date: Tue, 17 Oct 2023 17:34:11 +0800 Subject: [PATCH] refactor: change generated variable name --- .../replace_is_method_with_if_let_method.rs | 58 +++++++++++++++++-- crates/ide-assists/src/tests/generated.rs | 2 +- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs index e5b4c33eb34f..b1daaea1ed1b 100644 --- a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs +++ b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs @@ -1,6 +1,6 @@ use syntax::ast::{self, AstNode}; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; // Assist: replace_is_some_with_if_let_some // @@ -16,7 +16,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // ``` // fn main() { // let x = Some(1); -// if let Some(${0:_tmp}) = x {} +// if let Some(${0:x}) = x {} // } // ``` pub(crate) fn replace_is_method_with_if_let_method( @@ -35,6 +35,13 @@ pub(crate) fn replace_is_method_with_if_let_method( match name_ref.text().as_str() { "is_some" | "is_ok" => { let receiver = call_expr.receiver()?; + + let var_name = if let ast::Expr::PathExpr(path_expr) = receiver.clone() { + path_expr.path()?.to_string() + } else { + suggest_name::for_variable(&receiver, &ctx.sema) + }; + let target = call_expr.syntax().text_range(); let (assist_id, message, text) = if name_ref.text() == "is_some" { @@ -44,7 +51,8 @@ pub(crate) fn replace_is_method_with_if_let_method( }; acc.add(AssistId(assist_id, AssistKind::RefactorRewrite), message, target, |edit| { - let replacement = format!("let {}({}) = {}", text, "${0:_tmp}", receiver); + let var_name = format!("${{0:{}}}", var_name); + let replacement = format!("let {}({}) = {}", text, var_name, receiver); edit.replace(target, replacement); }) } @@ -71,7 +79,27 @@ fn main() { r#" fn main() { let x = Some(1); - if let Some(${0:_tmp}) = x {} + if let Some(${0:x}) = x {} +} +"#, + ); + + check_assist( + replace_is_method_with_if_let_method, + r#" +fn test() -> Option { + Some(1) +} +fn main() { + if test().is_som$0e() {} +} +"#, + r#" +fn test() -> Option { + Some(1) +} +fn main() { + if let Some(${0:test}) = test() {} } "#, ); @@ -103,7 +131,27 @@ fn main() { r#" fn main() { let x = Ok(1); - if let Ok(${0:_tmp}) = x {} + if let Ok(${0:x}) = x {} +} +"#, + ); + + check_assist( + replace_is_method_with_if_let_method, + r#" +fn test() -> Result { + Ok(1) +} +fn main() { + if test().is_o$0k() {} +} +"#, + r#" +fn test() -> Result { + Ok(1) +} +fn main() { + if let Ok(${0:test}) = test() {} } "#, ); diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs index 6b4a97440877..e9d0d373ee7c 100644 --- a/crates/ide-assists/src/tests/generated.rs +++ b/crates/ide-assists/src/tests/generated.rs @@ -2571,7 +2571,7 @@ fn main() { r#####" fn main() { let x = Some(1); - if let Some(${0:_tmp}) = x {} + if let Some(${0:x}) = x {} } "#####, )