Skip to content

Commit fba6355

Browse files
giacomocavalierilpil
authored andcommitted
format bit arrays the same as lists
1 parent b2f4228 commit fba6355

File tree

3 files changed

+352
-46
lines changed

3 files changed

+352
-46
lines changed

compiler-core/src/ast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,12 @@ impl Endianness {
28462846
}
28472847
}
28482848

2849+
impl<Value, Type> HasLocation for BitArraySegment<Value, Type> {
2850+
fn location(&self) -> SrcSpan {
2851+
self.location
2852+
}
2853+
}
2854+
28492855
impl<Type> BitArraySegment<Pattern<Type>, Type> {
28502856
/// Returns the value of the pattern unwrapping any assign pattern.
28512857
///

compiler-core/src/format.rs

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,14 @@ impl<'comments> Formatter<'comments> {
481481
.map(|segment| bit_array_segment(segment, |e| self.const_expr(e)))
482482
.collect_vec();
483483

484-
self.bit_array(
485-
segment_docs,
486-
segments
487-
.iter()
488-
.all(|s| s.value.can_have_multiple_per_line()),
489-
location,
490-
)
484+
let packing = self.items_sequence_packing(
485+
segments,
486+
None,
487+
|segment| segment.value.can_have_multiple_per_line(),
488+
*location,
489+
);
490+
491+
self.bit_array(segment_docs, packing, location)
491492
}
492493

493494
Constant::Record {
@@ -586,17 +587,15 @@ impl<'comments> Formatter<'comments> {
586587
};
587588
}
588589

589-
let list_packing = self.list_items_packing(
590+
let list_packing = self.items_sequence_packing(
590591
elements,
591592
None,
592593
|element| element.can_have_multiple_per_line(),
593594
*location,
594595
);
595596
let comma = match list_packing {
596-
ListItemsPacking::FitMultiplePerLine => flex_break(",", ", "),
597-
ListItemsPacking::FitOnePerLine | ListItemsPacking::BreakOnePerLine => {
598-
break_(",", ", ")
599-
}
597+
ItemsPacking::FitMultiplePerLine => flex_break(",", ", "),
598+
ItemsPacking::FitOnePerLine | ItemsPacking::BreakOnePerLine => break_(",", ", "),
600599
};
601600

602601
let mut elements_doc = nil();
@@ -645,8 +644,8 @@ impl<'comments> Formatter<'comments> {
645644
};
646645

647646
match list_packing {
648-
ListItemsPacking::FitOnePerLine | ListItemsPacking::FitMultiplePerLine => doc.group(),
649-
ListItemsPacking::BreakOnePerLine => doc.force_break(),
647+
ItemsPacking::FitOnePerLine | ItemsPacking::FitMultiplePerLine => doc.group(),
648+
ItemsPacking::BreakOnePerLine => doc.force_break(),
650649
}
651650
}
652651

@@ -1084,13 +1083,14 @@ impl<'comments> Formatter<'comments> {
10841083
.map(|segment| bit_array_segment(segment, |e| self.bit_array_segment_expr(e)))
10851084
.collect_vec();
10861085

1087-
self.bit_array(
1088-
segment_docs,
1089-
segments
1090-
.iter()
1091-
.all(|s| s.value.can_have_multiple_per_line()),
1092-
location,
1093-
)
1086+
let packing = self.items_sequence_packing(
1087+
segments,
1088+
None,
1089+
|segment| segment.value.can_have_multiple_per_line(),
1090+
*location,
1091+
);
1092+
1093+
self.bit_array(segment_docs, packing, location)
10941094
}
10951095
UntypedExpr::RecordUpdate {
10961096
constructor,
@@ -2028,18 +2028,16 @@ impl<'comments> Formatter<'comments> {
20282028
};
20292029
}
20302030

2031-
let list_packing = self.list_items_packing(
2031+
let list_packing = self.items_sequence_packing(
20322032
elements,
20332033
tail,
20342034
UntypedExpr::can_have_multiple_per_line,
20352035
*location,
20362036
);
20372037

20382038
let comma = match list_packing {
2039-
ListItemsPacking::FitMultiplePerLine => flex_break(",", ", "),
2040-
ListItemsPacking::FitOnePerLine | ListItemsPacking::BreakOnePerLine => {
2041-
break_(",", ", ")
2042-
}
2039+
ItemsPacking::FitMultiplePerLine => flex_break(",", ", "),
2040+
ItemsPacking::FitOnePerLine | ItemsPacking::BreakOnePerLine => break_(",", ", "),
20432041
};
20442042

20452043
let list_size = elements.len()
@@ -2110,18 +2108,18 @@ impl<'comments> Formatter<'comments> {
21102108
};
21112109

21122110
match list_packing {
2113-
ListItemsPacking::FitOnePerLine | ListItemsPacking::FitMultiplePerLine => doc.group(),
2114-
ListItemsPacking::BreakOnePerLine => doc.force_break(),
2111+
ItemsPacking::FitOnePerLine | ItemsPacking::FitMultiplePerLine => doc.group(),
2112+
ItemsPacking::BreakOnePerLine => doc.force_break(),
21152113
}
21162114
}
21172115

2118-
fn list_items_packing<'a, T: HasLocation>(
2116+
fn items_sequence_packing<'a, T: HasLocation>(
21192117
&self,
21202118
items: &'a [T],
21212119
tail: Option<&'a T>,
21222120
can_have_multiple_per_line: impl Fn(&'a T) -> bool,
21232121
list_location: SrcSpan,
2124-
) -> ListItemsPacking {
2122+
) -> ItemsPacking {
21252123
let ends_with_trailing_comma = tail
21262124
.map(|tail| tail.location().end)
21272125
.or_else(|| items.last().map(|last| last.location().end))
@@ -2143,12 +2141,12 @@ impl<'comments> Formatter<'comments> {
21432141
// If there's any empty line between elements we want to force each
21442142
// item onto its own line to preserve the empty lines that were
21452143
// intentionally added.
2146-
ListItemsPacking::BreakOnePerLine
2144+
ItemsPacking::BreakOnePerLine
21472145
} else if !ends_with_trailing_comma {
21482146
// If the list doesn't end with a trailing comma we try and pack it in
21492147
// a single line; if we can't we'll put one item per line, no matter
21502148
// the content of the list.
2151-
ListItemsPacking::FitOnePerLine
2149+
ItemsPacking::FitOnePerLine
21522150
} else if tail.is_none()
21532151
&& items.iter().all(can_have_multiple_per_line)
21542152
&& has_multiple_elements_per_line
@@ -2183,11 +2181,11 @@ impl<'comments> Formatter<'comments> {
21832181
// The first item is broken, meaning that once we get to the flex
21842182
// space separating it from the following one the formatter is not
21852183
// going to break it since there's enough space in the current line!
2186-
ListItemsPacking::FitMultiplePerLine
2184+
ItemsPacking::FitMultiplePerLine
21872185
} else {
21882186
// If it ends with a trailing comma we will force the list on
21892187
// multiple lines, with one item per line.
2190-
ListItemsPacking::BreakOnePerLine
2188+
ItemsPacking::BreakOnePerLine
21912189
}
21922190
}
21932191

@@ -2288,7 +2286,7 @@ impl<'comments> Formatter<'comments> {
22882286
.map(|segment| bit_array_segment(segment, |pattern| self.pattern(pattern)))
22892287
.collect_vec();
22902288

2291-
self.bit_array(segment_docs, false, location)
2289+
self.bit_array(segment_docs, ItemsPacking::FitOnePerLine, location)
22922290
}
22932291

22942292
Pattern::StringPrefix {
@@ -2588,7 +2586,7 @@ impl<'comments> Formatter<'comments> {
25882586
fn bit_array<'a>(
25892587
&mut self,
25902588
segments: Vec<Document<'a>>,
2591-
can_have_multiple_per_line: bool,
2589+
packing: ItemsPacking,
25922590
location: &SrcSpan,
25932591
) -> Document<'a> {
25942592
let comments = self.pop_comments(location.end);
@@ -2614,19 +2612,19 @@ impl<'comments> Formatter<'comments> {
26142612
.force_break(),
26152613
};
26162614
}
2617-
let comma = if can_have_multiple_per_line {
2618-
flex_break(",", ", ")
2619-
} else {
2620-
break_(",", ", ")
2615+
2616+
let comma = match packing {
2617+
ItemsPacking::FitMultiplePerLine => flex_break(",", ", "),
2618+
ItemsPacking::FitOnePerLine | ItemsPacking::BreakOnePerLine => break_(",", ", "),
26212619
};
26222620

26232621
let last_break = break_(",", "");
26242622
let doc = break_("<<", "<<")
26252623
.append(join(segments, comma))
26262624
.nest(INDENT);
26272625

2628-
match comments_doc {
2629-
None => doc.append(last_break).append(">>").group(),
2626+
let doc = match comments_doc {
2627+
None => doc.append(last_break).append(">>"),
26302628
Some(comments) => doc
26312629
.append(last_break.nest(INDENT))
26322630
// ^ Notice how in this case we nest the final break before
@@ -2635,8 +2633,12 @@ impl<'comments> Formatter<'comments> {
26352633
.append(comments.nest(INDENT))
26362634
.append(line())
26372635
.append(">>")
2638-
.force_break()
2639-
.group(),
2636+
.force_break(),
2637+
};
2638+
2639+
match packing {
2640+
ItemsPacking::FitOnePerLine | ItemsPacking::FitMultiplePerLine => doc.group(),
2641+
ItemsPacking::BreakOnePerLine => doc.force_break(),
26402642
}
26412643
}
26422644

@@ -3059,7 +3061,10 @@ impl<'a> Documentable<'a> for &'a BinOp {
30593061
}
30603062

30613063
#[allow(clippy::enum_variant_names)]
3062-
enum ListItemsPacking {
3064+
/// This is used to determine how to fit the items of a list, or the segments of
3065+
/// a bit array in a line.
3066+
///
3067+
enum ItemsPacking {
30633068
/// Try and fit everything on a single line; if the items don't fit, break
30643069
/// the list putting each item into its own line.
30653070
///

0 commit comments

Comments
 (0)