mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
introduce_variable -> extract_variable
This commit is contained in:
parent
7488cd6a1b
commit
f5584668db
3 changed files with 73 additions and 73 deletions
|
@ -11,7 +11,7 @@ use test_utils::mark;
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, Assists};
|
use crate::{AssistContext, AssistId, Assists};
|
||||||
|
|
||||||
// Assist: introduce_variable
|
// Assist: extract_variable
|
||||||
//
|
//
|
||||||
// Extracts subexpression into a variable.
|
// Extracts subexpression into a variable.
|
||||||
//
|
//
|
||||||
|
@ -27,13 +27,13 @@ use crate::{AssistContext, AssistId, Assists};
|
||||||
// var_name * 4;
|
// var_name * 4;
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
if ctx.frange.range.is_empty() {
|
if ctx.frange.range.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let node = ctx.covering_element();
|
let node = ctx.covering_element();
|
||||||
if node.kind() == COMMENT {
|
if node.kind() == COMMENT {
|
||||||
mark::hit!(introduce_var_in_comment_is_not_applicable);
|
mark::hit!(extract_var_in_comment_is_not_applicable);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let expr = node.ancestors().find_map(valid_target_expr)?;
|
let expr = node.ancestors().find_map(valid_target_expr)?;
|
||||||
|
@ -43,7 +43,7 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let target = expr.syntax().text_range();
|
let target = expr.syntax().text_range();
|
||||||
acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| {
|
acc.add(AssistId("extract_variable"), "Extract into variable", target, move |edit| {
|
||||||
let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) {
|
let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) {
|
||||||
Some(field) => field.name_ref(),
|
Some(field) => field.name_ref(),
|
||||||
None => None,
|
None => None,
|
||||||
|
@ -74,7 +74,7 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
if is_full_stmt {
|
if is_full_stmt {
|
||||||
mark::hit!(test_introduce_var_expr_stmt);
|
mark::hit!(test_extract_var_expr_stmt);
|
||||||
if full_stmt.unwrap().semicolon_token().is_none() {
|
if full_stmt.unwrap().semicolon_token().is_none() {
|
||||||
buf.push_str(";");
|
buf.push_str(";");
|
||||||
}
|
}
|
||||||
|
@ -133,7 +133,7 @@ fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the syntax node which will follow the freshly introduced var
|
/// Returns the syntax node which will follow the freshly extractd var
|
||||||
/// and a boolean indicating whether we have to wrap it within a { } block
|
/// and a boolean indicating whether we have to wrap it within a { } block
|
||||||
/// to produce correct code.
|
/// to produce correct code.
|
||||||
/// It can be a statement, the last in a block expression or a wanna be block
|
/// It can be a statement, the last in a block expression or a wanna be block
|
||||||
|
@ -142,7 +142,7 @@ fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> {
|
||||||
expr.syntax().ancestors().find_map(|node| {
|
expr.syntax().ancestors().find_map(|node| {
|
||||||
if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) {
|
if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) {
|
||||||
if expr.syntax() == &node {
|
if expr.syntax() == &node {
|
||||||
mark::hit!(test_introduce_var_last_expr);
|
mark::hit!(test_extract_var_last_expr);
|
||||||
return Some((node, false));
|
return Some((node, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,9 +170,9 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_simple() {
|
fn test_extract_var_simple() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
foo(<|>1 + 1<|>);
|
foo(<|>1 + 1<|>);
|
||||||
|
@ -186,16 +186,16 @@ fn foo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn introduce_var_in_comment_is_not_applicable() {
|
fn extract_var_in_comment_is_not_applicable() {
|
||||||
mark::check!(introduce_var_in_comment_is_not_applicable);
|
mark::check!(extract_var_in_comment_is_not_applicable);
|
||||||
check_assist_not_applicable(introduce_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }");
|
check_assist_not_applicable(extract_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_expr_stmt() {
|
fn test_extract_var_expr_stmt() {
|
||||||
mark::check!(test_introduce_var_expr_stmt);
|
mark::check!(test_extract_var_expr_stmt);
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
<|>1 + 1<|>;
|
<|>1 + 1<|>;
|
||||||
|
@ -206,7 +206,7 @@ fn foo() {
|
||||||
}"#,
|
}"#,
|
||||||
);
|
);
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
<|>{ let x = 0; x }<|>
|
<|>{ let x = 0; x }<|>
|
||||||
|
@ -221,9 +221,9 @@ fn foo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_part_of_expr_stmt() {
|
fn test_extract_var_part_of_expr_stmt() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
<|>1<|> + 1;
|
<|>1<|> + 1;
|
||||||
|
@ -237,10 +237,10 @@ fn foo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_last_expr() {
|
fn test_extract_var_last_expr() {
|
||||||
mark::check!(test_introduce_var_last_expr);
|
mark::check!(test_extract_var_last_expr);
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
bar(<|>1 + 1<|>)
|
bar(<|>1 + 1<|>)
|
||||||
|
@ -254,7 +254,7 @@ fn foo() {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
<|>bar(1 + 1)<|>
|
<|>bar(1 + 1)<|>
|
||||||
|
@ -270,9 +270,9 @@ fn foo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_in_match_arm_no_block() {
|
fn test_extract_var_in_match_arm_no_block() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = true;
|
let x = true;
|
||||||
|
@ -295,9 +295,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_in_match_arm_with_block() {
|
fn test_extract_var_in_match_arm_with_block() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = true;
|
let x = true;
|
||||||
|
@ -327,9 +327,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_in_closure_no_block() {
|
fn test_extract_var_in_closure_no_block() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let lambda = |x: u32| <|>x * 2<|>;
|
let lambda = |x: u32| <|>x * 2<|>;
|
||||||
|
@ -344,9 +344,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_in_closure_with_block() {
|
fn test_extract_var_in_closure_with_block() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let lambda = |x: u32| { <|>x * 2<|> };
|
let lambda = |x: u32| { <|>x * 2<|> };
|
||||||
|
@ -361,9 +361,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_path_simple() {
|
fn test_extract_var_path_simple() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let o = <|>Some(true)<|>;
|
let o = <|>Some(true)<|>;
|
||||||
|
@ -379,9 +379,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_path_method() {
|
fn test_extract_var_path_method() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = <|>bar.foo()<|>;
|
let v = <|>bar.foo()<|>;
|
||||||
|
@ -397,9 +397,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_return() {
|
fn test_extract_var_return() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn foo() -> u32 {
|
fn foo() -> u32 {
|
||||||
<|>return 2 + 2<|>;
|
<|>return 2 + 2<|>;
|
||||||
|
@ -415,9 +415,9 @@ fn foo() -> u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_does_not_add_extra_whitespace() {
|
fn test_extract_var_does_not_add_extra_whitespace() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn foo() -> u32 {
|
fn foo() -> u32 {
|
||||||
|
|
||||||
|
@ -436,7 +436,7 @@ fn foo() -> u32 {
|
||||||
);
|
);
|
||||||
|
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn foo() -> u32 {
|
fn foo() -> u32 {
|
||||||
|
|
||||||
|
@ -453,7 +453,7 @@ fn foo() -> u32 {
|
||||||
);
|
);
|
||||||
|
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn foo() -> u32 {
|
fn foo() -> u32 {
|
||||||
let foo = 1;
|
let foo = 1;
|
||||||
|
@ -479,9 +479,9 @@ fn foo() -> u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_break() {
|
fn test_extract_var_break() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let result = loop {
|
let result = loop {
|
||||||
|
@ -501,9 +501,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_for_cast() {
|
fn test_extract_var_for_cast() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let v = <|>0f32 as u32<|>;
|
let v = <|>0f32 as u32<|>;
|
||||||
|
@ -519,9 +519,9 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn introduce_var_field_shorthand() {
|
fn extract_var_field_shorthand() {
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
struct S {
|
struct S {
|
||||||
foo: i32
|
foo: i32
|
||||||
|
@ -545,22 +545,22 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_for_return_not_applicable() {
|
fn test_extract_var_for_return_not_applicable() {
|
||||||
check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");
|
check_assist_not_applicable(extract_variable, "fn foo() { <|>return<|>; } ");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_introduce_var_for_break_not_applicable() {
|
fn test_extract_var_for_break_not_applicable() {
|
||||||
check_assist_not_applicable(introduce_variable, "fn main() { loop { <|>break<|>; }; }");
|
check_assist_not_applicable(extract_variable, "fn main() { loop { <|>break<|>; }; }");
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic
|
// FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic
|
||||||
#[test]
|
#[test]
|
||||||
fn introduce_var_target() {
|
fn extract_var_target() {
|
||||||
check_assist_target(introduce_variable, "fn foo() -> u32 { <|>return 2 + 2<|>; }", "2 + 2");
|
check_assist_target(extract_variable, "fn foo() -> u32 { <|>return 2 + 2<|>; }", "2 + 2");
|
||||||
|
|
||||||
check_assist_target(
|
check_assist_target(
|
||||||
introduce_variable,
|
extract_variable,
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = true;
|
let x = true;
|
|
@ -116,6 +116,7 @@ mod handlers {
|
||||||
mod change_visibility;
|
mod change_visibility;
|
||||||
mod early_return;
|
mod early_return;
|
||||||
mod extract_struct_from_enum_variant;
|
mod extract_struct_from_enum_variant;
|
||||||
|
mod extract_variable;
|
||||||
mod fill_match_arms;
|
mod fill_match_arms;
|
||||||
mod fix_visibility;
|
mod fix_visibility;
|
||||||
mod flip_binexpr;
|
mod flip_binexpr;
|
||||||
|
@ -123,7 +124,6 @@ mod handlers {
|
||||||
mod flip_trait_bound;
|
mod flip_trait_bound;
|
||||||
mod inline_local_variable;
|
mod inline_local_variable;
|
||||||
mod introduce_named_lifetime;
|
mod introduce_named_lifetime;
|
||||||
mod introduce_variable;
|
|
||||||
mod invert_if;
|
mod invert_if;
|
||||||
mod merge_imports;
|
mod merge_imports;
|
||||||
mod merge_match_arms;
|
mod merge_match_arms;
|
||||||
|
@ -157,6 +157,7 @@ mod handlers {
|
||||||
change_visibility::change_visibility,
|
change_visibility::change_visibility,
|
||||||
early_return::convert_to_guarded_return,
|
early_return::convert_to_guarded_return,
|
||||||
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
|
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
|
||||||
|
extract_variable::extract_variable,
|
||||||
fill_match_arms::fill_match_arms,
|
fill_match_arms::fill_match_arms,
|
||||||
fix_visibility::fix_visibility,
|
fix_visibility::fix_visibility,
|
||||||
flip_binexpr::flip_binexpr,
|
flip_binexpr::flip_binexpr,
|
||||||
|
@ -164,7 +165,6 @@ mod handlers {
|
||||||
flip_trait_bound::flip_trait_bound,
|
flip_trait_bound::flip_trait_bound,
|
||||||
inline_local_variable::inline_local_variable,
|
inline_local_variable::inline_local_variable,
|
||||||
introduce_named_lifetime::introduce_named_lifetime,
|
introduce_named_lifetime::introduce_named_lifetime,
|
||||||
introduce_variable::introduce_variable,
|
|
||||||
invert_if::invert_if,
|
invert_if::invert_if,
|
||||||
merge_imports::merge_imports,
|
merge_imports::merge_imports,
|
||||||
merge_match_arms::merge_match_arms,
|
merge_match_arms::merge_match_arms,
|
||||||
|
|
|
@ -352,6 +352,24 @@ enum A { One(One) }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn doctest_extract_variable() {
|
||||||
|
check_doc_test(
|
||||||
|
"extract_variable",
|
||||||
|
r#####"
|
||||||
|
fn main() {
|
||||||
|
<|>(1 + 2)<|> * 4;
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
r#####"
|
||||||
|
fn main() {
|
||||||
|
let $0var_name = (1 + 2);
|
||||||
|
var_name * 4;
|
||||||
|
}
|
||||||
|
"#####,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctest_fill_match_arms() {
|
fn doctest_fill_match_arms() {
|
||||||
check_doc_test(
|
check_doc_test(
|
||||||
|
@ -491,24 +509,6 @@ impl<'a> Cursor<'a> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn doctest_introduce_variable() {
|
|
||||||
check_doc_test(
|
|
||||||
"introduce_variable",
|
|
||||||
r#####"
|
|
||||||
fn main() {
|
|
||||||
<|>(1 + 2)<|> * 4;
|
|
||||||
}
|
|
||||||
"#####,
|
|
||||||
r#####"
|
|
||||||
fn main() {
|
|
||||||
let $0var_name = (1 + 2);
|
|
||||||
var_name * 4;
|
|
||||||
}
|
|
||||||
"#####,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctest_invert_if() {
|
fn doctest_invert_if() {
|
||||||
check_doc_test(
|
check_doc_test(
|
||||||
|
|
Loading…
Reference in a new issue