Repository: typst/typst
File: crates/typst/src/layout/inline/mod.rs (Rust)

Code before:
                    } else if child.is::<SmartQuoteElem>() {
                        Some('"')
                    } else if child.is::<SpaceElem>()
                        || child.is::<HElem>()
                        || child.is::<LinebreakElem>()
                    {
                        Some(SPACING_REPLACE)
                    } else {
                        Some(OBJ_REPLACE)
                    }
                });

                full.push_str(quoter.quote(&quotes, elem.double(styles), peeked));
            } else {
                full.push(if elem.double(styles) { '"' } else { '\'' });
            }
            Segment::Text(full.len() - prev)
        } else if let Some(elem) = child.to::<EquationElem>() {
            let pod = Regions::one(region, Axes::splat(false));
            let par_items = elem.layout_inline(engine, styles, pod)?;

            for item in &par_items {
                let c = match item {
                    MathParItem::Space(_) => SPACING_REPLACE,
                    MathParItem::Frame(_) => OBJ_REPLACE,
                };
                full.push(c);
            }

            Segment::Equation(elem, par_items)
        } else if let Some(elem) = child.to::<BoxElem>() {
            match elem.width(styles) {
                Sizing::Fr(v) => {
                    full.push(SPACING_REPLACE);
                    Segment::FracBox(elem, v)
                }
                _ => {
                    full.push(OBJ_REPLACE);
                    let pod = Regions::one(region, Axes::splat(false));
                    let frame = elem.layout(engine, styles, pod)?.into_frame();
                    Segment::Box(elem, frame)
                }
            }
        } else if child.is::<MetaElem>() {
            Segment::Meta
        } else {
            bail!(child.span(), "unexpected paragraph child");
        };

        if let Some(last) = full.chars().last() {
            quoter.last(last, child.is::<SmartQuoteElem>());

Review comment (refactor):
this match could be deduplicated into a method on `MathParItem`

Code after:
                        Some(SPACING_REPLACE)
                    } else {
                        Some(OBJ_REPLACE)
                    }
                });

                full.push_str(quoter.quote(&quotes, elem.double(styles), peeked));
            } else {
                full.push(if elem.double(styles) { '"' } else { '\'' });
            }
            Segment::Text(full.len() - prev)
        } else if let Some(elem) = child.to::<EquationElem>() {
            let pod = Regions::one(region, Axes::splat(false));
            let items = elem.layout_inline(engine, styles, pod)?;
            full.extend(items.iter().map(MathParItem::text));
            Segment::Equation(elem, items)
        } else if let Some(elem) = child.to::<BoxElem>() {
            let frac = elem.width(styles).is_fractional();
            full.push(if frac { SPACING_REPLACE } else { OBJ_REPLACE });
            Segment::Box(elem, frac)
        } else if child.is::<MetaElem>() {
            Segment::Meta
        } else {
            bail!(child.span(), "unexpected paragraph child");
        };

        if let Some(last) = full.chars().last() {
            quoter.last(last, child.is::<SmartQuoteElem>());
        }

        spans.push(segment.len(), child.span());

        if let (Some((Segment::Text(last_len), last_styles)), Segment::Text(len)) =
            (segments.last_mut(), &segment)
        {
            if *last_styles == styles {
                *last_len += len;
                continue;
            }
        }

        segments.push((segment, styles));
    }

    Ok((full, segments, spans))
}

/// Prepare paragraph layout by shaping the whole paragraph.
fn prepare<'a>(
    engine: &mut Engine,
    children: &'a [Prehashed<Content>],