Repository: typst/typst
File: crates/typst/src/math/equation.rs (Rust)

Code before:
        let width = full_equation_builder.size.x;

        let equation_builders = if BlockElem::breakable_in(styles) {
            let mut rows = full_equation_builder.frames.into_iter().peekable();
            let mut equation_builders = vec![];
            let mut last_first_pos = Point::zero();

            for region in regions.iter() {
                // Keep track of the position of the first row in this region,
                // so that the offset can be reverted later.
                let Some(first_pos) = rows.peek().map(|(_, pos)| *pos) else { break };
                last_first_pos = first_pos;

                let mut frames = vec![];
                let mut height = Abs::zero();
                while let Some((sub, pos)) = rows.peek() {
                    let mut pos = *pos;
                    pos.y -= first_pos.y;

                    if !region.y.fits(sub.height() + pos.y) {
                        // Allow first row to overflow to prevent infinite
                        // creation of new regions which may all be too small.
                        if !(frames.is_empty() && regions.in_last()) {
                            break;
                        }
                    }

                    let (sub, _) = rows.next().unwrap();
                    height = height.max(pos.y + sub.height());
                    frames.push((sub, pos));
                }

                equation_builders
                    .push(MathRunFrameBuilder { frames, size: Size::new(width, height) });
            }

            // Append remaining rows to the equation builder of the last region.
            if let Some(equation_builder) = equation_builders.last_mut() {
                equation_builder.frames.extend(rows.map(|(frame, mut pos)| {
                    pos.y -= last_first_pos.y;
                    (frame, pos)
                }));

                let height = equation_builder
                    .frames
                    .iter()
                    .map(|(frame, pos)| frame.height() + pos.y)
                    .max()
                    .unwrap_or(equation_builder.size.y);

                equation_builder.size.y = height;

Review comment (suggestion):
```suggestion
                    // Finish this region if the line doesn't fit. Only do it if
                    // we placed at least one line _or_ we still have non-last
                    // regions. Crucially, we don't want to infinitely create
                    // new regions which are too small.
                    if !region.y.fits(sub.height() + pos.y)
                        && (!frames.is_empty() || !regions.in_last())
                    {
                        break;
                    }
```
This is a matter of personal preference, but I find expressions like `!(a && b)` super hard to understand, so I'd prefer to apply De-Morgan's law here.

Code after:
        let width = full_equation_builder.size.x;

        let equation_builders = if BlockElem::breakable_in(styles) {
            let mut rows = full_equation_builder.frames.into_iter().peekable();
            let mut equation_builders = vec![];
            let mut last_first_pos = Point::zero();

            for region in regions.iter() {
                // Keep track of the position of the first row in this region,
                // so that the offset can be reverted later.
                let Some(&(_, first_pos)) = rows.peek() else { break };
                last_first_pos = first_pos;

                let mut frames = vec![];
                let mut height = Abs::zero();
                while let Some((sub, pos)) = rows.peek() {
                    let mut pos = *pos;
                    pos.y -= first_pos.y;

                    // Finish this region if the line doesn't fit. Only do it if
                    // we placed at least one line _or_ we still have non-last
                    // regions. Crucially, we don't want to infinitely create
                    // new regions which are too small.
                    if !region.y.fits(sub.height() + pos.y)
                        && (!frames.is_empty() || !regions.in_last())
                    {
                        break;
                    }

                    let (sub, _) = rows.next().unwrap();
                    height = height.max(pos.y + sub.height());
                    frames.push((sub, pos));
                }

                equation_builders
                    .push(MathRunFrameBuilder { frames, size: Size::new(width, height) });
            }

            // Append remaining rows to the equation builder of the last region.
            if let Some(equation_builder) = equation_builders.last_mut() {
                equation_builder.frames.extend(rows.map(|(frame, mut pos)| {
                    pos.y -= last_first_pos.y;
                    (frame, pos)
                }));

                let height = equation_builder
                    .frames
                    .iter()
                    .map(|(frame, pos)| frame.height() + pos.y)
                    .max()
                    .unwrap_or(equation_builder.size.y);