Skip to content

Commit 4b3d502

Browse files
committed
Factor out a mess
1 parent 644db61 commit 4b3d502

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

src/expr.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,42 +2042,27 @@ where
20422042
tactic = DefinitiveListTactic::Horizontal;
20432043
} else {
20442044
tactic = default_tactic();
2045-
let is_simple_enough =
2046-
tactic == DefinitiveListTactic::Vertical && is_every_args_simple(args);
2047-
if is_simple_enough
2048-
&& FORMAT_LIKE_WHITELIST
2049-
.iter()
2050-
.find(|s| **s == callee_str)
2051-
.is_some()
2052-
{
2053-
let args_tactic = definitive_tactic(
2054-
&item_vec[1..],
2055-
ListTactic::HorizontalVertical,
2056-
Separator::Comma,
2057-
nested_shape.width,
2058-
);
2059-
tactic = if args_tactic == DefinitiveListTactic::Horizontal {
2060-
DefinitiveListTactic::FormatCall
2061-
} else {
2062-
default_tactic()
2063-
};
2064-
} else if is_simple_enough && item_vec.len() >= 2
2065-
&& WRITE_LIKE_WHITELIST
2066-
.iter()
2067-
.find(|s| **s == callee_str)
2068-
.is_some()
2069-
{
2045+
2046+
// For special-case macros, we may want to use different tactics.
2047+
let maybe_args_offset = maybe_get_args_offset(callee_str, args);
2048+
2049+
if tactic == DefinitiveListTactic::Vertical && maybe_args_offset.is_some() {
2050+
let args_offset = maybe_args_offset.unwrap();
20702051
let args_tactic = definitive_tactic(
2071-
&item_vec[2..],
2052+
&item_vec[args_offset..],
20722053
ListTactic::HorizontalVertical,
20732054
Separator::Comma,
20742055
nested_shape.width,
20752056
);
2076-
tactic = if args_tactic == DefinitiveListTactic::Horizontal {
2077-
DefinitiveListTactic::WriteCall
2078-
} else {
2079-
default_tactic()
2080-
};
2057+
2058+
// Every argument is simple and fits on a single line.
2059+
if args_tactic == DefinitiveListTactic::Horizontal {
2060+
tactic = if args_offset == 1 {
2061+
DefinitiveListTactic::FormatCall
2062+
} else {
2063+
DefinitiveListTactic::WriteCall
2064+
};
2065+
}
20812066
}
20822067
}
20832068
}
@@ -2111,6 +2096,29 @@ fn is_every_args_simple<T: ToExpr>(lists: &[&T]) -> bool {
21112096
.all(|arg| arg.to_expr().map_or(false, is_simple_arg))
21122097
}
21132098

2099+
/// In case special-case style is required, returns an offset from which we start horizontal layout.
2100+
fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<usize> {
2101+
if FORMAT_LIKE_WHITELIST
2102+
.iter()
2103+
.find(|s| **s == callee_str)
2104+
.is_some()
2105+
&& args.len() >= 1
2106+
&& is_every_args_simple(args)
2107+
{
2108+
Some(1)
2109+
} else if WRITE_LIKE_WHITELIST
2110+
.iter()
2111+
.find(|s| **s == callee_str)
2112+
.is_some()
2113+
&& args.len() >= 2
2114+
&& is_every_args_simple(args)
2115+
{
2116+
Some(2)
2117+
} else {
2118+
None
2119+
}
2120+
}
2121+
21142122
/// Returns a shape for the last argument which is going to be overflowed.
21152123
fn last_arg_shape<T>(
21162124
lists: &[&T],

0 commit comments

Comments
 (0)