mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +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
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue