Extend BestFitting with mode (#6814)

This commit is contained in:
Micha Reiser 2023-08-23 17:23:45 +02:00 committed by GitHub
parent 71c25e4f9d
commit 34b2ae73b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 181 additions and 15 deletions

View file

@ -6,7 +6,7 @@ mod stack;
use crate::format_element::document::Document;
use crate::format_element::tag::{Condition, GroupMode};
use crate::format_element::{BestFittingVariants, LineMode, PrintMode};
use crate::format_element::{BestFittingMode, BestFittingVariants, LineMode, PrintMode};
use crate::prelude::tag;
use crate::prelude::tag::{DedentMode, Tag, TagKind, VerbatimKind};
use crate::printer::call_stack::{
@ -135,8 +135,8 @@ impl<'a> Printer<'a> {
self.flush_line_suffixes(queue, stack, Some(HARD_BREAK));
}
FormatElement::BestFitting { variants } => {
self.print_best_fitting(variants, queue, stack)?;
FormatElement::BestFitting { variants, mode } => {
self.print_best_fitting(variants, *mode, queue, stack)?;
}
FormatElement::Interned(content) => {
@ -429,6 +429,7 @@ impl<'a> Printer<'a> {
fn print_best_fitting(
&mut self,
variants: &'a BestFittingVariants,
mode: BestFittingMode,
queue: &mut PrintQueue<'a>,
stack: &mut PrintCallStack,
) -> PrintResult<()> {
@ -454,7 +455,9 @@ impl<'a> Printer<'a> {
// 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);
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);
@ -1072,10 +1075,13 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
FormatElement::SourcePosition(_) => {}
FormatElement::BestFitting { variants } => {
let slice = match args.mode() {
PrintMode::Flat => variants.most_flat(),
PrintMode::Expanded => variants.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))) {
@ -1402,6 +1408,15 @@ enum MeasureMode {
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::*;