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..bd3cdb970c7 --- /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(_) + | 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) = + 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 8900fbc4bef..2e47b5215cf 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -93,6 +93,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; @@ -115,6 +116,7 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option