]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/handlers/move_guard.rs
Support `if let` match guards
[rust.git] / crates / ide_assists / src / handlers / move_guard.rs
1 use syntax::{
2     ast::{edit::AstNodeEdit, make, AstNode, BlockExpr, Expr, IfExpr, MatchArm},
3     SyntaxKind::WHITESPACE,
4 };
5
6 use crate::{AssistContext, AssistId, AssistKind, Assists};
7
8 // Assist: move_guard_to_arm_body
9 //
10 // Moves match guard into match arm body.
11 //
12 // ```
13 // enum Action { Move { distance: u32 }, Stop }
14 //
15 // fn handle(action: Action) {
16 //     match action {
17 //         Action::Move { distance } $0if distance > 10 => foo(),
18 //         _ => (),
19 //     }
20 // }
21 // ```
22 // ->
23 // ```
24 // enum Action { Move { distance: u32 }, Stop }
25 //
26 // fn handle(action: Action) {
27 //     match action {
28 //         Action::Move { distance } => if distance > 10 {
29 //             foo()
30 //         },
31 //         _ => (),
32 //     }
33 // }
34 // ```
35 pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
36     let match_arm = ctx.find_node_at_offset::<MatchArm>()?;
37     let guard = match_arm.guard()?;
38     let space_before_guard = guard.syntax().prev_sibling_or_token();
39
40     // FIXME: support `if let` guards too
41     if guard.let_token().is_some() {
42         return None;
43     }
44     let guard_condition = guard.expr()?;
45     let arm_expr = match_arm.expr()?;
46     let if_expr = make::expr_if(
47         make::condition(guard_condition, None),
48         make::block_expr(None, Some(arm_expr.clone())),
49         None,
50     )
51     .indent(arm_expr.indent_level());
52
53     let target = guard.syntax().text_range();
54     acc.add(
55         AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
56         "Move guard to arm body",
57         target,
58         |edit| {
59             match space_before_guard {
60                 Some(element) if element.kind() == WHITESPACE => {
61                     edit.delete(element.text_range());
62                 }
63                 _ => (),
64             };
65
66             edit.delete(guard.syntax().text_range());
67             edit.replace_ast(arm_expr, if_expr);
68         },
69     )
70 }
71
72 // Assist: move_arm_cond_to_match_guard
73 //
74 // Moves if expression from match arm body into a guard.
75 //
76 // ```
77 // enum Action { Move { distance: u32 }, Stop }
78 //
79 // fn handle(action: Action) {
80 //     match action {
81 //         Action::Move { distance } => $0if distance > 10 { foo() },
82 //         _ => (),
83 //     }
84 // }
85 // ```
86 // ->
87 // ```
88 // enum Action { Move { distance: u32 }, Stop }
89 //
90 // fn handle(action: Action) {
91 //     match action {
92 //         Action::Move { distance } if distance > 10 => foo(),
93 //         _ => (),
94 //     }
95 // }
96 // ```
97 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
98     let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
99     let match_pat = match_arm.pat()?;
100     let arm_body = match_arm.expr()?;
101
102     let mut replace_node = None;
103     let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
104         let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
105         if let Expr::IfExpr(e) = block_expr.tail_expr()? {
106             replace_node = Some(block_expr.syntax().clone());
107             Some(e)
108         } else {
109             None
110         }
111     })?;
112     let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
113
114     let cond = if_expr.condition()?;
115     let then_block = if_expr.then_branch()?;
116
117     // Not support if with else branch
118     if if_expr.else_branch().is_some() {
119         return None;
120     }
121     // Not support moving if let to arm guard
122     if cond.is_pattern_cond() {
123         return None;
124     }
125
126     let buf = format!(" if {}", cond.syntax().text());
127
128     acc.add(
129         AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
130         "Move condition to match guard",
131         replace_node.text_range(),
132         |edit| {
133             let then_only_expr = then_block.statements().next().is_none();
134
135             match &then_block.tail_expr() {
136                 Some(then_expr) if then_only_expr => {
137                     edit.replace(replace_node.text_range(), then_expr.syntax().text())
138                 }
139                 _ if replace_node != *if_expr.syntax() => {
140                     // Dedent because if_expr is in a BlockExpr
141                     let replace_with = then_block.dedent(1.into()).syntax().text();
142                     edit.replace(replace_node.text_range(), replace_with)
143                 }
144                 _ => edit.replace(replace_node.text_range(), then_block.syntax().text()),
145             }
146
147             edit.insert(match_pat.syntax().text_range().end(), buf);
148         },
149     )
150 }
151
152 #[cfg(test)]
153 mod tests {
154     use super::*;
155
156     use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
157
158     #[test]
159     fn move_guard_to_arm_body_target() {
160         check_assist_target(
161             move_guard_to_arm_body,
162             r#"
163 fn main() {
164     match 92 {
165         x $0if x > 10 => false,
166         _ => true
167     }
168 }
169 "#,
170             r#"if x > 10"#,
171         );
172     }
173
174     #[test]
175     fn move_guard_to_arm_body_works() {
176         check_assist(
177             move_guard_to_arm_body,
178             r#"
179 fn main() {
180     match 92 {
181         x $0if x > 10 => false,
182         _ => true
183     }
184 }
185 "#,
186             r#"
187 fn main() {
188     match 92 {
189         x => if x > 10 {
190             false
191         },
192         _ => true
193     }
194 }
195 "#,
196         );
197     }
198
199     #[test]
200     fn move_guard_to_arm_body_works_complex_match() {
201         check_assist(
202             move_guard_to_arm_body,
203             r#"
204 fn main() {
205     match 92 {
206         $0x @ 4 | x @ 5    if x > 5 => true,
207         _ => false
208     }
209 }
210 "#,
211             r#"
212 fn main() {
213     match 92 {
214         x @ 4 | x @ 5 => if x > 5 {
215             true
216         },
217         _ => false
218     }
219 }
220 "#,
221         );
222     }
223
224     #[test]
225     fn move_arm_cond_to_match_guard_works() {
226         check_assist(
227             move_arm_cond_to_match_guard,
228             r#"
229 fn main() {
230     match 92 {
231         x => if x > 10 { $0false },
232         _ => true
233     }
234 }
235 "#,
236             r#"
237 fn main() {
238     match 92 {
239         x if x > 10 => false,
240         _ => true
241     }
242 }
243 "#,
244         );
245     }
246
247     #[test]
248     fn move_arm_cond_in_block_to_match_guard_works() {
249         check_assist(
250             move_arm_cond_to_match_guard,
251             r#"
252 fn main() {
253     match 92 {
254         x => {
255             $0if x > 10 {
256                 false
257             }
258         },
259         _ => true
260     }
261 }
262 "#,
263             r#"
264 fn main() {
265     match 92 {
266         x if x > 10 => false,
267         _ => true
268     }
269 }
270 "#,
271         );
272     }
273
274     #[test]
275     fn move_arm_cond_to_match_guard_if_let_not_works() {
276         check_assist_not_applicable(
277             move_arm_cond_to_match_guard,
278             r#"
279 fn main() {
280     match 92 {
281         x => if let 62 = x { $0false },
282         _ => true
283     }
284 }
285 "#,
286         );
287     }
288
289     #[test]
290     fn move_arm_cond_to_match_guard_if_empty_body_works() {
291         check_assist(
292             move_arm_cond_to_match_guard,
293             r#"
294 fn main() {
295     match 92 {
296         x => if x > 10 { $0 },
297         _ => true
298     }
299 }
300 "#,
301             r#"
302 fn main() {
303     match 92 {
304         x if x > 10 => {  },
305         _ => true
306     }
307 }
308 "#,
309         );
310     }
311
312     #[test]
313     fn move_arm_cond_to_match_guard_if_multiline_body_works() {
314         check_assist(
315             move_arm_cond_to_match_guard,
316             r#"
317 fn main() {
318     match 92 {
319         x => if x > 10 {
320             92;$0
321             false
322         },
323         _ => true
324     }
325 }
326 "#,
327             r#"
328 fn main() {
329     match 92 {
330         x if x > 10 => {
331             92;
332             false
333         },
334         _ => true
335     }
336 }
337 "#,
338         );
339     }
340
341     #[test]
342     fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
343         check_assist(
344             move_arm_cond_to_match_guard,
345             r#"
346 fn main() {
347     match 92 {
348         x => {
349             if x > 10 {
350                 92;$0
351                 false
352             }
353         }
354         _ => true
355     }
356 }
357 "#,
358             r#"
359 fn main() {
360     match 92 {
361         x if x > 10 => {
362             92;
363             false
364         }
365         _ => true
366     }
367 }
368 "#,
369         )
370     }
371 }