diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-09 08:52:09 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-09 08:52:09 +0000 |
commit | 3e8351fb0607f8711749b00d80f68bf25de01a76 (patch) | |
tree | 97388dafe71ececcbaf97249021b9c8d49786ccf /crates/ra_assists/src/introduce_variable.rs | |
parent | 12e3b4c70b5ef23b2fdfc197296d483680e125f9 (diff) | |
parent | 4fdeb54bb5c7ba0704839a65996766d223c51fc1 (diff) |
Merge #768
768: Sort assists by the range of the affected element r=matklad a=robojumper
Closes #763.
https://github.com/rust-analyzer/rust-analyzer/blob/3be98f2ac93b278828e76eb813bdd8033f647b12/crates/ra_assists/src/lib.rs#L233-L236
This could be made more robust by a) adding a way to identify actions by things other than their label and b) allowing arbitrary actions to appear in the list as long as the tested actions are there in the correct order. Let me know if I should do any of that.
Co-authored-by: robojumper <[email protected]>
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 | } |