diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 26 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/replace_let_with_if_let.rs | 108 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 |
3 files changed, 136 insertions, 0 deletions
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 543224232..0848ab6bc 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -608,6 +608,32 @@ fn handle(action: Action) { | |||
608 | } | 608 | } |
609 | 609 | ||
610 | #[test] | 610 | #[test] |
611 | fn doctest_replace_let_with_if_let() { | ||
612 | check( | ||
613 | "replace_let_with_if_let", | ||
614 | r#####" | ||
615 | enum Option<T> { Some(T), None } | ||
616 | |||
617 | fn main(action: Action) { | ||
618 | <|>let x = compute(); | ||
619 | } | ||
620 | |||
621 | fn compute() -> Option<i32> { None } | ||
622 | "#####, | ||
623 | r#####" | ||
624 | enum Option<T> { Some(T), None } | ||
625 | |||
626 | fn main(action: Action) { | ||
627 | if let Some(x) = compute() { | ||
628 | } | ||
629 | } | ||
630 | |||
631 | fn compute() -> Option<i32> { None } | ||
632 | "#####, | ||
633 | ) | ||
634 | } | ||
635 | |||
636 | #[test] | ||
611 | fn doctest_replace_qualified_name_with_use() { | 637 | fn doctest_replace_qualified_name_with_use() { |
612 | check( | 638 | check( |
613 | "replace_qualified_name_with_use", | 639 | "replace_qualified_name_with_use", |
diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs new file mode 100644 index 000000000..10e41f97e --- /dev/null +++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs | |||
@@ -0,0 +1,108 @@ | |||
1 | use hir::Adt; | ||
2 | use ra_syntax::{ | ||
3 | ast::{self, make}, | ||
4 | AstNode, T, | ||
5 | }; | ||
6 | |||
7 | use crate::{ | ||
8 | assist_ctx::{Assist, AssistCtx}, | ||
9 | AssistId, | ||
10 | }; | ||
11 | use ast::edit::{AstNodeEdit, IndentLevel}; | ||
12 | use std::iter::once; | ||
13 | |||
14 | // Assist: replace_let_with_if_let | ||
15 | // | ||
16 | // Replaces `if let` with an else branch with a `match` expression. | ||
17 | // | ||
18 | // ``` | ||
19 | // # enum Option<T> { Some(T), None } | ||
20 | // | ||
21 | // fn main(action: Action) { | ||
22 | // <|>let x = compute(); | ||
23 | // } | ||
24 | // | ||
25 | // fn compute() -> Option<i32> { None } | ||
26 | // ``` | ||
27 | // -> | ||
28 | // ``` | ||
29 | // # enum Option<T> { Some(T), None } | ||
30 | // | ||
31 | // fn main(action: Action) { | ||
32 | // if let Some(x) = compute() { | ||
33 | // } | ||
34 | // } | ||
35 | // | ||
36 | // fn compute() -> Option<i32> { None } | ||
37 | // ``` | ||
38 | pub(crate) fn replace_let_with_if_let(ctx: AssistCtx) -> Option<Assist> { | ||
39 | let let_kw = ctx.find_token_at_offset(T![let])?; | ||
40 | let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?; | ||
41 | let init = let_stmt.initializer()?; | ||
42 | let original_pat = let_stmt.pat()?; | ||
43 | let ty = ctx.sema.type_of_expr(&init)?; | ||
44 | let enum_ = match ty.as_adt() { | ||
45 | Some(Adt::Enum(it)) => it, | ||
46 | _ => return None, | ||
47 | }; | ||
48 | let happy_case = | ||
49 | [("Result", "Ok"), ("Option", "Some")].iter().find_map(|(known_type, happy_case)| { | ||
50 | if &enum_.name(ctx.db).to_string() == known_type { | ||
51 | return Some(happy_case); | ||
52 | } | ||
53 | None | ||
54 | }); | ||
55 | |||
56 | ctx.add_assist(AssistId("replace_let_with_if_let"), "Replace with if-let", |edit| { | ||
57 | let with_placeholder: ast::Pat = match happy_case { | ||
58 | None => make::placeholder_pat().into(), | ||
59 | Some(var_name) => make::tuple_struct_pat( | ||
60 | make::path_unqualified(make::path_segment(make::name_ref(var_name))), | ||
61 | once(make::placeholder_pat().into()), | ||
62 | ) | ||
63 | .into(), | ||
64 | }; | ||
65 | let block = | ||
66 | IndentLevel::from_node(let_stmt.syntax()).increase_indent(make::block_expr(None, None)); | ||
67 | let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block); | ||
68 | let stmt = make::expr_stmt(if_); | ||
69 | |||
70 | let placeholder = stmt.syntax().descendants().find_map(ast::PlaceholderPat::cast).unwrap(); | ||
71 | let target_offset = | ||
72 | let_stmt.syntax().text_range().start() + placeholder.syntax().text_range().start(); | ||
73 | let stmt = stmt.replace_descendant(placeholder.into(), original_pat); | ||
74 | |||
75 | edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt)); | ||
76 | edit.target(let_kw.text_range()); | ||
77 | edit.set_cursor(target_offset); | ||
78 | }) | ||
79 | } | ||
80 | |||
81 | #[cfg(test)] | ||
82 | mod tests { | ||
83 | use crate::helpers::check_assist; | ||
84 | |||
85 | use super::*; | ||
86 | |||
87 | #[test] | ||
88 | fn replace_let_unknown_enum() { | ||
89 | check_assist( | ||
90 | replace_let_with_if_let, | ||
91 | r" | ||
92 | enum E<T> { X(T), Y(T) } | ||
93 | |||
94 | fn main() { | ||
95 | <|>let x = E::X(92); | ||
96 | } | ||
97 | ", | ||
98 | r" | ||
99 | enum E<T> { X(T), Y(T) } | ||
100 | |||
101 | fn main() { | ||
102 | if let <|>x = E::X(92) { | ||
103 | } | ||
104 | } | ||
105 | ", | ||
106 | ) | ||
107 | } | ||
108 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index becd5e99d..fa1f3dd26 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -118,6 +118,7 @@ mod handlers { | |||
118 | mod remove_dbg; | 118 | mod remove_dbg; |
119 | mod remove_mut; | 119 | mod remove_mut; |
120 | mod replace_if_let_with_match; | 120 | mod replace_if_let_with_match; |
121 | mod replace_let_with_if_let; | ||
121 | mod replace_qualified_name_with_use; | 122 | mod replace_qualified_name_with_use; |
122 | mod replace_unwrap_with_match; | 123 | mod replace_unwrap_with_match; |
123 | mod split_import; | 124 | mod split_import; |
@@ -154,6 +155,7 @@ mod handlers { | |||
154 | remove_dbg::remove_dbg, | 155 | remove_dbg::remove_dbg, |
155 | remove_mut::remove_mut, | 156 | remove_mut::remove_mut, |
156 | replace_if_let_with_match::replace_if_let_with_match, | 157 | replace_if_let_with_match::replace_if_let_with_match, |
158 | replace_let_with_if_let::replace_let_with_if_let, | ||
157 | replace_qualified_name_with_use::replace_qualified_name_with_use, | 159 | replace_qualified_name_with_use::replace_qualified_name_with_use, |
158 | replace_unwrap_with_match::replace_unwrap_with_match, | 160 | replace_unwrap_with_match::replace_unwrap_with_match, |
159 | split_import::split_import, | 161 | split_import::split_import, |