rust/crates/ra_assists/src/handlers/extract_variable.rs

589 lines
12 KiB
Rust
Raw Normal View History

2019-01-03 06:08:32 -06:00
use ra_syntax::{
2020-04-09 06:00:09 -05:00
ast::{self, AstNode},
SyntaxKind::{
2019-09-02 13:41:50 -05:00
BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
},
2020-05-20 16:07:17 -05:00
SyntaxNode,
2019-01-03 06:08:32 -06:00
};
use stdx::format_to;
2020-05-20 05:59:20 -05:00
use test_utils::mark;
2019-01-03 06:08:32 -06:00
2020-06-28 17:36:05 -05:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-01-03 06:08:32 -06:00
2020-06-26 18:21:43 -05:00
// Assist: extract_variable
2019-10-26 11:58:18 -05:00
//
// Extracts subexpression into a variable.
//
// ```
// fn main() {
// <|>(1 + 2)<|> * 4;
// }
// ```
// ->
// ```
// fn main() {
2020-05-20 16:07:17 -05:00
// let $0var_name = (1 + 2);
2019-10-26 11:58:18 -05:00
// var_name * 4;
// }
// ```
2020-06-26 18:21:43 -05:00
pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
if ctx.frange.range.is_empty() {
return None;
}
2019-03-30 05:25:53 -05:00
let node = ctx.covering_element();
2019-02-24 05:22:25 -06:00
if node.kind() == COMMENT {
2020-06-26 18:21:43 -05:00
mark::hit!(extract_var_in_comment_is_not_applicable);
return None;
}
2020-07-21 11:10:03 -05:00
let to_extract = node.ancestors().find_map(valid_target_expr)?;
let anchor = Anchor::from(&to_extract)?;
let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone();
let target = to_extract.syntax().text_range();
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-02 16:48:35 -05:00
AssistId("extract_variable", AssistKind::RefactorExtract),
2020-06-28 17:36:05 -05:00
"Extract into variable",
target,
move |edit| {
2020-07-21 11:10:03 -05:00
let field_shorthand =
2020-07-30 09:21:30 -05:00
match to_extract.syntax().parent().and_then(ast::RecordExprField::cast) {
2020-07-21 11:10:03 -05:00
Some(field) => field.name_ref(),
None => None,
};
2020-06-28 17:36:05 -05:00
let mut buf = String::new();
2019-01-03 06:08:32 -06:00
2020-06-28 17:36:05 -05:00
let var_name = match &field_shorthand {
Some(it) => it.to_string(),
None => "var_name".to_string(),
};
let expr_range = match &field_shorthand {
2020-07-21 11:10:03 -05:00
Some(it) => it.syntax().text_range().cover(to_extract.syntax().text_range()),
None => to_extract.syntax().text_range(),
2020-06-28 17:36:05 -05:00
};
2020-07-21 11:10:03 -05:00
if let Anchor::WrapInBlock(_) = anchor {
2020-06-28 17:36:05 -05:00
format_to!(buf, "{{ let {} = ", var_name);
} else {
format_to!(buf, "let {} = ", var_name);
};
2020-07-21 11:10:03 -05:00
format_to!(buf, "{}", to_extract.syntax());
2020-05-20 16:07:17 -05:00
2020-07-21 11:10:03 -05:00
if let Anchor::Replace(stmt) = anchor {
2020-06-28 17:36:05 -05:00
mark::hit!(test_extract_var_expr_stmt);
2020-07-21 11:10:03 -05:00
if stmt.semicolon_token().is_none() {
2020-06-28 17:36:05 -05:00
buf.push_str(";");
}
match ctx.config.snippet_cap {
Some(cap) => {
let snip = buf
.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
edit.replace_snippet(cap, expr_range, snip)
}
None => edit.replace(expr_range, buf),
}
return;
}
buf.push_str(";");
// We want to maintain the indent level,
// but we do not want to duplicate possible
// extra newlines in the indent block
let text = indent.text();
if text.starts_with('\n') {
buf.push_str("\n");
buf.push_str(text.trim_start_matches('\n'));
} else {
buf.push_str(text);
}
2020-06-28 17:36:05 -05:00
edit.replace(expr_range, var_name.clone());
2020-07-21 11:10:03 -05:00
let offset = anchor.syntax().text_range().start();
2020-05-20 16:07:17 -05:00
match ctx.config.snippet_cap {
Some(cap) => {
let snip =
buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
2020-06-28 17:36:05 -05:00
edit.insert_snippet(cap, offset, snip)
2020-05-20 16:07:17 -05:00
}
2020-06-28 17:36:05 -05:00
None => edit.insert(offset, buf),
}
2020-05-20 16:07:17 -05:00
2020-07-21 11:10:03 -05:00
if let Anchor::WrapInBlock(_) = anchor {
edit.insert(anchor.syntax().text_range().end(), " }");
2019-01-28 08:12:07 -06:00
}
2020-06-28 17:36:05 -05:00
},
)
2019-01-03 09:59:17 -06:00
}
2019-01-03 06:08:32 -06:00
2019-02-08 17:34:05 -06:00
/// Check whether the node is a valid expression which can be extracted to a variable.
/// In general that's true for any expression, but in some cases that would produce invalid code.
2019-07-19 03:24:41 -05:00
fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
2019-02-06 14:50:26 -06:00
match node.kind() {
2019-09-02 13:41:50 -05:00
PATH_EXPR | LOOP_EXPR => None,
BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()),
RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
2019-09-02 13:41:50 -05:00
BLOCK_EXPR => {
ast::BlockExpr::cast(node).filter(|it| it.is_standalone()).map(ast::Expr::from)
}
_ => ast::Expr::cast(node),
2019-02-06 14:50:26 -06:00
}
}
2020-07-21 11:10:03 -05:00
enum Anchor {
Before(SyntaxNode),
Replace(ast::ExprStmt),
WrapInBlock(SyntaxNode),
}
impl Anchor {
fn from(to_extract: &ast::Expr) -> Option<Anchor> {
to_extract.syntax().ancestors().find_map(|node| {
if let Some(expr) =
node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr())
{
if expr.syntax() == &node {
mark::hit!(test_extract_var_last_expr);
return Some(Anchor::Before(node));
}
2019-01-03 06:08:32 -06:00
}
2019-01-28 08:12:07 -06:00
2020-07-21 11:10:03 -05:00
if let Some(parent) = node.parent() {
if parent.kind() == MATCH_ARM || parent.kind() == LAMBDA_EXPR {
return Some(Anchor::WrapInBlock(node));
}
2019-01-28 08:12:07 -06:00
}
2020-07-21 11:10:03 -05:00
if let Some(stmt) = ast::Stmt::cast(node.clone()) {
if let ast::Stmt::ExprStmt(stmt) = stmt {
if stmt.expr().as_ref() == Some(to_extract) {
return Some(Anchor::Replace(stmt));
}
}
return Some(Anchor::Before(node));
}
None
})
}
2019-07-19 03:24:41 -05:00
2020-07-21 11:10:03 -05:00
fn syntax(&self) -> &SyntaxNode {
match self {
Anchor::Before(it) | Anchor::WrapInBlock(it) => it,
Anchor::Replace(stmt) => stmt.syntax(),
}
}
2019-01-03 06:08:32 -06:00
}
#[cfg(test)]
mod tests {
2020-05-20 05:59:20 -05:00
use test_utils::mark;
2019-02-24 05:22:25 -06:00
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
2019-01-03 06:08:32 -06:00
2019-02-24 05:22:25 -06:00
use super::*;
2019-01-03 06:08:32 -06:00
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_simple() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2020-05-20 16:07:17 -05:00
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
foo(<|>1 + 1<|>);
2020-05-20 16:07:17 -05:00
}"#,
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
2020-05-20 16:07:17 -05:00
let $0var_name = 1 + 1;
2019-01-03 06:08:32 -06:00
foo(var_name);
2020-05-20 16:07:17 -05:00
}"#,
2019-01-03 06:08:32 -06:00
);
}
2019-02-24 05:22:25 -06:00
#[test]
2020-06-26 18:21:43 -05:00
fn extract_var_in_comment_is_not_applicable() {
mark::check!(extract_var_in_comment_is_not_applicable);
check_assist_not_applicable(extract_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }");
2019-02-24 05:22:25 -06:00
}
2019-01-03 06:08:32 -06:00
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_expr_stmt() {
mark::check!(test_extract_var_expr_stmt);
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2020-05-20 16:07:17 -05:00
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
<|>1 + 1<|>;
2020-05-20 16:07:17 -05:00
}"#,
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
2020-05-20 16:07:17 -05:00
let $0var_name = 1 + 1;
}"#,
2019-02-24 05:22:25 -06:00
);
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-02-24 05:22:25 -06:00
"
fn foo() {
<|>{ let x = 0; x }<|>
something_else();
}",
"
fn foo() {
2020-05-20 16:07:17 -05:00
let $0var_name = { let x = 0; x };
2019-02-24 05:22:25 -06:00
something_else();
2019-01-03 06:08:32 -06:00
}",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_part_of_expr_stmt() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>1<|> + 1;
}",
"
fn foo() {
2020-05-20 16:07:17 -05:00
let $0var_name = 1;
2019-01-03 06:08:32 -06:00
var_name + 1;
}",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_last_expr() {
mark::check!(test_extract_var_last_expr);
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2020-06-23 17:30:34 -05:00
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
bar(<|>1 + 1<|>)
2020-06-23 17:30:34 -05:00
}
"#,
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
2020-05-20 16:07:17 -05:00
let $0var_name = 1 + 1;
2019-01-03 06:08:32 -06:00
bar(var_name)
2020-06-23 17:30:34 -05:00
}
"#,
2019-01-03 06:08:32 -06:00
);
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2020-06-23 17:30:34 -05:00
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
<|>bar(1 + 1)<|>
2020-06-23 17:30:34 -05:00
}
"#,
r#"
2019-01-03 06:08:32 -06:00
fn foo() {
2020-05-20 16:07:17 -05:00
let $0var_name = bar(1 + 1);
2019-01-03 06:08:32 -06:00
var_name
2020-06-23 17:30:34 -05:00
}
"#,
2019-02-24 05:22:25 -06:00
)
}
2019-01-28 08:12:07 -06:00
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_in_match_arm_no_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-01-28 08:12:07 -06:00
"
fn main() {
let x = true;
let tuple = match x {
true => (<|>2 + 2<|>, true)
_ => (0, false)
};
}
",
"
fn main() {
let x = true;
let tuple = match x {
2020-05-20 16:07:17 -05:00
true => { let $0var_name = 2 + 2; (var_name, true) }
2019-01-28 08:12:07 -06:00
_ => (0, false)
};
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_in_match_arm_with_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-01-28 08:12:07 -06:00
"
fn main() {
let x = true;
let tuple = match x {
true => {
let y = 1;
(<|>2 + y<|>, true)
}
_ => (0, false)
};
}
",
"
fn main() {
let x = true;
let tuple = match x {
true => {
let y = 1;
2020-05-20 16:07:17 -05:00
let $0var_name = 2 + y;
2019-01-28 08:12:07 -06:00
(var_name, true)
}
_ => (0, false)
};
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_in_closure_no_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-01-28 08:12:07 -06:00
"
fn main() {
let lambda = |x: u32| <|>x * 2<|>;
}
",
"
fn main() {
2020-05-20 16:07:17 -05:00
let lambda = |x: u32| { let $0var_name = x * 2; var_name };
2019-01-28 08:12:07 -06:00
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_in_closure_with_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-01-28 08:12:07 -06:00
"
fn main() {
let lambda = |x: u32| { <|>x * 2<|> };
}
",
"
fn main() {
2020-05-20 16:07:17 -05:00
let lambda = |x: u32| { let $0var_name = x * 2; var_name };
2019-01-28 08:12:07 -06:00
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_path_simple() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn main() {
let o = <|>Some(true)<|>;
}
",
"
fn main() {
2020-05-20 16:07:17 -05:00
let $0var_name = Some(true);
let o = var_name;
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_path_method() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn main() {
let v = <|>bar.foo()<|>;
}
",
"
fn main() {
2020-05-20 16:07:17 -05:00
let $0var_name = bar.foo();
let v = var_name;
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_return() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn foo() -> u32 {
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
2020-05-20 16:07:17 -05:00
let $0var_name = 2 + 2;
return var_name;
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_does_not_add_extra_whitespace() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn foo() -> u32 {
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
2020-05-20 16:07:17 -05:00
let $0var_name = 2 + 2;
return var_name;
}
",
);
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn foo() -> u32 {
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
2020-05-20 16:07:17 -05:00
let $0var_name = 2 + 2;
return var_name;
}
",
);
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn foo() -> u32 {
let foo = 1;
// bar
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
let foo = 1;
// bar
2020-05-20 16:07:17 -05:00
let $0var_name = 2 + 2;
return var_name;
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_break() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn main() {
let result = loop {
<|>break 2 + 2<|>;
};
}
",
"
fn main() {
let result = loop {
2020-05-20 16:07:17 -05:00
let $0var_name = 2 + 2;
break var_name;
};
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_for_cast() {
2020-02-25 11:57:47 -06:00
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
"
fn main() {
let v = <|>0f32 as u32<|>;
}
",
"
fn main() {
2020-05-20 16:07:17 -05:00
let $0var_name = 0f32 as u32;
let v = var_name;
}
",
);
}
#[test]
2020-06-26 18:21:43 -05:00
fn extract_var_field_shorthand() {
check_assist(
2020-06-26 18:21:43 -05:00
extract_variable,
r#"
struct S {
foo: i32
}
fn main() {
S { foo: <|>1 + 1<|> }
}
"#,
r#"
struct S {
foo: i32
}
fn main() {
let $0foo = 1 + 1;
S { foo }
}
"#,
)
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_for_return_not_applicable() {
check_assist_not_applicable(extract_variable, "fn foo() { <|>return<|>; } ");
}
#[test]
2020-06-26 18:21:43 -05:00
fn test_extract_var_for_break_not_applicable() {
check_assist_not_applicable(extract_variable, "fn main() { loop { <|>break<|>; }; }");
}
2019-02-08 17:34:05 -06:00
// FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic
#[test]
2020-06-26 18:21:43 -05:00
fn extract_var_target() {
check_assist_target(extract_variable, "fn foo() -> u32 { <|>return 2 + 2<|>; }", "2 + 2");
2019-02-08 17:34:05 -06:00
2020-02-25 11:57:47 -06:00
check_assist_target(
2020-06-26 18:21:43 -05:00
extract_variable,
2019-02-08 17:34:05 -06:00
"
fn main() {
let x = true;
let tuple = match x {
true => (<|>2 + 2<|>, true)
_ => (0, false)
};
}
",
"2 + 2",
);
}
2019-01-03 06:08:32 -06:00
}