From a0c978cd0ce95446f6d3e6a13047474670d9ee55 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 18:32:02 +0300 Subject: fix code duplication --- .../src/assists/replace_if_let_with_match.rs | 26 +++++----------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'crates/ra_ide_api_light/src/assists') 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 @@ -use ra_syntax::{ - AstNode, SyntaxKind::{L_CURLY, R_CURLY, WHITESPACE}, - ast, -}; +use ra_syntax::{AstNode, ast}; -use crate::assists::{AssistCtx, Assist}; +use crate::{ + assists::{AssistCtx, Assist}, + formatting::extract_trivial_expression, +}; pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option { let if_expr: &ast::IfExpr = ctx.node_at_offset()?; @@ -39,26 +39,12 @@ fn build_match_expr( } fn format_arm(block: &ast::Block) -> String { - match extract_expression(block) { + match extract_trivial_expression(block) { None => block.syntax().text().to_string(), Some(e) => format!("{},", e.syntax().text()), } } -fn extract_expression(block: &ast::Block) -> Option<&ast::Expr> { - let expr = block.expr()?; - let non_trivial_children = block.syntax().children().filter(|it| { - !(it == &expr.syntax() - || it.kind() == L_CURLY - || it.kind() == R_CURLY - || it.kind() == WHITESPACE) - }); - if non_trivial_children.count() > 0 { - return None; - } - Some(expr) -} - #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3