2021-01-01 18:55:56 -06:00
|
|
|
use syntax::{
|
|
|
|
ast::{self, edit::AstNodeEdit, make},
|
|
|
|
AstNode,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
AssistId, AssistKind,
|
|
|
|
};
|
|
|
|
|
2021-01-04 07:38:34 -06:00
|
|
|
// Assist: pull_assignment_up
|
2021-01-01 18:55:56 -06:00
|
|
|
//
|
2021-01-04 07:38:34 -06:00
|
|
|
// Extracts variable assignment to outside an if or match statement.
|
2021-01-01 18:55:56 -06:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// let mut foo = 6;
|
|
|
|
//
|
|
|
|
// if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
// $0foo = 5;
|
2021-01-01 18:55:56 -06:00
|
|
|
// } else {
|
|
|
|
// foo = 4;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// let mut foo = 6;
|
|
|
|
//
|
|
|
|
// foo = if true {
|
|
|
|
// 5
|
|
|
|
// } else {
|
|
|
|
// 4
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
// ```
|
2021-01-04 07:38:34 -06:00
|
|
|
pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-01-03 07:11:42 -06:00
|
|
|
let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
|
2021-05-08 12:40:07 -05:00
|
|
|
|
|
|
|
let op_kind = assign_expr.op_kind()?;
|
|
|
|
if op_kind != ast::BinOp::Assignment {
|
|
|
|
cov_mark::hit!(test_cant_pull_non_assignments);
|
2021-01-03 07:11:42 -06:00
|
|
|
return None;
|
2021-05-08 12:40:07 -05:00
|
|
|
}
|
2021-01-01 18:55:56 -06:00
|
|
|
|
2021-05-08 12:40:07 -05:00
|
|
|
let name_expr = assign_expr.lhs()?;
|
|
|
|
|
|
|
|
let old_stmt: ast::Expr;
|
|
|
|
let new_stmt: ast::Expr;
|
|
|
|
|
|
|
|
if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
|
|
|
|
new_stmt = exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level());
|
|
|
|
old_stmt = if_expr.into();
|
2021-01-02 08:33:23 -06:00
|
|
|
} else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
|
2021-05-08 12:40:07 -05:00
|
|
|
new_stmt = exprify_match(&match_expr, &ctx.sema, &name_expr)?;
|
|
|
|
old_stmt = match_expr.into()
|
2021-01-02 08:33:23 -06:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
2021-01-01 18:55:56 -06:00
|
|
|
|
|
|
|
let expr_stmt = make::expr_stmt(new_stmt);
|
|
|
|
|
|
|
|
acc.add(
|
2021-01-04 07:38:34 -06:00
|
|
|
AssistId("pull_assignment_up", AssistKind::RefactorExtract),
|
|
|
|
"Pull assignment up",
|
2021-01-02 08:33:23 -06:00
|
|
|
old_stmt.syntax().text_range(),
|
2021-01-01 18:55:56 -06:00
|
|
|
move |edit| {
|
2021-01-03 07:11:42 -06:00
|
|
|
edit.replace(old_stmt.syntax().text_range(), format!("{} = {};", name_expr, expr_stmt));
|
2021-01-01 18:55:56 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:11:42 -06:00
|
|
|
fn exprify_match(
|
|
|
|
match_expr: &ast::MatchExpr,
|
|
|
|
sema: &hir::Semantics<ide_db::RootDatabase>,
|
|
|
|
name: &ast::Expr,
|
|
|
|
) -> Option<ast::Expr> {
|
2021-01-02 08:33:23 -06:00
|
|
|
let new_arm_list = match_expr
|
|
|
|
.match_arm_list()?
|
|
|
|
.arms()
|
|
|
|
.map(|arm| {
|
|
|
|
if let ast::Expr::BlockExpr(block) = arm.expr()? {
|
2021-01-03 07:11:42 -06:00
|
|
|
let new_block = exprify_block(&block, sema, name)?.indent(block.indent_level());
|
2021-01-02 08:33:23 -06:00
|
|
|
Some(arm.replace_descendant(block, new_block))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>()?;
|
|
|
|
let new_arm_list = match_expr
|
|
|
|
.match_arm_list()?
|
|
|
|
.replace_descendants(match_expr.match_arm_list()?.arms().zip(new_arm_list));
|
|
|
|
Some(make::expr_match(match_expr.expr()?, new_arm_list))
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:11:42 -06:00
|
|
|
fn exprify_if(
|
|
|
|
statement: &ast::IfExpr,
|
|
|
|
sema: &hir::Semantics<ide_db::RootDatabase>,
|
|
|
|
name: &ast::Expr,
|
|
|
|
) -> Option<ast::Expr> {
|
|
|
|
let then_branch = exprify_block(&statement.then_branch()?, sema, name)?;
|
2021-01-01 18:55:56 -06:00
|
|
|
let else_branch = match statement.else_branch()? {
|
2021-05-08 12:40:07 -05:00
|
|
|
ast::ElseBranch::Block(block) => ast::ElseBranch::Block(exprify_block(&block, sema, name)?),
|
2021-01-01 18:55:56 -06:00
|
|
|
ast::ElseBranch::IfExpr(expr) => {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_pull_assignment_up_chained_if);
|
2021-01-01 18:55:56 -06:00
|
|
|
ast::ElseBranch::IfExpr(ast::IfExpr::cast(
|
2021-01-03 07:11:42 -06:00
|
|
|
exprify_if(&expr, sema, name)?.syntax().to_owned(),
|
2021-01-01 18:55:56 -06:00
|
|
|
)?)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(make::expr_if(statement.condition()?, then_branch, Some(else_branch)))
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:11:42 -06:00
|
|
|
fn exprify_block(
|
|
|
|
block: &ast::BlockExpr,
|
|
|
|
sema: &hir::Semantics<ide_db::RootDatabase>,
|
|
|
|
name: &ast::Expr,
|
|
|
|
) -> Option<ast::BlockExpr> {
|
2021-01-05 06:45:46 -06:00
|
|
|
if block.tail_expr().is_some() {
|
2021-01-01 18:55:56 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut stmts: Vec<_> = block.statements().collect();
|
|
|
|
let stmt = stmts.pop()?;
|
|
|
|
|
|
|
|
if let ast::Stmt::ExprStmt(stmt) = stmt {
|
|
|
|
if let ast::Expr::BinExpr(expr) = stmt.expr()? {
|
2021-01-03 07:11:42 -06:00
|
|
|
if expr.op_kind()? == ast::BinOp::Assignment && is_equivalent(sema, &expr.lhs()?, name)
|
2021-01-01 18:55:56 -06:00
|
|
|
{
|
|
|
|
// The last statement in the block is an assignment to the name we want
|
|
|
|
return Some(make::block_expr(stmts, Some(expr.rhs()?)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-01-03 07:11:42 -06:00
|
|
|
fn is_equivalent(
|
|
|
|
sema: &hir::Semantics<ide_db::RootDatabase>,
|
|
|
|
expr0: &ast::Expr,
|
|
|
|
expr1: &ast::Expr,
|
|
|
|
) -> bool {
|
|
|
|
match (expr0, expr1) {
|
|
|
|
(ast::Expr::FieldExpr(field_expr0), ast::Expr::FieldExpr(field_expr1)) => {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_pull_assignment_up_field_assignment);
|
2021-01-03 07:11:42 -06:00
|
|
|
sema.resolve_field(field_expr0) == sema.resolve_field(field_expr1)
|
|
|
|
}
|
|
|
|
(ast::Expr::PathExpr(path0), ast::Expr::PathExpr(path1)) => {
|
|
|
|
let path0 = path0.path();
|
|
|
|
let path1 = path1.path();
|
|
|
|
if let (Some(path0), Some(path1)) = (path0, path1) {
|
|
|
|
sema.resolve_path(&path0) == sema.resolve_path(&path1)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 07:43:52 -06:00
|
|
|
(ast::Expr::PrefixExpr(prefix0), ast::Expr::PrefixExpr(prefix1))
|
|
|
|
if prefix0.op_kind() == Some(ast::PrefixOp::Deref)
|
|
|
|
&& prefix1.op_kind() == Some(ast::PrefixOp::Deref) =>
|
|
|
|
{
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_pull_assignment_up_deref);
|
2021-03-04 07:43:52 -06:00
|
|
|
if let (Some(prefix0), Some(prefix1)) = (prefix0.expr(), prefix1.expr()) {
|
|
|
|
is_equivalent(sema, &prefix0, &prefix1)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 07:11:42 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 18:55:56 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_if() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = if true {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-02 08:33:23 -06:00
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_match() {
|
2021-01-02 08:33:23 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-02 08:33:23 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
match 1 {
|
|
|
|
1 => {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-02 08:33:23 -06:00
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
a = 3;
|
|
|
|
},
|
|
|
|
3 => {
|
|
|
|
a = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = match 1 {
|
|
|
|
1 => {
|
|
|
|
2
|
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
3
|
|
|
|
},
|
|
|
|
3 => {
|
|
|
|
4
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-01 18:55:56 -06:00
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_not_last_not_applicable() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
b = a;
|
|
|
|
} else {
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_chained_if() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_pull_assignment_up_chained_if);
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else if false {
|
|
|
|
a = 3;
|
|
|
|
} else {
|
|
|
|
a = 4;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = if true {
|
|
|
|
2
|
|
|
|
} else if false {
|
|
|
|
3
|
|
|
|
} else {
|
|
|
|
4
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_retains_stmts() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
|
|
|
let b = 2;
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {
|
|
|
|
let b = 3;
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = if true {
|
|
|
|
let b = 2;
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
let b = 3;
|
|
|
|
3
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn pull_assignment_up_let_stmt_not_applicable() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
let b = if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {
|
|
|
|
a = 3
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn pull_assignment_up_if_missing_assigment_not_applicable() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {}
|
2021-01-02 08:33:23 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn pull_assignment_up_match_missing_assigment_not_applicable() {
|
2021-01-02 08:33:23 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-02 08:33:23 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
match 1 {
|
|
|
|
1 => {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-02 08:33:23 -06:00
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
a = 3;
|
|
|
|
},
|
|
|
|
3 => {},
|
|
|
|
}
|
2021-01-03 07:11:42 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_field_assignment() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_pull_assignment_up_field_assignment);
|
2021-01-03 07:11:42 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-03 07:11:42 -06:00
|
|
|
r#"
|
|
|
|
struct A(usize);
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let mut a = A(1);
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a.0 = 2;
|
2021-01-03 07:11:42 -06:00
|
|
|
} else {
|
|
|
|
a.0 = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct A(usize);
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let mut a = A(1);
|
|
|
|
|
|
|
|
a.0 = if true {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
2021-01-01 18:55:56 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2021-03-04 07:43:52 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pull_assignment_up_deref() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_pull_assignment_up_deref);
|
2021-03-04 07:43:52 -06:00
|
|
|
check_assist(
|
|
|
|
pull_assignment_up,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
let b = &mut a;
|
|
|
|
|
|
|
|
if true {
|
|
|
|
$0*b = 2;
|
|
|
|
} else {
|
|
|
|
*b = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
let b = &mut a;
|
|
|
|
|
|
|
|
*b = if true {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
|
|
|
}
|
2021-05-08 12:40:07 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cant_pull_non_assignments() {
|
|
|
|
cov_mark::check!(test_cant_pull_non_assignments);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
pull_assignment_up,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
let b = &mut a;
|
|
|
|
|
|
|
|
if true {
|
|
|
|
$0*b + 2;
|
|
|
|
} else {
|
|
|
|
*b + 3;
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 07:43:52 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|