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 +++-------- crates/ra_ide_api_light/src/formatting.rs | 42 +++++++++++++++++ crates/ra_ide_api_light/src/join_lines.rs | 54 +++------------------- crates/ra_ide_api_light/src/lib.rs | 1 + 4 files changed, 55 insertions(+), 68 deletions(-) create mode 100644 crates/ra_ide_api_light/src/formatting.rs (limited to 'crates/ra_ide_api_light') 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::*; diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs new file mode 100644 index 000000000..1f3769209 --- /dev/null +++ b/crates/ra_ide_api_light/src/formatting.rs @@ -0,0 +1,42 @@ +use ra_syntax::{ + ast, AstNode, + SyntaxNode, SyntaxKind::*, +}; + +pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { + let expr = block.expr()?; + if expr.syntax().text().contains('\n') { + return None; + } + let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { + WHITESPACE | L_CURLY | R_CURLY => false, + _ => it != &expr.syntax(), + }); + if non_trivial_children.count() > 0 { + return None; + } + Some(expr) +} + +pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { + match left.kind() { + L_PAREN | L_BRACK => return "", + L_CURLY => { + if let USE_TREE = right.kind() { + return ""; + } + } + _ => (), + } + match right.kind() { + R_PAREN | R_BRACK => return "", + R_CURLY => { + if let USE_TREE = left.kind() { + return ""; + } + } + DOT => return "", + _ => (), + } + " " +} diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api_light/src/join_lines.rs index bda6be878..ab7c5b4b5 100644 --- a/crates/ra_ide_api_light/src/join_lines.rs +++ b/crates/ra_ide_api_light/src/join_lines.rs @@ -1,14 +1,15 @@ -use std::mem; - use itertools::Itertools; use ra_syntax::{ SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, - SyntaxKind::{self, WHITESPACE, COMMA, L_CURLY, R_CURLY, L_PAREN, R_PAREN, L_BRACK, R_BRACK, USE_TREE, DOT}, + SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, algo::find_covering_node, ast, }; -use crate::{LocalEdit, TextEditBuilder}; +use crate::{ + LocalEdit, TextEditBuilder, + formatting::{compute_ws, extract_trivial_expression}, +}; pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { let range = if range.is_empty() { @@ -132,7 +133,7 @@ fn remove_newline( fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { let block = ast::Block::cast(node.parent()?)?; let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; - let expr = single_expr(block)?; + let expr = extract_trivial_expression(block)?; edit.replace( block_expr.syntax().range(), expr.syntax().text().to_string(), @@ -140,26 +141,6 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Opti Some(()) } -fn single_expr(block: &ast::Block) -> Option<&ast::Expr> { - let mut res = None; - for child in block.syntax().children() { - if let Some(expr) = ast::Expr::cast(child) { - if expr.syntax().text().contains('\n') { - return None; - } - if mem::replace(&mut res, Some(expr)).is_some() { - return None; - } - } else { - match child.kind() { - WHITESPACE | L_CURLY | R_CURLY => (), - _ => return None, - } - } - } - res -} - fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; let (tree,) = use_tree_list.use_trees().collect_tuple()?; @@ -177,29 +158,6 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool { } } -fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { - match left.kind() { - L_PAREN | L_BRACK => return "", - L_CURLY => { - if let USE_TREE = right.kind() { - return ""; - } - } - _ => (), - } - match right.kind() { - R_PAREN | R_BRACK => return "", - R_CURLY => { - if let USE_TREE = left.kind() { - return ""; - } - } - DOT => return "", - _ => (), - } - " " -} - #[cfg(test)] mod tests { use crate::test_utils::{assert_eq_text, check_action, extract_range}; diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs index e632108ce..bc9bee752 100644 --- a/crates/ra_ide_api_light/src/lib.rs +++ b/crates/ra_ide_api_light/src/lib.rs @@ -14,6 +14,7 @@ mod test_utils; mod join_lines; mod typing; mod diagnostics; +pub(crate) mod formatting; pub use self::{ assists::LocalEdit, -- cgit v1.2.3