diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-10 15:32:34 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-10 15:32:34 +0000 |
commit | acd90bc33b6111b47af68d51ec668bf027afb31d (patch) | |
tree | 42e0151888d94e8f9e33d52e44771252d9e151d5 /crates/ra_ide_api_light/src/assists | |
parent | 3990f93921537c4ab4db3a89b76ecfd20504d93f (diff) | |
parent | a0c978cd0ce95446f6d3e6a13047474670d9ee55 (diff) |
Merge #482
482: fix code duplication r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api_light/src/assists')
-rw-r--r-- | crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs | 26 |
1 files changed, 6 insertions, 20 deletions
diff --git a/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs b/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs index 30c371480..d64c34d54 100644 --- a/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs +++ b/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs | |||
@@ -1,9 +1,9 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{AstNode, ast}; |
2 | AstNode, SyntaxKind::{L_CURLY, R_CURLY, WHITESPACE}, | ||
3 | ast, | ||
4 | }; | ||
5 | 2 | ||
6 | use crate::assists::{AssistCtx, Assist}; | 3 | use crate::{ |
4 | assists::{AssistCtx, Assist}, | ||
5 | formatting::extract_trivial_expression, | ||
6 | }; | ||
7 | 7 | ||
8 | pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { | 8 | pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { |
9 | let if_expr: &ast::IfExpr = ctx.node_at_offset()?; | 9 | let if_expr: &ast::IfExpr = ctx.node_at_offset()?; |
@@ -39,26 +39,12 @@ fn build_match_expr( | |||
39 | } | 39 | } |
40 | 40 | ||
41 | fn format_arm(block: &ast::Block) -> String { | 41 | fn format_arm(block: &ast::Block) -> String { |
42 | match extract_expression(block) { | 42 | match extract_trivial_expression(block) { |
43 | None => block.syntax().text().to_string(), | 43 | None => block.syntax().text().to_string(), |
44 | Some(e) => format!("{},", e.syntax().text()), | 44 | Some(e) => format!("{},", e.syntax().text()), |
45 | } | 45 | } |
46 | } | 46 | } |
47 | 47 | ||
48 | fn extract_expression(block: &ast::Block) -> Option<&ast::Expr> { | ||
49 | let expr = block.expr()?; | ||
50 | let non_trivial_children = block.syntax().children().filter(|it| { | ||
51 | !(it == &expr.syntax() | ||
52 | || it.kind() == L_CURLY | ||
53 | || it.kind() == R_CURLY | ||
54 | || it.kind() == WHITESPACE) | ||
55 | }); | ||
56 | if non_trivial_children.count() > 0 { | ||
57 | return None; | ||
58 | } | ||
59 | Some(expr) | ||
60 | } | ||
61 | |||
62 | #[cfg(test)] | 48 | #[cfg(test)] |
63 | mod tests { | 49 | mod tests { |
64 | use super::*; | 50 | use super::*; |