Add BestFittingMode (#5184)

## Summary
Black supports for layouts when it comes to breaking binary expressions:

```rust
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum BinaryLayout {
    /// Put each operand on their own line if either side expands
    Default,

    /// Try to expand the left to make it fit. Add parentheses if the left or right don't fit.
    ///
    ///```python
    /// [
    ///     a,
    ///     b
    /// ] & c
    ///```
    ExpandLeft,

    /// Try to expand the right to make it fix. Add parentheses if the left or right don't fit.
    ///
    /// ```python
    /// a & [
    ///     b,
    ///     c
    /// ]
    /// ```
    ExpandRight,

    /// Both the left and right side can be expanded. Try in the following order:
    /// * expand the right side
    /// * expand the left side
    /// * expand both sides
    ///
    /// to make the expression fit
    ///
    /// ```python
    /// [
    ///     a,
    ///     b
    /// ] & [
    ///     c,
    ///     d
    /// ]
    /// ```
    ExpandRightThenLeft,
}
```

Our current implementation only handles `ExpandRight` and `Default` correctly. `ExpandLeft` turns out to be surprisingly hard. This PR adds a new `BestFittingMode` parameter to `BestFitting` to support `ExpandLeft`.

There are 3 variants that `ExpandLeft` must support:

**Variant 1**: Everything fits on the line (easy)

```python
[a, b] + c
```

**Variant 2**: Left breaks, but right fits on the line. Doesn't need parentheses

```python
[
	a,
	b
] + c
```

**Variant 3**: The left breaks, but there's still not enough space for the right hand side. Parenthesize the whole expression:

```python
(
	[
		a, 
		b
	]
	+ ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
)
```

Solving Variant 1 and 2 on their own is straightforward The printer gives us this behavior by nesting right inside of the group of left:

```
group(&format_args![
	if_group_breaks(&text("(")),
	soft_block_indent(&group(&format_args![
		left, 
		soft_line_break_or_space(), 
		op, 
		space(), 
		group(&right)
	])),
	if_group_breaks(&text(")"))
])
```

The fundamental problem is that the outer group, which adds the parentheses, always breaks if the left side breaks. That means, we end up with

```python
(
	[
		a,
		b
	] + c
)
```

which is not what we want (we only want parentheses if the right side doesn't fit). 

Okay, so nesting groups don't work because of the outer parentheses. Sequencing groups doesn't work because it results in a right-to-left breaking which is the opposite of what we want. 

Could we use best fitting? Almost! 

```
best_fitting![
	// All flat
	format_args![left, space(), op, space(), right],
	// Break left
	format_args!(group(&left).should_expand(true), space(), op, space(), right],
	// Break all
	format_args![
		text("("), 
		block_indent!(&format_args![
			left, 
			hard_line_break(), 
			op,
			space()
			right
		])
	]
]
```

I hope I managed to write this up correctly. The problem is that the printer never reaches the 3rd variant because the second variant always fits:

* The `group(&left).should_expand(true)` changes the group so that all `soft_line_breaks` are turned into hard line breaks. This is necessary because we want to test if the content fits if we break after the `[`. 
* Now, the whole idea of `best_fitting` is that you can pretend that some content fits on the line when it actually does not. The way this works is that the printer **only** tests if all the content of the variant **up to** the first line break fits on the line (we insert that line break by using `should_expand(true))`. The printer doesn't care whether the rest `a\n, b\n ] + c` all fits on (multiple?) lines. 

Why does breaking right work but not breaking the left? The difference is that we can make the decision whether to parenthesis the expression based on the left expression. We can't do this for breaking left because the decision whether to insert parentheses or not would depend on a lookahead: will the right side break. We simply don't know this yet when printing the parentheses (it would work for the right parentheses but not for the left and indent).

What we kind of want here is to tell the printer: Look, what comes here may or may not fit on a single line but we don't care. Simply test that what comes **after** fits on a line. 

This PR adds a new `BestFittingMode` that has a new `AllLines` option that gives us the desired behavior of testing all content and not just up to the first line break. 

## Test Plan

I added a new example to  `BestFitting::with_mode`
This commit is contained in:
Micha Reiser 2023-06-20 18:16:01 +02:00 committed by GitHub
parent 6929fcc55f
commit d9e59b21cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 329 additions and 120 deletions

View file

@ -1,7 +1,7 @@
use crate::format_element::tag::TagKind;
use crate::format_element::PrintMode;
use crate::printer::stack::{Stack, StackedStack};
use crate::printer::Indention;
use crate::printer::{Indention, MeasureMode};
use crate::{IndentStyle, InvalidDocumentError, PrintError, PrintResult};
use std::fmt::Debug;
use std::num::NonZeroU8;
@ -28,6 +28,7 @@ pub(super) struct StackFrame {
pub(super) struct PrintElementArgs {
indent: Indention,
mode: PrintMode,
measure_mode: MeasureMode,
}
impl PrintElementArgs {
@ -42,6 +43,10 @@ impl PrintElementArgs {
self.mode
}
pub(super) fn measure_mode(&self) -> MeasureMode {
self.measure_mode
}
pub(super) fn indention(&self) -> Indention {
self.indent
}
@ -70,6 +75,11 @@ impl PrintElementArgs {
self.mode = mode;
self
}
pub(crate) fn with_measure_mode(mut self, mode: MeasureMode) -> Self {
self.measure_mode = mode;
self
}
}
impl Default for PrintElementArgs {
@ -77,6 +87,7 @@ impl Default for PrintElementArgs {
Self {
indent: Indention::Level(0),
mode: PrintMode::Expanded,
measure_mode: MeasureMode::FirstLine,
}
}
}

View file

@ -4,18 +4,10 @@ mod printer_options;
mod queue;
mod stack;
pub use printer_options::*;
use crate::format_element::{BestFitting, LineMode, PrintMode};
use crate::{
ActualStart, FormatElement, GroupId, IndentStyle, InvalidDocumentError, PrintError,
PrintResult, Printed, SourceMarker, TextRange,
};
use crate::format_element::document::Document;
use crate::format_element::tag::Condition;
use crate::format_element::tag::{Condition, GroupMode};
use crate::format_element::{BestFittingMode, BestFittingVariants, LineMode, PrintMode};
use crate::prelude::tag::{DedentMode, Tag, TagKind, VerbatimKind};
use crate::prelude::Tag::EndFill;
use crate::printer::call_stack::{
CallStack, FitsCallStack, PrintCallStack, PrintElementArgs, StackFrame,
};
@ -24,7 +16,12 @@ use crate::printer::queue::{
AllPredicate, FitsEndPredicate, FitsQueue, PrintQueue, Queue, SingleEntryPredicate,
};
use crate::source_code::SourceCode;
use crate::{
ActualStart, FormatElement, GroupId, IndentStyle, InvalidDocumentError, PrintError,
PrintResult, Printed, SourceMarker, TextRange,
};
use drop_bomb::DebugDropBomb;
pub use printer_options::*;
use ruff_text_size::{TextLen, TextSize};
use std::num::NonZeroU8;
use unicode_width::UnicodeWidthChar;
@ -137,8 +134,8 @@ impl<'a> Printer<'a> {
self.flush_line_suffixes(queue, stack, Some(HARD_BREAK));
}
FormatElement::BestFitting(best_fitting) => {
self.print_best_fitting(best_fitting, queue, stack)?;
FormatElement::BestFitting { variants, mode } => {
self.print_best_fitting(variants, *mode, queue, stack)?;
}
FormatElement::Interned(content) => {
@ -146,30 +143,31 @@ impl<'a> Printer<'a> {
}
FormatElement::Tag(StartGroup(group)) => {
let group_mode = if !group.mode().is_flat() {
PrintMode::Expanded
} else {
match args.mode() {
PrintMode::Flat if self.state.measured_group_fits => {
// A parent group has already verified that this group fits on a single line
// Thus, just continue in flat mode
PrintMode::Flat
}
// The printer is either in expanded mode or it's necessary to re-measure if the group fits
// because the printer printed a line break
_ => {
self.state.measured_group_fits = true;
// Measure to see if the group fits up on a single line. If that's the case,
// print the group in "flat" mode, otherwise continue in expanded mode
stack.push(TagKind::Group, args.with_print_mode(PrintMode::Flat));
let fits = self.fits(queue, stack)?;
stack.pop(TagKind::Group)?;
if fits {
let group_mode = match group.mode() {
GroupMode::Expand | GroupMode::Propagated => PrintMode::Expanded,
GroupMode::Flat => {
match args.mode() {
PrintMode::Flat if self.state.measured_group_fits => {
// A parent group has already verified that this group fits on a single line
// Thus, just continue in flat mode
PrintMode::Flat
} else {
PrintMode::Expanded
}
// The printer is either in expanded mode or it's necessary to re-measure if the group fits
// because the printer printed a line break
_ => {
self.state.measured_group_fits = true;
// Measure to see if the group fits up on a single line. If that's the case,
// print the group in "flat" mode, otherwise continue in expanded mode
stack.push(TagKind::Group, args.with_print_mode(PrintMode::Flat));
let fits = self.fits(queue, stack)?;
stack.pop(TagKind::Group)?;
if fits {
PrintMode::Flat
} else {
PrintMode::Expanded
}
}
}
}
@ -211,10 +209,10 @@ impl<'a> Printer<'a> {
Some(id) => self.state.group_modes.unwrap_print_mode(*id, element),
};
if group_mode != *mode {
queue.skip_content(TagKind::ConditionalContent);
} else {
if *mode == group_mode {
stack.push(TagKind::ConditionalContent, args);
} else {
queue.skip_content(TagKind::ConditionalContent);
}
}
@ -249,6 +247,7 @@ impl<'a> Printer<'a> {
FormatElement::Tag(tag @ (StartLabelled(_) | StartEntry)) => {
stack.push(tag.kind(), args);
}
FormatElement::Tag(
tag @ (EndLabelled
| EndEntry
@ -371,19 +370,19 @@ impl<'a> Printer<'a> {
fn print_best_fitting(
&mut self,
best_fitting: &'a BestFitting,
variants: &'a BestFittingVariants,
mode: BestFittingMode,
queue: &mut PrintQueue<'a>,
stack: &mut PrintCallStack,
) -> PrintResult<()> {
let args = stack.top();
if args.mode().is_flat() && self.state.measured_group_fits {
queue.extend_back(best_fitting.most_flat());
queue.extend_back(variants.most_flat());
self.print_entry(queue, stack, args)
} else {
self.state.measured_group_fits = true;
let normal_variants = &best_fitting.variants()[..best_fitting.variants().len() - 1];
let normal_variants = &variants[..variants.len() - 1];
for variant in normal_variants.iter() {
// Test if this variant fits and if so, use it. Otherwise try the next
@ -394,12 +393,14 @@ impl<'a> Printer<'a> {
return invalid_start_tag(TagKind::Entry, variant.first());
}
let entry_args = args.with_print_mode(PrintMode::Flat);
// Skip the first element because we want to override the args for the entry and the
// args must be popped from the stack as soon as it sees the matching end entry.
let content = &variant[1..];
let entry_args = args
.with_print_mode(PrintMode::Flat)
.with_measure_mode(MeasureMode::from(mode));
queue.extend_back(content);
stack.push(TagKind::Entry, entry_args);
let variant_fits = self.fits(queue, stack)?;
@ -411,12 +412,12 @@ impl<'a> Printer<'a> {
if variant_fits {
queue.extend_back(variant);
return self.print_entry(queue, stack, entry_args);
return self.print_entry(queue, stack, args.with_print_mode(PrintMode::Flat));
}
}
// No variant fits, take the last (most expanded) as fallback
let most_expanded = best_fitting.most_expanded();
let most_expanded = variants.most_expanded();
queue.extend_back(most_expanded);
self.print_entry(queue, stack, args.with_print_mode(PrintMode::Expanded))
}
@ -555,7 +556,7 @@ impl<'a> Printer<'a> {
}
}
if queue.top() == Some(&FormatElement::Tag(EndFill)) {
if queue.top() == Some(&FormatElement::Tag(Tag::EndFill)) {
Ok(())
} else {
invalid_end_tag(TagKind::Fill, stack.top_kind())
@ -959,8 +960,8 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
FormatElement::Space => return Ok(self.fits_text(" ")),
FormatElement::Line(line_mode) => {
if args.mode().is_flat() {
match line_mode {
match args.mode() {
PrintMode::Flat => match line_mode {
LineMode::SoftOrSpace => return Ok(self.fits_text(" ")),
LineMode::Soft => {}
LineMode::Hard | LineMode::Empty => {
@ -970,13 +971,22 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
Fits::Yes
});
}
},
PrintMode::Expanded => {
match args.measure_mode() {
MeasureMode::FirstLine => {
// Reachable if the restQueue contains an element with mode expanded because Expanded
// is what the mode's initialized to by default
// This means, the printer is outside of the current element at this point and any
// line break should be printed as regular line break
return Ok(Fits::Yes);
}
MeasureMode::AllLines => {
// Continue measuring on the next line
self.state.line_width = 0;
}
}
}
} else {
// Reachable if the restQueue contains an element with mode expanded because Expanded
// is what the mode's initialized to by default
// This means, the printer is outside of the current element at this point and any
// line break should be printed as regular line break -> Fits
return Ok(Fits::Yes);
}
}
@ -1000,17 +1010,21 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
FormatElement::SourcePosition(_) => {}
FormatElement::BestFitting(best_fitting) => {
let slice = match args.mode() {
PrintMode::Flat => best_fitting.most_flat(),
PrintMode::Expanded => best_fitting.most_expanded(),
FormatElement::BestFitting { variants, mode } => {
let (slice, args) = match args.mode() {
PrintMode::Flat => (
variants.most_flat(),
args.with_measure_mode(MeasureMode::from(*mode)),
),
PrintMode::Expanded => (variants.most_expanded(), args),
};
if !matches!(slice.first(), Some(FormatElement::Tag(Tag::StartEntry))) {
return invalid_start_tag(TagKind::Entry, slice.first());
}
self.queue.extend_back(slice);
self.stack.push(TagKind::Entry, args);
self.queue.extend_back(&slice[1..]);
}
FormatElement::Interned(content) => self.queue.extend_back(content),
@ -1040,22 +1054,23 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
return Ok(Fits::No);
}
let group_mode = if !group.mode().is_flat() {
// Continue printing groups in expanded mode if measuring a `fits_expanded` element
let print_mode = if !group.mode().is_flat() {
PrintMode::Expanded
} else {
args.mode()
};
self.stack
.push(TagKind::Group, args.with_print_mode(group_mode));
.push(TagKind::Group, args.with_print_mode(print_mode));
if let Some(id) = group.id() {
self.group_modes_mut().insert_print_mode(id, group_mode);
self.group_modes_mut().insert_print_mode(id, print_mode);
}
}
FormatElement::Tag(StartConditionalContent(condition)) => {
let group_mode = match condition.group_id {
let print_mode = match condition.group_id {
None => args.mode(),
Some(group_id) => self
.group_modes()
@ -1063,20 +1078,20 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
.unwrap_or_else(|| args.mode()),
};
if group_mode != condition.mode {
self.queue.skip_content(TagKind::ConditionalContent);
} else {
if condition.mode == print_mode {
self.stack.push(TagKind::ConditionalContent, args);
} else {
self.queue.skip_content(TagKind::ConditionalContent);
}
}
FormatElement::Tag(StartIndentIfGroupBreaks(id)) => {
let group_mode = self
let print_mode = self
.group_modes()
.get_print_mode(*id)
.unwrap_or_else(|| args.mode());
match group_mode {
match print_mode {
PrintMode::Flat => {
self.stack.push(TagKind::IndentIfGroupBreaks, args);
}
@ -1103,6 +1118,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
) => {
self.stack.push(tag.kind(), args);
}
FormatElement::Tag(
tag @ (EndFill
| EndVerbatim
@ -1234,6 +1250,27 @@ struct FitsState {
line_width: usize,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum MeasureMode {
/// The content fits if a hard line break or soft line break in [`PrintMode::Expanded`] is seen
/// before exceeding the configured print width.
/// Returns
FirstLine,
/// The content only fits if non 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,
}
impl From<BestFittingMode> for MeasureMode {
fn from(value: BestFittingMode) -> Self {
match value {
BestFittingMode::FirstLine => Self::FirstLine,
BestFittingMode::AllLines => Self::AllLines,
}
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;