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

View file

@ -86,6 +86,13 @@ pub enum Tag {
StartBestFittingEntry,
EndBestFittingEntry,
/// Parenthesizes the content but only if adding the parentheses and indenting the content
/// makes the content fit in the configured line width.
StartBestFitParenthesize {
id: Option<GroupId>,
},
EndBestFitParenthesize,
}
impl Tag {
@ -102,11 +109,12 @@ impl Tag {
| Tag::StartIndentIfGroupBreaks(_)
| Tag::StartFill
| Tag::StartEntry
| Tag::StartLineSuffix { reserved_width: _ }
| Tag::StartLineSuffix { .. }
| Tag::StartVerbatim(_)
| Tag::StartLabelled(_)
| Tag::StartFitsExpanded(_)
| Tag::StartBestFittingEntry,
| Tag::StartBestFittingEntry
| Tag::StartBestFitParenthesize { .. }
)
}
@ -134,6 +142,9 @@ impl Tag {
StartLabelled(_) | EndLabelled => TagKind::Labelled,
StartFitsExpanded { .. } | EndFitsExpanded => TagKind::FitsExpanded,
StartBestFittingEntry { .. } | EndBestFittingEntry => TagKind::BestFittingEntry,
StartBestFitParenthesize { .. } | EndBestFitParenthesize => {
TagKind::BestFitParenthesize
}
}
}
}
@ -158,6 +169,7 @@ pub enum TagKind {
Labelled,
FitsExpanded,
BestFittingEntry,
BestFitParenthesize,
}
#[derive(Debug, Copy, Default, Clone, Eq, PartialEq)]