Merge pull request #2236 from topecongiro/simple-array

Compress an array whose items are all 'simple'
This commit is contained in:
Nick Cameron 2017-12-04 17:33:42 +13:00 committed by GitHub
commit 24f1f69d5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 146 deletions

View File

@ -436,38 +436,7 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
}
}
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,54 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
Some(result)
}
fn array_tactic<T: Rewrite + Spanned + ToExpr>(
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 => {
let tactic = 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,
};
if tactic == DefinitiveListTactic::Vertical && !has_long_item
&& is_every_args_simple(exprs)
{
DefinitiveListTactic::Mixed
} else {
tactic
}
}
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<String>, budget: usize) -> Option<String> {
debug!("nop_block_collapse {:?} {}", block_str, budget);
block_str.map(|block_str| {

View File

@ -41,41 +41,8 @@ fn arrays() {
fn arrays() {
let x = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0,
7,
8,
9,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 0,
];
let y = [/* comment */ 1, 2 /* post comment */, 3];

View File

@ -171,41 +171,8 @@ fn issue184(source: &str) {
fn arrays() {
let x = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0,
7,
8,
9,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 0,
];
let y = [/* comment */ 1, 2 /* post comment */, 3];

View File

@ -2,50 +2,10 @@ const FILE_GENERIC_READ: DWORD =
STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
static boolnames: &'static [&'static str] = &[
"bw",
"am",
"xsb",
"xhp",
"xenl",
"eo",
"gn",
"hc",
"km",
"hs",
"in",
"db",
"da",
"mir",
"msgr",
"os",
"eslok",
"xt",
"hz",
"ul",
"xon",
"nxon",
"mc5i",
"chts",
"nrrmc",
"npc",
"ndscr",
"ccc",
"bce",
"hls",
"xhpa",
"crxm",
"daisy",
"xvpa",
"sam",
"cpix",
"lpix",
"OTbs",
"OTns",
"OTnc",
"OTMT",
"OTNL",
"OTpt",
"OTxr",
"bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc", "km", "hs", "in", "db", "da", "mir",
"msgr", "os", "eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i", "chts", "nrrmc", "npc",
"ndscr", "ccc", "bce", "hls", "xhpa", "crxm", "daisy", "xvpa", "sam", "cpix", "lpix", "OTbs",
"OTns", "OTnc", "OTMT", "OTNL", "OTpt", "OTxr",
];
static mut name: SomeType =