Add optimized best_fit_parenthesize IR (#7475)

This commit is contained in:
Micha Reiser 2023-09-19 08:29:05 +02:00 committed by GitHub
parent 28b48ab902
commit 6a4dbd622b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 413 additions and 96 deletions

View file

@ -40,6 +40,9 @@ impl Document {
expands_before: bool,
},
BestFitting,
BestFitParenthesize {
expanded: bool,
},
}
fn expand_parent(enclosing: &[Enclosing]) {
@ -67,6 +70,18 @@ impl Document {
Some(Enclosing::Group(group)) => !group.mode().is_flat(),
_ => false,
},
FormatElement::Tag(Tag::StartBestFitParenthesize { .. }) => {
enclosing.push(Enclosing::BestFitParenthesize { expanded: expands });
expands = false;
continue;
}
FormatElement::Tag(Tag::EndBestFitParenthesize) => {
if let Some(Enclosing::BestFitParenthesize { expanded }) = enclosing.pop() {
expands = expanded;
}
continue;
}
FormatElement::Tag(Tag::StartConditionalGroup(group)) => {
enclosing.push(Enclosing::ConditionalGroup(group));
false
@ -503,6 +518,21 @@ impl Format<IrFormatContext<'_>> for &[FormatElement] {
}
}
StartBestFitParenthesize { id } => {
write!(f, [token("best_fit_parenthesize(")])?;
if let Some(group_id) = id {
write!(
f,
[
text(&std::format!("\"{group_id:?}\""), None),
token(","),
space(),
]
)?;
}
}
StartConditionalGroup(group) => {
write!(
f,
@ -611,6 +641,7 @@ impl Format<IrFormatContext<'_>> for &[FormatElement] {
| EndIndent
| EndGroup
| EndConditionalGroup
| EndBestFitParenthesize
| EndLineSuffix
| EndDedent
| EndFitsExpanded