mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 07:04:53 +00:00
Allow parenthesized content exceed configured line width (#7490)
This commit is contained in:
parent
5849a75223
commit
192463c2fb
13 changed files with 171 additions and 151 deletions
|
@ -1183,7 +1183,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
// line break should be printed as regular line break
|
||||
return Ok(Fits::Yes);
|
||||
}
|
||||
MeasureMode::AllLines => {
|
||||
MeasureMode::AllLines | MeasureMode::AllLinesAllowTextOverflow => {
|
||||
// Continue measuring on the next line
|
||||
self.state.line_width = 0;
|
||||
self.state.pending_indent = args.indention();
|
||||
|
@ -1354,9 +1354,11 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
}
|
||||
|
||||
FormatElement::Tag(StartLineSuffix { reserved_width }) => {
|
||||
self.state.line_width += reserved_width;
|
||||
if self.state.line_width > self.options().line_width.into() {
|
||||
return Ok(Fits::No);
|
||||
if *reserved_width > 0 {
|
||||
self.state.line_width += reserved_width;
|
||||
if self.state.line_width > self.options().line_width.into() {
|
||||
return Ok(Fits::No);
|
||||
}
|
||||
}
|
||||
self.queue.skip_content(TagKind::LineSuffix);
|
||||
self.state.has_line_suffix = true;
|
||||
|
@ -1370,32 +1372,42 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
condition,
|
||||
propagate_expand,
|
||||
})) => {
|
||||
let condition_met = match condition {
|
||||
Some(condition) => {
|
||||
let group_mode = match condition.group_id {
|
||||
Some(group_id) => self.group_modes().get_print_mode(group_id)?,
|
||||
None => args.mode(),
|
||||
match args.mode() {
|
||||
PrintMode::Expanded => {
|
||||
// As usual, nothing to measure
|
||||
self.stack.push(TagKind::FitsExpanded, args);
|
||||
}
|
||||
PrintMode::Flat => {
|
||||
let condition_met = match condition {
|
||||
Some(condition) => {
|
||||
let group_mode = match condition.group_id {
|
||||
Some(group_id) => {
|
||||
self.group_modes().get_print_mode(group_id)?
|
||||
}
|
||||
None => args.mode(),
|
||||
};
|
||||
|
||||
condition.mode == group_mode
|
||||
}
|
||||
None => true,
|
||||
};
|
||||
|
||||
condition.mode == group_mode
|
||||
}
|
||||
None => true,
|
||||
};
|
||||
if condition_met {
|
||||
// Measure in fully expanded mode and allow overflows
|
||||
self.stack.push(
|
||||
TagKind::FitsExpanded,
|
||||
args.with_measure_mode(MeasureMode::AllLinesAllowTextOverflow)
|
||||
.with_print_mode(PrintMode::Expanded),
|
||||
);
|
||||
} else {
|
||||
if propagate_expand.get() {
|
||||
return Ok(Fits::No);
|
||||
}
|
||||
|
||||
if condition_met {
|
||||
// Measure in fully expanded mode.
|
||||
self.stack.push(
|
||||
TagKind::FitsExpanded,
|
||||
args.with_print_mode(PrintMode::Expanded)
|
||||
.with_measure_mode(MeasureMode::AllLines),
|
||||
);
|
||||
} else {
|
||||
if propagate_expand.get() && args.mode().is_flat() {
|
||||
return Ok(Fits::No);
|
||||
// As usual
|
||||
self.stack.push(TagKind::FitsExpanded, args);
|
||||
}
|
||||
}
|
||||
|
||||
// As usual
|
||||
self.stack.push(TagKind::FitsExpanded, args);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1482,7 +1494,8 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
}
|
||||
match args.measure_mode() {
|
||||
MeasureMode::FirstLine => return Fits::Yes,
|
||||
MeasureMode::AllLines => {
|
||||
MeasureMode::AllLines
|
||||
| MeasureMode::AllLinesAllowTextOverflow => {
|
||||
self.state.line_width = 0;
|
||||
continue;
|
||||
}
|
||||
|
@ -1498,7 +1511,9 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
}
|
||||
}
|
||||
|
||||
if self.state.line_width > self.options().line_width.into() {
|
||||
if self.state.line_width > self.options().line_width.into()
|
||||
&& !args.measure_mode().allows_text_overflow()
|
||||
{
|
||||
return Fits::No;
|
||||
}
|
||||
|
||||
|
@ -1601,6 +1616,17 @@ enum MeasureMode {
|
|||
/// The content only fits if none of the lines exceed the print width. Lines are terminated by either
|
||||
/// a hard line break or a soft line break in [`PrintMode::Expanded`].
|
||||
AllLines,
|
||||
|
||||
/// Measures all lines and allows lines to exceed the configured line width. Useful when it only matters
|
||||
/// whether the content *before* and *after* fits.
|
||||
AllLinesAllowTextOverflow,
|
||||
}
|
||||
|
||||
impl MeasureMode {
|
||||
/// Returns `true` if this mode allows text exceeding the configured line width.
|
||||
const fn allows_text_overflow(self) -> bool {
|
||||
matches!(self, MeasureMode::AllLinesAllowTextOverflow)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BestFittingMode> for MeasureMode {
|
||||
|
|
|
@ -120,7 +120,6 @@ impl SourceMapGeneration {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum LineEnding {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue