aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/replace_if_let_with_match.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-02-09 08:52:09 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-02-09 08:52:09 +0000
commit3e8351fb0607f8711749b00d80f68bf25de01a76 (patch)
tree97388dafe71ececcbaf97249021b9c8d49786ccf /crates/ra_assists/src/replace_if_let_with_match.rs
parent12e3b4c70b5ef23b2fdfc197296d483680e125f9 (diff)
parent4fdeb54bb5c7ba0704839a65996766d223c51fc1 (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/replace_if_let_with_match.rs')
-rw-r--r--crates/ra_assists/src/replace_if_let_with_match.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs
index 683f0d119..a22ec5584 100644
--- a/crates/ra_assists/src/replace_if_let_with_match.rs
+++ b/crates/ra_assists/src/replace_if_let_with_match.rs
@@ -17,6 +17,7 @@ pub(crate) fn replace_if_let_with_match(ctx: AssistCtx<impl HirDatabase>) -> Opt
17 17
18 ctx.build("replace with match", |edit| { 18 ctx.build("replace with match", |edit| {
19 let match_expr = build_match_expr(expr, pat, then_block, else_block); 19 let match_expr = build_match_expr(expr, pat, then_block, else_block);
20 edit.target(if_expr.syntax().range());
20 edit.replace_node_and_indent(if_expr.syntax(), match_expr); 21 edit.replace_node_and_indent(if_expr.syntax(), match_expr);
21 edit.set_cursor(if_expr.syntax().range().start()) 22 edit.set_cursor(if_expr.syntax().range().start())
22 }) 23 })
@@ -46,7 +47,7 @@ fn format_arm(block: &ast::Block) -> String {
46#[cfg(test)] 47#[cfg(test)]
47mod tests { 48mod tests {
48 use super::*; 49 use super::*;
49 use crate::helpers::check_assist; 50 use crate::helpers::{check_assist, check_assist_target};
50 51
51 #[test] 52 #[test]
52 fn test_replace_if_let_with_match_unwraps_simple_expressions() { 53 fn test_replace_if_let_with_match_unwraps_simple_expressions() {
@@ -73,4 +74,26 @@ impl VariantData {
73} ", 74} ",
74 ) 75 )
75 } 76 }
77
78 #[test]
79 fn replace_if_let_with_match_target() {
80 check_assist_target(
81 replace_if_let_with_match,
82 "
83impl VariantData {
84 pub fn is_struct(&self) -> bool {
85 if <|>let VariantData::Struct(..) = *self {
86 true
87 } else {
88 false
89 }
90 }
91} ",
92 "if let VariantData::Struct(..) = *self {
93 true
94 } else {
95 false
96 }",
97 );
98 }
76} 99}