Skip to content

Commit 24f1f69

Browse files
authored
Merge pull request #2236 from topecongiro/simple-array
Compress an array whose items are all 'simple'
2 parents 7752463 + 1684df6 commit 24f1f69

File tree

4 files changed

+57
-146
lines changed

4 files changed

+57
-146
lines changed

src/expr.rs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -436,38 +436,7 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
436436
}
437437
}
438438

439-
let has_long_item = items
440-
.iter()
441-
.any(|li| li.item.as_ref().map(|s| s.len() > 10).unwrap_or(false));
442-
443-
let tactic = match context.config.indent_style() {
444-
IndentStyle::Block => {
445-
// FIXME wrong shape in one-line case
446-
match shape.width.checked_sub(2 * bracket_size) {
447-
Some(width) => {
448-
let tactic = ListTactic::LimitedHorizontalVertical(
449-
context.config.width_heuristics().array_width,
450-
);
451-
definitive_tactic(&items, tactic, Separator::Comma, width)
452-
}
453-
None => DefinitiveListTactic::Vertical,
454-
}
455-
}
456-
IndentStyle::Visual => {
457-
if has_long_item || items.iter().any(ListItem::is_multiline) {
458-
definitive_tactic(
459-
&items,
460-
ListTactic::LimitedHorizontalVertical(
461-
context.config.width_heuristics().array_width,
462-
),
463-
Separator::Comma,
464-
nested_shape.width,
465-
)
466-
} else {
467-
DefinitiveListTactic::Mixed
468-
}
469-
}
470-
};
439+
let tactic = array_tactic(context, shape, nested_shape, exprs, &items, bracket_size);
471440
let ends_with_newline = tactic.ends_with_newline(context.config.indent_style());
472441

473442
let fmt = ListFormatting {
@@ -518,6 +487,54 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
518487
Some(result)
519488
}
520489

490+
fn array_tactic<T: Rewrite + Spanned + ToExpr>(
491+
context: &RewriteContext,
492+
shape: Shape,
493+
nested_shape: Shape,
494+
exprs: &[&T],
495+
items: &[ListItem],
496+
bracket_size: usize,
497+
) -> DefinitiveListTactic {
498+
let has_long_item = items
499+
.iter()
500+
.any(|li| li.item.as_ref().map(|s| s.len() > 10).unwrap_or(false));
501+
502+
match context.config.indent_style() {
503+
IndentStyle::Block => {
504+
let tactic = match shape.width.checked_sub(2 * bracket_size) {
505+
Some(width) => {
506+
let tactic = ListTactic::LimitedHorizontalVertical(
507+
context.config.width_heuristics().array_width,
508+
);
509+
definitive_tactic(items, tactic, Separator::Comma, width)
510+
}
511+
None => DefinitiveListTactic::Vertical,
512+
};
513+
if tactic == DefinitiveListTactic::Vertical && !has_long_item
514+
&& is_every_args_simple(exprs)
515+
{
516+
DefinitiveListTactic::Mixed
517+
} else {
518+
tactic
519+
}
520+
}
521+
IndentStyle::Visual => {
522+
if has_long_item || items.iter().any(ListItem::is_multiline) {
523+
definitive_tactic(
524+
items,
525+
ListTactic::LimitedHorizontalVertical(
526+
context.config.width_heuristics().array_width,
527+
),
528+
Separator::Comma,
529+
nested_shape.width,
530+
)
531+
} else {
532+
DefinitiveListTactic::Mixed
533+
}
534+
}
535+
}
536+
}
537+
521538
fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
522539
debug!("nop_block_collapse {:?} {}", block_str, budget);
523540
block_str.map(|block_str| {

tests/target/expr-block.rs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,8 @@ fn arrays() {
4141

4242
fn arrays() {
4343
let x = [
44-
0,
45-
1,
46-
2,
47-
3,
48-
4,
49-
5,
50-
6,
51-
7,
52-
8,
53-
9,
54-
0,
55-
1,
56-
2,
57-
3,
58-
4,
59-
5,
60-
6,
61-
7,
62-
8,
63-
9,
64-
0,
65-
7,
66-
8,
67-
9,
68-
0,
69-
1,
70-
2,
71-
3,
72-
4,
73-
5,
74-
6,
75-
7,
76-
8,
77-
9,
78-
0,
44+
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,
45+
6, 7, 8, 9, 0,
7946
];
8047

8148
let y = [/* comment */ 1, 2 /* post comment */, 3];

tests/target/expr.rs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -171,41 +171,8 @@ fn issue184(source: &str) {
171171

172172
fn arrays() {
173173
let x = [
174-
0,
175-
1,
176-
2,
177-
3,
178-
4,
179-
5,
180-
6,
181-
7,
182-
8,
183-
9,
184-
0,
185-
1,
186-
2,
187-
3,
188-
4,
189-
5,
190-
6,
191-
7,
192-
8,
193-
9,
194-
0,
195-
7,
196-
8,
197-
9,
198-
0,
199-
1,
200-
2,
201-
3,
202-
4,
203-
5,
204-
6,
205-
7,
206-
8,
207-
9,
208-
0,
174+
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,
175+
6, 7, 8, 9, 0,
209176
];
210177

211178
let y = [/* comment */ 1, 2 /* post comment */, 3];

tests/target/static.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,10 @@ const FILE_GENERIC_READ: DWORD =
22
STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
33

44
static boolnames: &'static [&'static str] = &[
5-
"bw",
6-
"am",
7-
"xsb",
8-
"xhp",
9-
"xenl",
10-
"eo",
11-
"gn",
12-
"hc",
13-
"km",
14-
"hs",
15-
"in",
16-
"db",
17-
"da",
18-
"mir",
19-
"msgr",
20-
"os",
21-
"eslok",
22-
"xt",
23-
"hz",
24-
"ul",
25-
"xon",
26-
"nxon",
27-
"mc5i",
28-
"chts",
29-
"nrrmc",
30-
"npc",
31-
"ndscr",
32-
"ccc",
33-
"bce",
34-
"hls",
35-
"xhpa",
36-
"crxm",
37-
"daisy",
38-
"xvpa",
39-
"sam",
40-
"cpix",
41-
"lpix",
42-
"OTbs",
43-
"OTns",
44-
"OTnc",
45-
"OTMT",
46-
"OTNL",
47-
"OTpt",
48-
"OTxr",
5+
"bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc", "km", "hs", "in", "db", "da", "mir",
6+
"msgr", "os", "eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i", "chts", "nrrmc", "npc",
7+
"ndscr", "ccc", "bce", "hls", "xhpa", "crxm", "daisy", "xvpa", "sam", "cpix", "lpix", "OTbs",
8+
"OTns", "OTnc", "OTMT", "OTNL", "OTpt", "OTxr",
499
];
5010

5111
static mut name: SomeType =

0 commit comments

Comments
 (0)