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

Code before:
        }

        // Handle MathFragment::Variant fragments that should be scaled up.
        for fragment in &mut fragments {
            if let MathFragment::Variant(ref mut variant) = fragment {
                if variant.mid_stretched == Some(false) {
                    variant.mid_stretched = Some(true);
                    scale(ctx, fragment, height, Some(MathClass::Large));
                }
            }
        }

        // Remove weak SpacingFragment immediately after the opening or immediately
        // before the closing.
        let (mut count, original_len) = (0, fragments.len());
        fragments.retain(|fragment| {
            count += 1;
            if count == 2 || count == original_len - 1 {
                !matches!(
                    fragment,
                    MathFragment::Spacing(SpacingFragment { weak: true, .. })
                )
            } else {
                true
            }
        });

        ctx.extend(fragments);

        Ok(())
    }
}

/// Scales delimiters vertically to the nearest surrounding `{lr()}` group.
///
/// ```example
/// $ { x mid(|) sum_(i=1)^n w_i|f_i (x)| < 1 } $
/// ```
#[elem(LayoutMath)]
pub struct MidElem {
    /// The content to be scaled.
    #[required]
    pub body: Content,
}

impl LayoutMath for MidElem {
    #[tracing::instrument(skip(ctx))]
    fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
        let mut fragments = ctx.layout_fragments(self.body())?;

        for fragment in &mut fragments {

Review comment (suggestion):
```suggestion
        let original_len = fragments.len();
        let mut index = 0;
        fragments.retain(|fragment| {
            index += 1;
            (index != 2 && index + 1 != original_len) || !matches!(
                fragment,
                MathFragment::Spacing(SpacingFragment { weak: true, .. })
            )
        });
```

Code after:
        }

        // Handle MathFragment::Variant fragments that should be scaled up.
        for fragment in &mut fragments {
            if let MathFragment::Variant(ref mut variant) = fragment {
                if variant.mid_stretched == Some(false) {
                    variant.mid_stretched = Some(true);
                    scale(ctx, fragment, height, Some(MathClass::Large));
                }
            }
        }

        // Remove weak SpacingFragment immediately after the opening or immediately
        // before the closing.
        let original_len = fragments.len();
        let mut index = 0;
        fragments.retain(|fragment| {
            index += 1;
            (index != 2 && index + 1 != original_len)
                || !matches!(
                    fragment,
                    MathFragment::Spacing(SpacingFragment { weak: true, .. })
                )
        });

        ctx.extend(fragments);

        Ok(())
    }
}

/// Scales delimiters vertically to the nearest surrounding `{lr()}` group.
///
/// ```example
/// $ { x mid(|) sum_(i=1)^n w_i|f_i (x)| < 1 } $
/// ```
#[elem(LayoutMath)]
pub struct MidElem {
    /// The content to be scaled.
    #[required]
    pub body: Content,
}

impl LayoutMath for MidElem {
    #[typst_macros::time(name = "math.mid", span = self.span())]
    fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
        let mut fragments = ctx.layout_fragments(self.body())?;

        for fragment in &mut fragments {
            match fragment {
                MathFragment::Glyph(glyph) => {