diff options
author | robojumper <[email protected]> | 2019-02-08 23:34:05 +0000 |
---|---|---|
committer | robojumper <[email protected]> | 2019-02-08 23:34:26 +0000 |
commit | 3be98f2ac93b278828e76eb813bdd8033f647b12 (patch) | |
tree | f18bf13961c0ad22f48278c46ab4d7dc072fac08 /crates/ra_assists/src/introduce_variable.rs | |
parent | a3622eb629bf7acea933d956f8cee902ae357fca (diff) |
Add tests for action target ranges
Diffstat (limited to 'crates/ra_assists/src/introduce_variable.rs')
-rw-r--r-- | crates/ra_assists/src/introduce_variable.rs | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index 4f7c9f3c2..934d1d6b3 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs | |||
@@ -45,6 +45,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Ass | |||
45 | } else { | 45 | } else { |
46 | buf.push_str(";"); | 46 | buf.push_str(";"); |
47 | indent.text().push_to(&mut buf); | 47 | indent.text().push_to(&mut buf); |
48 | edit.target(expr.syntax().range()); | ||
48 | edit.replace(expr.syntax().range(), "var_name".to_string()); | 49 | edit.replace(expr.syntax().range(), "var_name".to_string()); |
49 | edit.insert(anchor_stmt.range().start(), buf); | 50 | edit.insert(anchor_stmt.range().start(), buf); |
50 | if wrap_in_block { | 51 | if wrap_in_block { |
@@ -58,7 +59,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Ass | |||
58 | fn valid_covering_node(node: &SyntaxNode) -> bool { | 59 | fn valid_covering_node(node: &SyntaxNode) -> bool { |
59 | node.kind() != COMMENT | 60 | node.kind() != COMMENT |
60 | } | 61 | } |
61 | /// Check wether the node is a valid expression which can be extracted to a variable. | 62 | /// Check whether the node is a valid expression which can be extracted to a variable. |
62 | /// In general that's true for any expression, but in some cases that would produce invalid code. | 63 | /// In general that's true for any expression, but in some cases that would produce invalid code. |
63 | fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> { | 64 | fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> { |
64 | match node.kind() { | 65 | match node.kind() { |
@@ -74,7 +75,7 @@ fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> { | |||
74 | /// and a boolean indicating whether we have to wrap it within a { } block | 75 | /// and a boolean indicating whether we have to wrap it within a { } block |
75 | /// to produce correct code. | 76 | /// to produce correct code. |
76 | /// It can be a statement, the last in a block expression or a wanna be block | 77 | /// It can be a statement, the last in a block expression or a wanna be block |
77 | /// expression like a lamba or match arm. | 78 | /// expression like a lambda or match arm. |
78 | fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { | 79 | fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { |
79 | expr.syntax().ancestors().find_map(|node| { | 80 | expr.syntax().ancestors().find_map(|node| { |
80 | if ast::Stmt::cast(node).is_some() { | 81 | if ast::Stmt::cast(node).is_some() { |
@@ -100,7 +101,7 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { | |||
100 | #[cfg(test)] | 101 | #[cfg(test)] |
101 | mod tests { | 102 | mod tests { |
102 | use super::*; | 103 | use super::*; |
103 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_range}; | 104 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_range, check_assist_target, check_assist_range_target}; |
104 | 105 | ||
105 | #[test] | 106 | #[test] |
106 | fn test_introduce_var_simple() { | 107 | fn test_introduce_var_simple() { |
@@ -425,4 +426,32 @@ fn main() { | |||
425 | ", | 426 | ", |
426 | ); | 427 | ); |
427 | } | 428 | } |
429 | |||
430 | // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic | ||
431 | #[test] | ||
432 | fn introduce_var_target() { | ||
433 | check_assist_target( | ||
434 | introduce_variable, | ||
435 | " | ||
436 | fn foo() -> u32 { | ||
437 | r<|>eturn 2 + 2; | ||
438 | } | ||
439 | ", | ||
440 | "2 + 2", | ||
441 | ); | ||
442 | |||
443 | check_assist_range_target( | ||
444 | introduce_variable, | ||
445 | " | ||
446 | fn main() { | ||
447 | let x = true; | ||
448 | let tuple = match x { | ||
449 | true => (<|>2 + 2<|>, true) | ||
450 | _ => (0, false) | ||
451 | }; | ||
452 | } | ||
453 | ", | ||
454 | "2 + 2", | ||
455 | ); | ||
456 | } | ||
428 | } | 457 | } |