@@ -1862,9 +1862,6 @@ where
1862
1862
} ;
1863
1863
let used_width = extra_offset ( callee_str, shape) ;
1864
1864
let one_line_width = shape. width . checked_sub ( used_width + 2 * paren_overhead) ?;
1865
- // 1 = "("
1866
- let combine_arg_with_callee =
1867
- callee_str. len ( ) + 1 <= context. config . tab_spaces ( ) && args. len ( ) == 1 ;
1868
1865
1869
1866
// 1 = "(" or ")"
1870
1867
let one_line_shape = shape
@@ -1889,7 +1886,7 @@ where
1889
1886
one_line_width,
1890
1887
args_max_width,
1891
1888
force_trailing_comma,
1892
- combine_arg_with_callee ,
1889
+ callee_str ,
1893
1890
) ?;
1894
1891
1895
1892
if !context. use_block_indent ( ) && need_block_indent ( & list_str, nested_shape) && !extendable {
@@ -1930,7 +1927,7 @@ fn rewrite_call_args<'a, T>(
1930
1927
one_line_width : usize ,
1931
1928
args_max_width : usize ,
1932
1929
force_trailing_comma : bool ,
1933
- combine_arg_with_callee : bool ,
1930
+ callee_str : & str ,
1934
1931
) -> Option < ( bool , String ) >
1935
1932
where
1936
1933
T : Rewrite + Spanned + ToExpr + ' a ,
@@ -1960,7 +1957,7 @@ where
1960
1957
nested_shape,
1961
1958
one_line_width,
1962
1959
args_max_width,
1963
- combine_arg_with_callee ,
1960
+ callee_str ,
1964
1961
) ;
1965
1962
1966
1963
let fmt = ListFormatting {
@@ -1980,7 +1977,8 @@ where
1980
1977
config : context. config ,
1981
1978
} ;
1982
1979
1983
- write_list ( & item_vec, & fmt) . map ( |args_str| ( tactic != DefinitiveListTactic :: Vertical , args_str) )
1980
+ write_list ( & item_vec, & fmt)
1981
+ . map ( |args_str| ( tactic == DefinitiveListTactic :: Horizontal , args_str) )
1984
1982
}
1985
1983
1986
1984
fn try_overflow_last_arg < ' a , T > (
@@ -1991,11 +1989,14 @@ fn try_overflow_last_arg<'a, T>(
1991
1989
nested_shape : Shape ,
1992
1990
one_line_width : usize ,
1993
1991
args_max_width : usize ,
1994
- combine_arg_with_callee : bool ,
1992
+ callee_str : & str ,
1995
1993
) -> DefinitiveListTactic
1996
1994
where
1997
1995
T : Rewrite + Spanned + ToExpr + ' a ,
1998
1996
{
1997
+ // 1 = "("
1998
+ let combine_arg_with_callee =
1999
+ callee_str. len ( ) + 1 <= context. config . tab_spaces ( ) && args. len ( ) == 1 ;
1999
2000
let overflow_last = combine_arg_with_callee || can_be_overflowed ( context, args) ;
2000
2001
2001
2002
// Replace the last item with its first line to see if it fits with
@@ -2032,6 +2033,16 @@ where
2032
2033
_ if args. len ( ) >= 1 => {
2033
2034
item_vec[ args. len ( ) - 1 ] . item = args. last ( )
2034
2035
. and_then ( |last_arg| last_arg. rewrite ( context, nested_shape) ) ;
2036
+
2037
+ let default_tactic = || {
2038
+ definitive_tactic (
2039
+ & * item_vec,
2040
+ ListTactic :: LimitedHorizontalVertical ( args_max_width) ,
2041
+ Separator :: Comma ,
2042
+ one_line_width,
2043
+ )
2044
+ } ;
2045
+
2035
2046
// Use horizontal layout for a function with a single argument as long as
2036
2047
// everything fits in a single line.
2037
2048
if args. len ( ) == 1
@@ -2042,12 +2053,44 @@ where
2042
2053
{
2043
2054
tactic = DefinitiveListTactic :: Horizontal ;
2044
2055
} else {
2045
- tactic = definitive_tactic (
2046
- & * item_vec,
2047
- ListTactic :: LimitedHorizontalVertical ( args_max_width) ,
2048
- Separator :: Comma ,
2049
- one_line_width,
2050
- ) ;
2056
+ tactic = default_tactic ( ) ;
2057
+ let is_simple_enough =
2058
+ tactic == DefinitiveListTactic :: Vertical && is_every_args_simple ( args) ;
2059
+ if is_simple_enough
2060
+ && FORMAT_LIKE_WHITELIST
2061
+ . iter ( )
2062
+ . find ( |s| * * s == callee_str)
2063
+ . is_some ( )
2064
+ {
2065
+ let args_tactic = definitive_tactic (
2066
+ & item_vec[ 1 ..] ,
2067
+ ListTactic :: HorizontalVertical ,
2068
+ Separator :: Comma ,
2069
+ nested_shape. width ,
2070
+ ) ;
2071
+ tactic = if args_tactic == DefinitiveListTactic :: Horizontal {
2072
+ DefinitiveListTactic :: FormatCall
2073
+ } else {
2074
+ default_tactic ( )
2075
+ } ;
2076
+ } else if is_simple_enough && item_vec. len ( ) >= 2
2077
+ && WRITE_LIKE_WHITELIST
2078
+ . iter ( )
2079
+ . find ( |s| * * s == callee_str)
2080
+ . is_some ( )
2081
+ {
2082
+ let args_tactic = definitive_tactic (
2083
+ & item_vec[ 2 ..] ,
2084
+ ListTactic :: HorizontalVertical ,
2085
+ Separator :: Comma ,
2086
+ nested_shape. width ,
2087
+ ) ;
2088
+ tactic = if args_tactic == DefinitiveListTactic :: Horizontal {
2089
+ DefinitiveListTactic :: WriteCall
2090
+ } else {
2091
+ default_tactic ( )
2092
+ } ;
2093
+ }
2051
2094
}
2052
2095
}
2053
2096
_ => ( ) ,
@@ -2056,6 +2099,30 @@ where
2056
2099
tactic
2057
2100
}
2058
2101
2102
+ fn is_simple_arg ( expr : & ast:: Expr ) -> bool {
2103
+ match expr. node {
2104
+ ast:: ExprKind :: Lit ( ..) => true ,
2105
+ ast:: ExprKind :: Path ( ref qself, ref path) => qself. is_none ( ) && path. segments . len ( ) <= 1 ,
2106
+ ast:: ExprKind :: AddrOf ( _, ref expr)
2107
+ | ast:: ExprKind :: Box ( ref expr)
2108
+ | ast:: ExprKind :: Cast ( ref expr, _)
2109
+ | ast:: ExprKind :: Field ( ref expr, _)
2110
+ | ast:: ExprKind :: Try ( ref expr)
2111
+ | ast:: ExprKind :: TupField ( ref expr, _)
2112
+ | ast:: ExprKind :: Unary ( _, ref expr) => is_simple_arg ( expr) ,
2113
+ ast:: ExprKind :: Index ( ref lhs, ref rhs) | ast:: ExprKind :: Repeat ( ref lhs, ref rhs) => {
2114
+ is_simple_arg ( lhs) && is_simple_arg ( rhs)
2115
+ }
2116
+ _ => false ,
2117
+ }
2118
+ }
2119
+
2120
+ fn is_every_args_simple < T : ToExpr > ( lists : & [ & T ] ) -> bool {
2121
+ lists
2122
+ . iter ( )
2123
+ . all ( |arg| arg. to_expr ( ) . map_or ( false , is_simple_arg) )
2124
+ }
2125
+
2059
2126
/// Returns a shape for the last argument which is going to be overflowed.
2060
2127
fn last_arg_shape < T > (
2061
2128
lists : & [ & T ] ,
0 commit comments