From 27a540db4709307928f5f5ca0e4b7f3e1ac947c9 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 1 Dec 2017 13:44:40 +0900 Subject: [PATCH] Factor out a mess --- src/expr.rs | 70 +++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 0215975f9a3..2c878fe16b5 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2042,42 +2042,27 @@ fn try_overflow_last_arg<'a, T>( tactic = DefinitiveListTactic::Horizontal; } else { tactic = default_tactic(); - let is_simple_enough = - tactic == DefinitiveListTactic::Vertical && is_every_args_simple(args); - if is_simple_enough - && FORMAT_LIKE_WHITELIST - .iter() - .find(|s| **s == callee_str) - .is_some() - { - let args_tactic = definitive_tactic( - &item_vec[1..], - ListTactic::HorizontalVertical, - Separator::Comma, - nested_shape.width, - ); - tactic = if args_tactic == DefinitiveListTactic::Horizontal { - DefinitiveListTactic::FormatCall - } else { - default_tactic() - }; - } else if is_simple_enough && item_vec.len() >= 2 - && WRITE_LIKE_WHITELIST - .iter() - .find(|s| **s == callee_str) - .is_some() - { + + // For special-case macros, we may want to use different tactics. + let maybe_args_offset = maybe_get_args_offset(callee_str, args); + + if tactic == DefinitiveListTactic::Vertical && maybe_args_offset.is_some() { + let args_offset = maybe_args_offset.unwrap(); let args_tactic = definitive_tactic( - &item_vec[2..], + &item_vec[args_offset..], ListTactic::HorizontalVertical, Separator::Comma, nested_shape.width, ); - tactic = if args_tactic == DefinitiveListTactic::Horizontal { - DefinitiveListTactic::WriteCall - } else { - default_tactic() - }; + + // Every argument is simple and fits on a single line. + if args_tactic == DefinitiveListTactic::Horizontal { + tactic = if args_offset == 1 { + DefinitiveListTactic::FormatCall + } else { + DefinitiveListTactic::WriteCall + }; + } } } } @@ -2111,6 +2096,29 @@ fn is_every_args_simple(lists: &[&T]) -> bool { .all(|arg| arg.to_expr().map_or(false, is_simple_arg)) } +/// In case special-case style is required, returns an offset from which we start horizontal layout. +fn maybe_get_args_offset(callee_str: &str, args: &[&T]) -> Option { + if FORMAT_LIKE_WHITELIST + .iter() + .find(|s| **s == callee_str) + .is_some() + && args.len() >= 1 + && is_every_args_simple(args) + { + Some(1) + } else if WRITE_LIKE_WHITELIST + .iter() + .find(|s| **s == callee_str) + .is_some() + && args.len() >= 2 + && is_every_args_simple(args) + { + Some(2) + } else { + None + } +} + /// Returns a shape for the last argument which is going to be overflowed. fn last_arg_shape( lists: &[&T], -- 2.44.0