mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
Add optimized best_fit_parenthesize
IR (#7475)
This commit is contained in:
parent
28b48ab902
commit
6a4dbd622b
11 changed files with 413 additions and 96 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue