2021-02-16 14:48:15 -06:00
|
|
|
use stdx::format_to;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-02-16 14:48:15 -06:00
|
|
|
ast::{self, AstNode},
|
2021-10-03 04:09:49 -05:00
|
|
|
NodeOrToken,
|
2019-07-04 15:05:17 -05:00
|
|
|
SyntaxKind::{
|
2021-04-05 06:36:27 -05:00
|
|
|
BLOCK_EXPR, BREAK_EXPR, CLOSURE_EXPR, COMMENT, LOOP_EXPR, MATCH_ARM, MATCH_GUARD,
|
|
|
|
PATH_EXPR, RETURN_EXPR,
|
2019-07-04 15:05:17 -05:00
|
|
|
},
|
2020-05-20 16:07:17 -05:00
|
|
|
SyntaxNode,
|
2019-01-03 06:08:32 -06:00
|
|
|
};
|
|
|
|
|
2021-02-16 14:48:15 -06:00
|
|
|
use crate::{utils::suggest_name, 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() {
|
2021-01-06 14:15:48 -06:00
|
|
|
// $0(1 + 2)$0 * 4;
|
2019-10-26 11:58:18 -05:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// 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<()> {
|
2021-10-13 07:39:37 -05:00
|
|
|
if ctx.has_empty_selection() {
|
2019-02-24 05:18:10 -06:00
|
|
|
return None;
|
|
|
|
}
|
2021-10-12 07:41:59 -05:00
|
|
|
|
2021-10-03 04:09:49 -05:00
|
|
|
let node = match ctx.covering_element() {
|
|
|
|
NodeOrToken::Node(it) => it,
|
|
|
|
NodeOrToken::Token(it) if it.kind() == COMMENT => {
|
|
|
|
cov_mark::hit!(extract_var_in_comment_is_not_applicable);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
NodeOrToken::Token(it) => it.parent()?,
|
|
|
|
};
|
|
|
|
let node = node.ancestors().take_while(|anc| anc.text_range() == node.text_range()).last()?;
|
2021-07-21 10:52:17 -05:00
|
|
|
let to_extract = node
|
2021-10-03 04:09:49 -05:00
|
|
|
.descendants()
|
2021-10-13 07:39:37 -05:00
|
|
|
.take_while(|it| ctx.selection_trimmed().contains_range(it.text_range()))
|
2021-07-21 10:52:17 -05:00
|
|
|
.find_map(valid_target_expr)?;
|
2021-10-03 04:09:49 -05:00
|
|
|
|
2021-08-02 13:42:25 -05:00
|
|
|
if let Some(ty_info) = ctx.sema.type_of_expr(&to_extract) {
|
2021-08-03 10:28:51 -05:00
|
|
|
if ty_info.adjusted().is_unit() {
|
2021-06-02 01:49:31 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2021-10-03 04:09:49 -05:00
|
|
|
|
2022-01-05 15:16:24 -06:00
|
|
|
let reference_modifier = match get_receiver_type(&ctx, &to_extract) {
|
|
|
|
Some(receiver_type) if receiver_type.is_mutable_reference() => "&mut ",
|
|
|
|
Some(receiver_type) if receiver_type.is_reference() => "&",
|
|
|
|
_ => "",
|
2022-01-04 19:03:27 -06:00
|
|
|
};
|
|
|
|
|
2020-07-21 11:10:03 -05:00
|
|
|
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-25 17:51:34 -05:00
|
|
|
|
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(),
|
2021-05-09 09:59:52 -05:00
|
|
|
None => suggest_name::for_variable(&to_extract, &ctx.sema),
|
2020-06-28 17:36:05 -05:00
|
|
|
};
|
|
|
|
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-06-25 17:51:34 -05:00
|
|
|
|
2021-10-03 07:53:01 -05:00
|
|
|
match anchor {
|
|
|
|
Anchor::Before(_) | Anchor::Replace(_) => {
|
2022-01-04 19:03:27 -06:00
|
|
|
format_to!(buf, "let {} = {}", var_name, reference_modifier)
|
|
|
|
}
|
|
|
|
Anchor::WrapInBlock(_) => {
|
|
|
|
format_to!(buf, "{{ let {} = {}", var_name, reference_modifier)
|
2021-10-03 07:53:01 -05:00
|
|
|
}
|
2020-06-28 17:36:05 -05:00
|
|
|
};
|
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 {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_extract_var_expr_stmt);
|
2020-07-21 11:10:03 -05:00
|
|
|
if stmt.semicolon_token().is_none() {
|
2021-09-13 11:50:19 -05:00
|
|
|
buf.push(';');
|
2020-06-28 17:36:05 -05:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-13 11:50:19 -05:00
|
|
|
buf.push(';');
|
2020-06-28 17:36:05 -05:00
|
|
|
|
|
|
|
// 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') {
|
2020-12-29 06:35:49 -06:00
|
|
|
buf.push('\n');
|
2020-06-28 17:36:05 -05:00
|
|
|
buf.push_str(text.trim_start_matches('\n'));
|
|
|
|
} else {
|
|
|
|
buf.push_str(text);
|
2019-01-13 20:22:53 -06:00
|
|
|
}
|
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) => {
|
2020-06-25 17:51:34 -05:00
|
|
|
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),
|
2019-02-09 05:41:03 -06:00
|
|
|
}
|
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.
|
2019-01-30 14:36:49 -06:00
|
|
|
/// 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,
|
2019-01-30 14:36:49 -06:00
|
|
|
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)
|
|
|
|
}
|
2019-01-30 14:36:49 -06:00
|
|
|
_ => ast::Expr::cast(node),
|
2019-02-06 14:50:26 -06:00
|
|
|
}
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
|
2022-01-04 19:03:27 -06:00
|
|
|
fn get_receiver_type(ctx: &AssistContext, expression: &ast::Expr) -> Option<hir::Type> {
|
2022-01-05 15:10:03 -06:00
|
|
|
let receiver = get_receiver(expression.clone())?;
|
2022-01-04 19:03:27 -06:00
|
|
|
Some(ctx.sema.type_of_expr(&receiver)?.original())
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:48:57 -06:00
|
|
|
/// In the expression `a.b.c.x()`, find `a`
|
2022-01-04 19:03:27 -06:00
|
|
|
fn get_receiver(expression: ast::Expr) -> Option<ast::Expr> {
|
|
|
|
match expression {
|
|
|
|
ast::Expr::FieldExpr(field) if field.expr().is_some() => {
|
|
|
|
let nested_expression = &field.expr()?;
|
|
|
|
get_receiver(nested_expression.to_owned())
|
|
|
|
}
|
2022-01-05 15:12:09 -06:00
|
|
|
_ => Some(expression),
|
2022-01-04 19:03:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 04:12:57 -05:00
|
|
|
#[derive(Debug)]
|
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> {
|
2021-10-03 04:09:49 -05:00
|
|
|
to_extract
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| !ast::Item::can_cast(it.kind()) || ast::MacroCall::can_cast(it.kind()))
|
|
|
|
.find_map(|node| {
|
2021-10-25 11:28:15 -05:00
|
|
|
if ast::MacroCall::can_cast(node.kind()) {
|
|
|
|
return None;
|
|
|
|
}
|
2021-07-21 10:52:17 -05:00
|
|
|
if let Some(expr) =
|
2021-09-26 04:12:57 -05:00
|
|
|
node.parent().and_then(ast::StmtList::cast).and_then(|it| it.tail_expr())
|
2021-07-21 10:52:17 -05:00
|
|
|
{
|
|
|
|
if expr.syntax() == &node {
|
|
|
|
cov_mark::hit!(test_extract_var_last_expr);
|
|
|
|
return Some(Anchor::Before(node));
|
|
|
|
}
|
2020-07-21 11:10:03 -05:00
|
|
|
}
|
2019-01-28 08:12:07 -06:00
|
|
|
|
2021-07-21 10:52:17 -05:00
|
|
|
if let Some(parent) = node.parent() {
|
|
|
|
if parent.kind() == CLOSURE_EXPR {
|
|
|
|
cov_mark::hit!(test_extract_var_in_closure_no_block);
|
2021-04-05 06:36:27 -05:00
|
|
|
return Some(Anchor::WrapInBlock(node));
|
|
|
|
}
|
2021-07-21 10:52:17 -05:00
|
|
|
if parent.kind() == MATCH_ARM {
|
|
|
|
if node.kind() == MATCH_GUARD {
|
|
|
|
cov_mark::hit!(test_extract_var_in_match_guard);
|
|
|
|
} else {
|
|
|
|
cov_mark::hit!(test_extract_var_in_match_arm_no_block);
|
|
|
|
return Some(Anchor::WrapInBlock(node));
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
}
|
2019-01-28 08:12:07 -06:00
|
|
|
|
2021-07-21 10:52:17 -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));
|
|
|
|
}
|
2020-07-21 11:10:03 -05:00
|
|
|
}
|
2021-07-21 10:52:17 -05:00
|
|
|
return Some(Anchor::Before(node));
|
2020-07-21 11:10:03 -05:00
|
|
|
}
|
2021-07-21 10:52:17 -05:00
|
|
|
None
|
2021-10-03 04:09:49 -05:00
|
|
|
})
|
2020-07-21 11:10:03 -05:00
|
|
|
}
|
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-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() {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo($01 + 1$0);
|
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() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(extract_var_in_comment_is_not_applicable);
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(extract_variable, "fn main() { 1 + /* $0comment$0 */ 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() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_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() {
|
2021-10-12 07:41:59 -05:00
|
|
|
$0 1 + 1$0;
|
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,
|
2021-10-12 07:41:59 -05:00
|
|
|
r"
|
2019-02-24 05:22:25 -06:00
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0{ let x = 0; x }$0
|
2019-02-24 05:22:25 -06:00
|
|
|
something_else();
|
|
|
|
}",
|
2021-10-12 07:41:59 -05:00
|
|
|
r"
|
2019-02-24 05:22:25 -06:00
|
|
|
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,
|
2021-10-12 07:41:59 -05:00
|
|
|
r"
|
2019-01-03 06:08:32 -06:00
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$01$0 + 1;
|
2019-01-03 06:08:32 -06:00
|
|
|
}",
|
2021-10-12 07:41:59 -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;
|
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() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_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() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar($01 + 1$0)
|
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#"
|
2021-06-02 01:49:31 -05:00
|
|
|
fn foo() -> i32 {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(1 + 1)$0
|
2020-06-23 17:30:34 -05:00
|
|
|
}
|
2021-06-02 01:49:31 -05:00
|
|
|
|
|
|
|
fn bar(i: i32) -> i32 {
|
|
|
|
i
|
|
|
|
}
|
2020-06-23 17:30:34 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2021-06-02 01:49:31 -05:00
|
|
|
fn foo() -> i32 {
|
2021-02-15 19:43:32 -06:00
|
|
|
let $0bar = bar(1 + 1);
|
|
|
|
bar
|
2020-06-23 17:30:34 -05:00
|
|
|
}
|
2021-06-02 01:49:31 -05:00
|
|
|
|
|
|
|
fn bar(i: i32) -> i32 {
|
|
|
|
i
|
|
|
|
}
|
2020-06-23 17:30:34 -05:00
|
|
|
"#,
|
2019-02-24 05:22:25 -06:00
|
|
|
)
|
2019-01-13 19:59:56 -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() {
|
2021-04-05 06:36:27 -05:00
|
|
|
cov_mark::check!(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,
|
2021-04-05 06:36:27 -05:00
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
fn main() {
|
|
|
|
let x = true;
|
|
|
|
let tuple = match x {
|
2021-01-06 14:15:48 -06:00
|
|
|
true => ($02 + 2$0, true)
|
2019-01-28 08:12:07 -06:00
|
|
|
_ => (0, false)
|
|
|
|
};
|
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|
2021-04-05 06:36:27 -05: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_with_block() {
|
2020-02-25 11:57:47 -06:00
|
|
|
check_assist(
|
2020-06-26 18:21:43 -05:00
|
|
|
extract_variable,
|
2021-04-05 06:36:27 -05:00
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
fn main() {
|
|
|
|
let x = true;
|
|
|
|
let tuple = match x {
|
|
|
|
true => {
|
|
|
|
let y = 1;
|
2021-01-06 14:15:48 -06:00
|
|
|
($02 + y$0, true)
|
2019-01-28 08:12:07 -06:00
|
|
|
}
|
|
|
|
_ => (0, false)
|
|
|
|
};
|
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_in_match_guard() {
|
|
|
|
cov_mark::check!(test_extract_var_in_match_guard);
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match () {
|
|
|
|
() if $010 > 0$0 => 1
|
|
|
|
_ => 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let $0var_name = 10 > 0;
|
|
|
|
match () {
|
|
|
|
() if var_name => 1
|
|
|
|
_ => 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-28 08:12:07 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-26 18:21:43 -05:00
|
|
|
fn test_extract_var_in_closure_no_block() {
|
2021-04-05 06:36:27 -05:00
|
|
|
cov_mark::check!(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,
|
2021-04-05 06:36:27 -05:00
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let lambda = |x: u32| $0x * 2$0;
|
2019-01-28 08:12:07 -06:00
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
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
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
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,
|
2021-04-05 06:36:27 -05:00
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let lambda = |x: u32| { $0x * 2$0 };
|
2019-01-28 08:12:07 -06:00
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2019-01-28 08:12:07 -06:00
|
|
|
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
|
|
|
}
|
2021-04-05 06:36:27 -05:00
|
|
|
"#,
|
2019-01-30 14:36:49 -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,
|
2019-01-30 14:36:49 -06:00
|
|
|
"
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let o = $0Some(true)$0;
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn main() {
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = Some(true);
|
2019-01-30 14:36:49 -06:00
|
|
|
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,
|
2019-01-30 14:36:49 -06:00
|
|
|
"
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let v = $0bar.foo()$0;
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn main() {
|
2021-02-15 19:43:32 -06:00
|
|
|
let $0foo = bar.foo();
|
|
|
|
let v = foo;
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
2019-01-30 14:36:49 -06:00
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0return 2 + 2$0;
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = 2 + 2;
|
2019-02-09 05:41:03 -06:00
|
|
|
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,
|
2019-02-09 05:41:03 -06:00
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
$0return 2 + 2$0;
|
2019-02-09 05:41:03 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
|
|
|
|
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = 2 + 2;
|
2019-02-09 05:41:03 -06:00
|
|
|
return var_name;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2020-02-25 11:57:47 -06:00
|
|
|
check_assist(
|
2020-06-26 18:21:43 -05:00
|
|
|
extract_variable,
|
2019-02-09 05:41:03 -06:00
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
$0return 2 + 2$0;
|
2019-02-09 05:41:03 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = 2 + 2;
|
2019-02-09 05:41:03 -06:00
|
|
|
return var_name;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2020-02-25 11:57:47 -06:00
|
|
|
check_assist(
|
2020-06-26 18:21:43 -05:00
|
|
|
extract_variable,
|
2019-02-09 05:41:03 -06:00
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
let foo = 1;
|
|
|
|
|
|
|
|
// bar
|
|
|
|
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
$0return 2 + 2$0;
|
2019-02-09 05:41:03 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn foo() -> u32 {
|
|
|
|
let foo = 1;
|
|
|
|
|
|
|
|
// bar
|
|
|
|
|
|
|
|
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = 2 + 2;
|
2019-01-30 14:36:49 -06:00
|
|
|
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,
|
2019-01-30 14:36:49 -06:00
|
|
|
"
|
|
|
|
fn main() {
|
|
|
|
let result = loop {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0break 2 + 2$0;
|
2019-01-30 14:36:49 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn main() {
|
|
|
|
let result = loop {
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = 2 + 2;
|
2019-01-30 14:36:49 -06:00
|
|
|
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,
|
2019-01-30 14:36:49 -06:00
|
|
|
"
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let v = $00f32 as u32$0;
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
fn main() {
|
2020-05-20 16:07:17 -05:00
|
|
|
let $0var_name = 0f32 as u32;
|
2019-01-30 14:36:49 -06:00
|
|
|
let v = var_name;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:51:34 -05:00
|
|
|
#[test]
|
2020-06-26 18:21:43 -05:00
|
|
|
fn extract_var_field_shorthand() {
|
2020-06-25 17:51:34 -05:00
|
|
|
check_assist(
|
2020-06-26 18:21:43 -05:00
|
|
|
extract_variable,
|
2020-06-25 17:51:34 -05:00
|
|
|
r#"
|
|
|
|
struct S {
|
|
|
|
foo: i32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
S { foo: $01 + 1$0 }
|
2020-06-25 17:51:34 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S {
|
|
|
|
foo: i32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let $0foo = 1 + 1;
|
|
|
|
S { foo }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:43:32 -06:00
|
|
|
#[test]
|
|
|
|
fn extract_var_name_from_type() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct Test(i32);
|
|
|
|
|
|
|
|
fn foo() -> Test {
|
|
|
|
$0{ Test(10) }$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Test(i32);
|
|
|
|
|
|
|
|
fn foo() -> Test {
|
|
|
|
let $0test = { Test(10) };
|
|
|
|
test
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_name_from_parameter() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
fn bar(test: u32, size: u32)
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar(1, $01+1$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn bar(test: u32, size: u32)
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let $0size = 1+1;
|
|
|
|
bar(1, size);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_parameter_name_has_precedence_over_type() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct TextSize(u32);
|
|
|
|
fn bar(test: u32, size: TextSize)
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar(1, $0{ TextSize(1+1) }$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct TextSize(u32);
|
|
|
|
fn bar(test: u32, size: TextSize)
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let $0size = { TextSize(1+1) };
|
|
|
|
bar(1, size);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_name_from_function() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
fn is_required(test: u32, size: u32) -> bool
|
|
|
|
|
|
|
|
fn foo() -> bool {
|
|
|
|
$0is_required(1, 2)$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn is_required(test: u32, size: u32) -> bool
|
|
|
|
|
|
|
|
fn foo() -> bool {
|
|
|
|
let $0is_required = is_required(1, 2);
|
|
|
|
is_required
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_name_from_method() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn bar(&self, n: u32) -> u32 { n }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() -> u32 {
|
|
|
|
$0S.bar(1)$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn bar(&self, n: u32) -> u32 { n }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() -> u32 {
|
|
|
|
let $0bar = S.bar(1);
|
|
|
|
bar
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_name_from_method_param() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn bar(&self, n: u32, size: u32) { n }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
S.bar($01 + 1$0, 2)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn bar(&self, n: u32, size: u32) { n }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let $0n = 1 + 1;
|
|
|
|
S.bar(n, 2)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_name_from_ufcs_method_param() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn bar(&self, n: u32, size: u32) { n }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
S::bar(&S, $01 + 1$0, 2)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn bar(&self, n: u32, size: u32) { n }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let $0n = 1 + 1;
|
|
|
|
S::bar(&S, n, 2)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-15 20:55:47 -06:00
|
|
|
fn extract_var_parameter_name_has_precedence_over_function() {
|
2021-02-15 19:43:32 -06:00
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
fn bar(test: u32, size: u32)
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar(1, $0symbol_size(1, 2)$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn bar(test: u32, size: u32)
|
|
|
|
|
|
|
|
fn foo() {
|
2021-02-15 20:55:47 -06:00
|
|
|
let $0size = symbol_size(1, 2);
|
|
|
|
bar(1, size);
|
2021-02-15 19:43:32 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-25 11:28:15 -05:00
|
|
|
#[test]
|
|
|
|
fn extract_macro_call() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r"
|
|
|
|
struct Vec;
|
|
|
|
macro_rules! vec {
|
|
|
|
() => {Vec}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let _ = $0vec![]$0;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Vec;
|
|
|
|
macro_rules! vec {
|
|
|
|
() => {Vec}
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let $0vec = vec![];
|
|
|
|
let _ = vec;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-30 14:36:49 -06:00
|
|
|
#[test]
|
2020-06-26 18:21:43 -05:00
|
|
|
fn test_extract_var_for_return_not_applicable() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(extract_variable, "fn foo() { $0return$0; } ");
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-26 18:21:43 -05:00
|
|
|
fn test_extract_var_for_break_not_applicable() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(extract_variable, "fn main() { loop { $0break$0; }; }");
|
2019-01-30 14:36:49 -06:00
|
|
|
}
|
|
|
|
|
2021-06-02 01:49:31 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_var_unit_expr_not_applicable() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut i = 3;
|
|
|
|
$0if i >= 0 {
|
|
|
|
i += 1;
|
|
|
|
} else {
|
|
|
|
i -= 1;
|
|
|
|
}$0
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_target(extract_variable, "fn foo() -> u32 { $0return 2 + 2$0; }", "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 {
|
2021-01-06 14:15:48 -06:00
|
|
|
true => ($02 + 2$0, true)
|
2019-02-08 17:34:05 -06:00
|
|
|
_ => (0, false)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"2 + 2",
|
|
|
|
);
|
|
|
|
}
|
2021-07-21 10:52:17 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_var_no_block_body() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
extract_variable,
|
|
|
|
r"
|
|
|
|
const X: usize = $0100$0;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2022-01-04 19:03:27 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_mutable_reference_parameter() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct S {
|
|
|
|
vec: Vec<u8>
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &mut S) {
|
|
|
|
$0s.vec$0.push(0);
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct S {
|
|
|
|
vec: Vec<u8>
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &mut S) {
|
|
|
|
let $0var_name = &mut s.vec;
|
|
|
|
var_name.push(0);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_mutable_reference_parameter_deep_nesting() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct Y {
|
|
|
|
field: X
|
|
|
|
}
|
|
|
|
struct X {
|
|
|
|
field: S
|
|
|
|
}
|
|
|
|
struct S {
|
|
|
|
vec: Vec<u8>
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(f: &mut Y) {
|
|
|
|
$0f.field.field.vec$0.push(0);
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct Y {
|
|
|
|
field: X
|
|
|
|
}
|
|
|
|
struct X {
|
|
|
|
field: S
|
|
|
|
}
|
|
|
|
struct S {
|
|
|
|
vec: Vec<u8>
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(f: &mut Y) {
|
|
|
|
let $0var_name = &mut f.field.field.vec;
|
|
|
|
var_name.push(0);
|
2022-01-04 19:15:54 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_reference_parameter() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &S) {
|
|
|
|
$0s.sub$0.do_thing();
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &S) {
|
|
|
|
let $0x = &s.sub;
|
|
|
|
x.do_thing();
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_reference_parameter_deep_nesting() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct Z;
|
|
|
|
impl Z {
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Y {
|
|
|
|
field: Z
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X {
|
|
|
|
field: Y
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &S) {
|
|
|
|
$0s.sub.field.field$0.do_thing();
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct Z;
|
|
|
|
impl Z {
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Y {
|
|
|
|
field: Z
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X {
|
|
|
|
field: Y
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &S) {
|
|
|
|
let $0z = &s.sub.field.field;
|
|
|
|
z.do_thing();
|
2022-01-04 19:18:55 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_regular_parameter() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: S) {
|
|
|
|
$0s.sub$0.do_thing();
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: S) {
|
|
|
|
let $0x = s.sub;
|
|
|
|
x.do_thing();
|
2022-01-04 19:27:15 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-01-04 19:36:04 -06:00
|
|
|
fn test_extract_var_mutable_reference_local() {
|
2022-01-04 19:27:15 -06:00
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
fn new() -> S {
|
2022-01-04 20:16:22 -06:00
|
|
|
S {
|
2022-01-04 19:27:15 -06:00
|
|
|
sub: X::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn new() -> X {
|
|
|
|
X { }
|
|
|
|
}
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let local = &mut S::new();
|
|
|
|
$0local.sub$0.do_thing();
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
fn new() -> S {
|
2022-01-04 20:16:22 -06:00
|
|
|
S {
|
2022-01-04 19:27:15 -06:00
|
|
|
sub: X::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn new() -> X {
|
|
|
|
X { }
|
|
|
|
}
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let local = &mut S::new();
|
2022-01-04 19:36:04 -06:00
|
|
|
let $0x = &mut local.sub;
|
|
|
|
x.do_thing();
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_var_reference_local() {
|
|
|
|
check_assist(
|
|
|
|
extract_variable,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
fn new() -> S {
|
2022-01-04 20:16:22 -06:00
|
|
|
S {
|
2022-01-04 19:36:04 -06:00
|
|
|
sub: X::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn new() -> X {
|
|
|
|
X { }
|
|
|
|
}
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let local = &S::new();
|
|
|
|
$0local.sub$0.do_thing();
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
sub: X
|
|
|
|
}
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
fn new() -> S {
|
2022-01-04 20:16:22 -06:00
|
|
|
S {
|
2022-01-04 19:36:04 -06:00
|
|
|
sub: X::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl X {
|
|
|
|
fn new() -> X {
|
|
|
|
X { }
|
|
|
|
}
|
|
|
|
fn do_thing(&self) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let local = &S::new();
|
|
|
|
let $0x = &local.sub;
|
2022-01-04 19:27:15 -06:00
|
|
|
x.do_thing();
|
2022-01-04 19:03:27 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|