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

518 lines
11 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,
WHITESPACE,
},
2020-04-24 16:40:41 -05:00
SyntaxNode, TextSize,
2019-01-03 06:08:32 -06:00
};
use stdx::format_to;
use test_utils::tested_by;
2019-01-03 06:08:32 -06:00
use crate::{Assist, AssistCtx, AssistId};
2019-01-03 06:08:32 -06:00
2019-10-26 11:58:18 -05:00
// Assist: introduce_variable
//
// Extracts subexpression into a variable.
//
// ```
// fn main() {
// <|>(1 + 2)<|> * 4;
// }
// ```
// ->
// ```
// fn main() {
// let var_name = (1 + 2);
// var_name * 4;
// }
// ```
2020-02-06 09:58:57 -06:00
pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> {
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 {
tested_by!(introduce_var_in_comment_is_not_applicable);
return None;
}
2019-02-24 05:22:25 -06:00
let expr = node.ancestors().find_map(valid_target_expr)?;
2019-07-19 03:24:41 -05:00
let (anchor_stmt, wrap_in_block) = anchor_stmt(expr.clone())?;
let indent = anchor_stmt.prev_sibling_or_token()?.as_token()?.clone();
2019-01-03 06:08:32 -06:00
if indent.kind() != WHITESPACE {
return None;
}
2020-01-14 11:32:26 -06:00
ctx.add_assist(AssistId("introduce_variable"), "Extract into variable", move |edit| {
2019-01-03 06:08:32 -06:00
let mut buf = String::new();
2019-01-28 08:12:07 -06:00
let cursor_offset = if wrap_in_block {
buf.push_str("{ let var_name = ");
2020-04-24 16:40:41 -05:00
TextSize::of("{ let ")
2019-01-28 08:12:07 -06:00
} else {
buf.push_str("let var_name = ");
2020-04-24 16:40:41 -05:00
TextSize::of("let ")
2019-01-28 08:12:07 -06:00
};
format_to!(buf, "{}", expr.syntax());
2019-07-19 03:24:41 -05:00
let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone());
let is_full_stmt = if let Some(expr_stmt) = &full_stmt {
Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone())
2019-01-03 06:08:32 -06:00
} else {
false
};
if is_full_stmt {
2019-02-24 05:22:25 -06:00
tested_by!(test_introduce_var_expr_stmt);
2020-04-10 03:11:05 -05:00
if full_stmt.unwrap().semicolon_token().is_none() {
2019-01-15 22:27:15 -06:00
buf.push_str(";");
}
2019-07-20 04:58:27 -05:00
edit.replace(expr.syntax().text_range(), buf);
2019-01-03 06:08:32 -06:00
} else {
buf.push_str(";");
// We want to maintain the indent level,
// but we do not want to duplicate possible
// extra newlines in the indent block
2019-03-30 05:25:53 -05:00
let text = indent.text();
if text.starts_with('\n') {
2019-03-30 05:25:53 -05:00
buf.push_str("\n");
2019-06-04 01:38:13 -05:00
buf.push_str(text.trim_start_matches('\n'));
2019-03-30 05:25:53 -05:00
} else {
buf.push_str(text);
}
2019-07-20 04:58:27 -05:00
edit.target(expr.syntax().text_range());
edit.replace(expr.syntax().text_range(), "var_name".to_string());
edit.insert(anchor_stmt.text_range().start(), buf);
2019-01-28 08:12:07 -06:00
if wrap_in_block {
2019-07-20 04:58:27 -05:00
edit.insert(anchor_stmt.text_range().end(), " }");
2019-01-28 08:12:07 -06:00
}
2019-01-03 06:08:32 -06:00
}
2019-07-20 04:58:27 -05:00
edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset);
})
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
}
}
2019-01-28 08:12:07 -06:00
/// Returns the syntax node which will follow the freshly introduced var
/// and a boolean indicating whether we have to wrap it within a { } block
/// to produce correct code.
/// It can be a statement, the last in a block expression or a wanna be block
2019-02-08 17:34:05 -06:00
/// expression like a lambda or match arm.
2019-07-19 03:24:41 -05:00
fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> {
2019-01-28 08:12:07 -06:00
expr.syntax().ancestors().find_map(|node| {
2020-05-01 18:18:19 -05:00
if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) {
2019-07-19 03:24:41 -05:00
if expr.syntax() == &node {
2019-02-24 05:22:25 -06:00
tested_by!(test_introduce_var_last_expr);
2019-01-28 08:12:07 -06:00
return Some((node, false));
2019-01-03 06:08:32 -06:00
}
2019-01-03 09:59:17 -06:00
}
2019-01-28 08:12:07 -06:00
if let Some(parent) = node.parent() {
if parent.kind() == MATCH_ARM || parent.kind() == LAMBDA_EXPR {
2019-01-28 08:12:07 -06:00
return Some((node, true));
}
}
2020-04-19 14:15:49 -05:00
if ast::Stmt::cast(node.clone()).is_some() {
2019-07-19 03:24:41 -05:00
return Some((node, false));
}
2019-01-28 08:12:07 -06:00
None
2019-01-03 09:59:17 -06:00
})
2019-01-03 06:08:32 -06:00
}
#[cfg(test)]
mod tests {
2019-02-24 05:22:25 -06:00
use test_utils::covers;
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]
fn test_introduce_var_simple() {
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-03 09:59:17 -06:00
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
foo(<|>1 + 1<|>);
}",
"
fn foo() {
let <|>var_name = 1 + 1;
foo(var_name);
}",
);
}
2019-02-24 05:22:25 -06:00
#[test]
fn introduce_var_in_comment_is_not_applicable() {
covers!(introduce_var_in_comment_is_not_applicable);
2020-02-25 11:57:47 -06:00
check_assist_not_applicable(introduce_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }");
2019-02-24 05:22:25 -06:00
}
2019-01-03 06:08:32 -06:00
#[test]
fn test_introduce_var_expr_stmt() {
2019-02-24 05:22:25 -06:00
covers!(test_introduce_var_expr_stmt);
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-03 09:59:17 -06:00
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>1 + 1<|>;
}",
"
fn foo() {
let <|>var_name = 1 + 1;
2019-02-24 05:22:25 -06:00
}",
);
2020-02-25 11:57:47 -06:00
check_assist(
2019-02-24 05:22:25 -06:00
introduce_variable,
"
fn foo() {
<|>{ let x = 0; x }<|>
something_else();
}",
"
fn foo() {
let <|>var_name = { let x = 0; x };
something_else();
2019-01-03 06:08:32 -06:00
}",
);
}
#[test]
fn test_introduce_var_part_of_expr_stmt() {
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-03 09:59:17 -06:00
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>1<|> + 1;
}",
"
fn foo() {
let <|>var_name = 1;
var_name + 1;
}",
);
}
#[test]
fn test_introduce_var_last_expr() {
2019-02-24 05:22:25 -06:00
covers!(test_introduce_var_last_expr);
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-03 09:59:17 -06:00
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
bar(<|>1 + 1<|>)
}",
"
fn foo() {
let <|>var_name = 1 + 1;
bar(var_name)
}",
);
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-03 09:59:17 -06:00
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>bar(1 + 1)<|>
}",
"
fn foo() {
let <|>var_name = bar(1 + 1);
var_name
}",
2019-02-24 05:22:25 -06:00
)
}
2019-01-28 08:12:07 -06:00
#[test]
fn test_introduce_var_in_match_arm_no_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-28 08:12:07 -06:00
introduce_variable,
"
fn main() {
let x = true;
let tuple = match x {
true => (<|>2 + 2<|>, true)
_ => (0, false)
};
}
",
"
fn main() {
let x = true;
let tuple = match x {
true => { let <|>var_name = 2 + 2; (var_name, true) }
_ => (0, false)
};
}
",
);
}
#[test]
fn test_introduce_var_in_match_arm_with_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-28 08:12:07 -06:00
introduce_variable,
"
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;
let <|>var_name = 2 + y;
(var_name, true)
}
_ => (0, false)
};
}
",
);
}
#[test]
fn test_introduce_var_in_closure_no_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-28 08:12:07 -06:00
introduce_variable,
"
fn main() {
let lambda = |x: u32| <|>x * 2<|>;
}
",
"
fn main() {
let lambda = |x: u32| { let <|>var_name = x * 2; var_name };
}
",
);
}
#[test]
fn test_introduce_var_in_closure_with_block() {
2020-02-25 11:57:47 -06:00
check_assist(
2019-01-28 08:12:07 -06:00
introduce_variable,
"
fn main() {
let lambda = |x: u32| { <|>x * 2<|> };
}
",
"
fn main() {
let lambda = |x: u32| { let <|>var_name = x * 2; var_name };
}
",
);
}
#[test]
fn test_introduce_var_path_simple() {
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn main() {
let o = <|>Some(true)<|>;
}
",
"
fn main() {
let <|>var_name = Some(true);
let o = var_name;
}
",
);
}
#[test]
fn test_introduce_var_path_method() {
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn main() {
let v = <|>bar.foo()<|>;
}
",
"
fn main() {
let <|>var_name = bar.foo();
let v = var_name;
}
",
);
}
#[test]
fn test_introduce_var_return() {
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn foo() -> u32 {
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
let <|>var_name = 2 + 2;
return var_name;
}
",
);
}
#[test]
fn test_introduce_var_does_not_add_extra_whitespace() {
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn foo() -> u32 {
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
let <|>var_name = 2 + 2;
return var_name;
}
",
);
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn foo() -> u32 {
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
let <|>var_name = 2 + 2;
return var_name;
}
",
);
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn foo() -> u32 {
let foo = 1;
// bar
<|>return 2 + 2<|>;
}
",
"
fn foo() -> u32 {
let foo = 1;
// bar
let <|>var_name = 2 + 2;
return var_name;
}
",
);
}
#[test]
fn test_introduce_var_break() {
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn main() {
let result = loop {
<|>break 2 + 2<|>;
};
}
",
"
fn main() {
let result = loop {
let <|>var_name = 2 + 2;
break var_name;
};
}
",
);
}
#[test]
fn test_introduce_var_for_cast() {
2020-02-25 11:57:47 -06:00
check_assist(
introduce_variable,
"
fn main() {
let v = <|>0f32 as u32<|>;
}
",
"
fn main() {
let <|>var_name = 0f32 as u32;
let v = var_name;
}
",
);
}
#[test]
fn test_introduce_var_for_return_not_applicable() {
2020-02-25 11:57:47 -06:00
check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");
}
#[test]
fn test_introduce_var_for_break_not_applicable() {
2020-02-25 11:57:47 -06:00
check_assist_not_applicable(introduce_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]
fn introduce_var_target() {
2020-02-25 11:57:47 -06:00
check_assist_target(introduce_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(
2019-02-08 17:34:05 -06:00
introduce_variable,
"
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
}