From fd1585a071718ef9c9fb44f88336608dd7e624a5 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Mon, 25 Mar 2019 01:05:11 +0800 Subject: [PATCH 1/2] inline immutable local varialbe --- .../ra_assists/src/inline_local_variable.rs | 298 ++++++++++++++++++ crates/ra_assists/src/lib.rs | 2 + 2 files changed, 300 insertions(+) create mode 100644 crates/ra_assists/src/inline_local_variable.rs diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs new file mode 100644 index 00000000000..ce0aafb27f1 --- /dev/null +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -0,0 +1,298 @@ +use hir::db::HirDatabase; +use hir::source_binder::function_from_child_node; +use ra_syntax::{ast::{self, AstNode}, TextRange}; +use ra_syntax::ast::{PatKind, ExprKind}; + +use crate::{Assist, AssistCtx, AssistId}; +use crate::assist_ctx::AssistBuilder; + +pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Option { + let let_stmt = ctx.node_at_offset::()?; + let bind_pat = match let_stmt.pat()?.kind() { + PatKind::BindPat(pat) => pat, + _ => return None, + }; + if bind_pat.is_mutable() { + return None; + } + let initializer = let_stmt.initializer()?; + let wrap_in_parens = match initializer.kind() { + ExprKind::LambdaExpr(_) => true, + ExprKind::IfExpr(_) => true, + ExprKind::LoopExpr(_) => true, + ExprKind::ForExpr(_) => true, + ExprKind::WhileExpr(_) => true, + ExprKind::ContinueExpr(_) => true, + ExprKind::BreakExpr(_) => true, + ExprKind::Label(_) => true, + ExprKind::ReturnExpr(_) => true, + ExprKind::MatchExpr(_) => true, + ExprKind::StructLit(_) => true, + ExprKind::CastExpr(_) => true, + ExprKind::PrefixExpr(_) => true, + ExprKind::RangeExpr(_) => true, + ExprKind::BinExpr(_) => true, + ExprKind::CallExpr(_) => false, + ExprKind::IndexExpr(_) => false, + ExprKind::MethodCallExpr(_) => false, + ExprKind::FieldExpr(_) => false, + ExprKind::TryExpr(_) => false, + ExprKind::RefExpr(_) => false, + ExprKind::Literal(_) => false, + ExprKind::TupleExpr(_) => false, + ExprKind::ArrayExpr(_) => false, + ExprKind::ParenExpr(_) => false, + ExprKind::PathExpr(_) => false, + ExprKind::BlockExpr(_) => false, + }; + + let delete_range = if let Some(whitespace) = + let_stmt.syntax().next_sibling().and_then(ast::Whitespace::cast) + { + TextRange::from_to(let_stmt.syntax().range().start(), whitespace.syntax().range().end()) + } else { + let_stmt.syntax().range() + }; + + let init_str = if wrap_in_parens { + format!("({})", initializer.syntax().text().to_string()) + } else { + initializer.syntax().text().to_string() + }; + let function = function_from_child_node(ctx.db, ctx.frange.file_id, bind_pat.syntax())?; + let scope = function.scopes(ctx.db); + let refs = scope.find_all_refs(bind_pat); + + ctx.add_action( + AssistId("inline_local_variable"), + "inline local variable", + move |edit: &mut AssistBuilder| { + edit.delete(delete_range); + for desc in refs { + edit.replace(desc.range, init_str.clone()) + } + edit.set_cursor(delete_range.start()) + }, + ); + + ctx.build() +} + +#[cfg(test)] +mod tests { + use crate::helpers::{check_assist, check_assist_not_applicable}; + + use super::*; + + #[test] + fn test_inline_let_bind_literal_expr() { + check_assist( + inline_local_varialbe, + " +fn bar(a: usize) {} +fn foo() { + let a<|> = 1; + a + 1; + if a > 10 { + } + + while a > 10 { + + } + let b = a * 10; + bar(a); +}", + " +fn bar(a: usize) {} +fn foo() { + <|>1 + 1; + if 1 > 10 { + } + + while 1 > 10 { + + } + let b = 1 * 10; + bar(1); +}", + ); + } + + #[test] + fn test_inline_let_bind_bin_expr() { + check_assist( + inline_local_varialbe, + " +fn bar(a: usize) {} +fn foo() { + let a<|> = 1 + 1; + a + 1; + if a > 10 { + } + + while a > 10 { + + } + let b = a * 10; + bar(a); +}", + " +fn bar(a: usize) {} +fn foo() { + <|>(1 + 1) + 1; + if (1 + 1) > 10 { + } + + while (1 + 1) > 10 { + + } + let b = (1 + 1) * 10; + bar((1 + 1)); +}", + ); + } + + #[test] + fn test_inline_let_bind_function_call_expr() { + check_assist( + inline_local_varialbe, + " +fn bar(a: usize) {} +fn foo() { + let a<|> = bar(1); + a + 1; + if a > 10 { + } + + while a > 10 { + + } + let b = a * 10; + bar(a); +}", + " +fn bar(a: usize) {} +fn foo() { + <|>bar(1) + 1; + if bar(1) > 10 { + } + + while bar(1) > 10 { + + } + let b = bar(1) * 10; + bar(bar(1)); +}", + ); + } + + #[test] + fn test_inline_let_bind_cast_expr() { + check_assist( + inline_local_varialbe, + " +fn bar(a: usize): usize { a } +fn foo() { + let a<|> = bar(1) as u64; + a + 1; + if a > 10 { + } + + while a > 10 { + + } + let b = a * 10; + bar(a); +}", + " +fn bar(a: usize): usize { a } +fn foo() { + <|>(bar(1) as u64) + 1; + if (bar(1) as u64) > 10 { + } + + while (bar(1) as u64) > 10 { + + } + let b = (bar(1) as u64) * 10; + bar((bar(1) as u64)); +}", + ); + } + + #[test] + fn test_inline_let_bind_block_expr() { + check_assist( + inline_local_varialbe, + " +fn foo() { + let a<|> = { 10 + 1 }; + a + 1; + if a > 10 { + } + + while a > 10 { + + } + let b = a * 10; + bar(a); +}", + " +fn foo() { + <|>{ 10 + 1 } + 1; + if { 10 + 1 } > 10 { + } + + while { 10 + 1 } > 10 { + + } + let b = { 10 + 1 } * 10; + bar({ 10 + 1 }); +}", + ); + } + + #[test] + fn test_inline_let_bind_paren_expr() { + check_assist( + inline_local_varialbe, + " +fn foo() { + let a<|> = ( 10 + 1 ); + a + 1; + if a > 10 { + } + + while a > 10 { + + } + let b = a * 10; + bar(a); +}", + " +fn foo() { + <|>( 10 + 1 ) + 1; + if ( 10 + 1 ) > 10 { + } + + while ( 10 + 1 ) > 10 { + + } + let b = ( 10 + 1 ) * 10; + bar(( 10 + 1 )); +}", + ); + } + + #[test] + fn test_not_inline_mut_variable() { + check_assist_not_applicable( + inline_local_varialbe, + " +fn foo() { + let mut a<|> = 1 + 1; + a + 1; +}", + ); + } +} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index fc36e8cc97f..378470ac82e 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -92,6 +92,7 @@ mod change_visibility; mod fill_match_arms; mod fill_struct_fields; mod introduce_variable; +mod inline_local_variable; mod replace_if_let_with_match; mod split_import; mod remove_dbg; @@ -113,6 +114,7 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option Date: Mon, 25 Mar 2019 20:57:43 +0800 Subject: [PATCH 2/2] use | instead of multiple match arms --- .../ra_assists/src/inline_local_variable.rs | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index ce0aafb27f1..bd3cdb970c7 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -17,33 +17,33 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt } let initializer = let_stmt.initializer()?; let wrap_in_parens = match initializer.kind() { - ExprKind::LambdaExpr(_) => true, - ExprKind::IfExpr(_) => true, - ExprKind::LoopExpr(_) => true, - ExprKind::ForExpr(_) => true, - ExprKind::WhileExpr(_) => true, - ExprKind::ContinueExpr(_) => true, - ExprKind::BreakExpr(_) => true, - ExprKind::Label(_) => true, - ExprKind::ReturnExpr(_) => true, - ExprKind::MatchExpr(_) => true, - ExprKind::StructLit(_) => true, - ExprKind::CastExpr(_) => true, - ExprKind::PrefixExpr(_) => true, - ExprKind::RangeExpr(_) => true, - ExprKind::BinExpr(_) => true, - ExprKind::CallExpr(_) => false, - ExprKind::IndexExpr(_) => false, - ExprKind::MethodCallExpr(_) => false, - ExprKind::FieldExpr(_) => false, - ExprKind::TryExpr(_) => false, - ExprKind::RefExpr(_) => false, - ExprKind::Literal(_) => false, - ExprKind::TupleExpr(_) => false, - ExprKind::ArrayExpr(_) => false, - ExprKind::ParenExpr(_) => false, - ExprKind::PathExpr(_) => false, - ExprKind::BlockExpr(_) => false, + ExprKind::LambdaExpr(_) + | ExprKind::IfExpr(_) + | ExprKind::LoopExpr(_) + | ExprKind::ForExpr(_) + | ExprKind::WhileExpr(_) + | ExprKind::ContinueExpr(_) + | ExprKind::BreakExpr(_) + | ExprKind::Label(_) + | ExprKind::ReturnExpr(_) + | ExprKind::MatchExpr(_) + | ExprKind::StructLit(_) + | ExprKind::CastExpr(_) + | ExprKind::PrefixExpr(_) + | ExprKind::RangeExpr(_) + | ExprKind::BinExpr(_) => true, + ExprKind::CallExpr(_) + | ExprKind::IndexExpr(_) + | ExprKind::MethodCallExpr(_) + | ExprKind::FieldExpr(_) + | ExprKind::TryExpr(_) + | ExprKind::RefExpr(_) + | ExprKind::Literal(_) + | ExprKind::TupleExpr(_) + | ExprKind::ArrayExpr(_) + | ExprKind::ParenExpr(_) + | ExprKind::PathExpr(_) + | ExprKind::BlockExpr(_) => false, }; let delete_range = if let Some(whitespace) =