From 8cf99b1d9065df4d941fb7fd0c67337eeb71de56 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 4 Dec 2017 12:06:46 +0900 Subject: [PATCH] Factor out array_tactic --- src/expr.rs | 74 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index e3b06b343fe..cbfb0f10512 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -436,38 +436,7 @@ pub fn rewrite_array( } } - let has_long_item = items - .iter() - .any(|li| li.item.as_ref().map(|s| s.len() > 10).unwrap_or(false)); - - let tactic = match context.config.indent_style() { - IndentStyle::Block => { - // FIXME wrong shape in one-line case - match shape.width.checked_sub(2 * bracket_size) { - Some(width) => { - let tactic = ListTactic::LimitedHorizontalVertical( - context.config.width_heuristics().array_width, - ); - definitive_tactic(&items, tactic, Separator::Comma, width) - } - None => DefinitiveListTactic::Vertical, - } - } - IndentStyle::Visual => { - if has_long_item || items.iter().any(ListItem::is_multiline) { - definitive_tactic( - &items, - ListTactic::LimitedHorizontalVertical( - context.config.width_heuristics().array_width, - ), - Separator::Comma, - nested_shape.width, - ) - } else { - DefinitiveListTactic::Mixed - } - } - }; + let tactic = array_tactic(context, shape, nested_shape, exprs, &items, bracket_size); let ends_with_newline = tactic.ends_with_newline(context.config.indent_style()); let fmt = ListFormatting { @@ -518,6 +487,47 @@ pub fn rewrite_array( Some(result) } +fn array_tactic( + context: &RewriteContext, + shape: Shape, + nested_shape: Shape, + exprs: &[&T], + items: &[ListItem], + bracket_size: usize, +) -> DefinitiveListTactic { + let has_long_item = items + .iter() + .any(|li| li.item.as_ref().map(|s| s.len() > 10).unwrap_or(false)); + + match context.config.indent_style() { + IndentStyle::Block => { + match shape.width.checked_sub(2 * bracket_size) { + Some(width) => { + let tactic = ListTactic::LimitedHorizontalVertical( + context.config.width_heuristics().array_width, + ); + definitive_tactic(items, tactic, Separator::Comma, width) + } + None => DefinitiveListTactic::Vertical, + } + } + IndentStyle::Visual => { + if has_long_item || items.iter().any(ListItem::is_multiline) { + definitive_tactic( + items, + ListTactic::LimitedHorizontalVertical( + context.config.width_heuristics().array_width, + ), + Separator::Comma, + nested_shape.width, + ) + } else { + DefinitiveListTactic::Mixed + } + } + } +} + fn nop_block_collapse(block_str: Option, budget: usize) -> Option { debug!("nop_block_collapse {:?} {}", block_str, budget); block_str.map(|block_str| {