Repository: typst/typst
File: crates/typst/src/model/heading.rs (Rust)

Code before:
}

impl Show for Packed<HeadingElem> {
    #[typst_macros::time(name = "heading", span = self.span())]
    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
        let span = self.span();
        let mut realized = self.body().clone();

        let hanging_indent = self.hanging_indent(styles);

        let mut indent = match self.hanging_indent(styles) {
            Smart::Custom(length) => length.resolve(styles),
            Smart::Auto => Abs::zero(),
        };

        if let Some(numbering) = (**self).numbering(styles).as_ref() {
            let numbering = Counter::of(HeadingElem::elem())
                .display_at_loc(engine, self.location().unwrap(), styles, numbering)?
                .spanned(span)
                + HElem::new(Em::new(0.3).into()).with_weak(true).pack();

            if hanging_indent.is_auto() {
                let pod = Regions::one(Axes::splat(Abs::inf()), Axes::splat(false));
                let size = numbering.measure(engine, styles, pod)?.into_frame().size();

                let min_indent = Length { abs: size.x, em: Em::new(0.3) }.resolve(styles);
                indent = indent.max(min_indent);
            }

            realized = numbering + realized;
        }

        realized = realized.styled(ParElem::set_hanging_indent(indent.into()));

        Ok(BlockElem::new().with_body(Some(realized)).pack().spanned(span))
    }
}

impl ShowSet for Packed<HeadingElem> {
    fn show_set(&self, styles: StyleChain) -> Styles {
        let level = (**self).resolve_level(styles).get();
        let scale = match level {
            1 => 1.4,
            2 => 1.2,
            _ => 1.0,
        };

        let size = Em::new(scale);
        let above = Em::new(if level == 1 { 1.8 } else { 1.44 }) / scale;
        let below = Em::new(0.75) / scale;


Review comment (refactor):
Could you extract the `Em::new(0.3)` into a constant?

Code after:
    #[typst_macros::time(name = "heading", span = self.span())]
    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
        const SPACING_TO_NUMBERING: Em = Em::new(0.3);

        let span = self.span();
        let mut realized = self.body().clone();

        let hanging_indent = self.hanging_indent(styles);

        let mut indent = match hanging_indent {
            Smart::Custom(length) => length.resolve(styles),
            Smart::Auto => Abs::zero(),
        };

        if let Some(numbering) = (**self).numbering(styles).as_ref() {
            let numbering = Counter::of(HeadingElem::elem())
                .display_at_loc(engine, self.location().unwrap(), styles, numbering)?
                .spanned(span);

            if hanging_indent.is_auto() {
                let pod = Regions::one(Axes::splat(Abs::inf()), Axes::splat(false));
                let size = numbering.measure(engine, styles, pod)?.into_frame().size();

                indent = size.x + SPACING_TO_NUMBERING.resolve(styles);
            }

            realized = numbering
                + HElem::new(SPACING_TO_NUMBERING.into()).with_weak(true).pack()
                + realized;
        }

        if indent != Abs::zero() {
            realized = realized.styled(ParElem::set_hanging_indent(indent.into()));
        }

        Ok(BlockElem::new().with_body(Some(realized)).pack().spanned(span))
    }
}

impl ShowSet for Packed<HeadingElem> {
    fn show_set(&self, styles: StyleChain) -> Styles {
        let level = (**self).resolve_level(styles).get();
        let scale = match level {
            1 => 1.4,
            2 => 1.2,
            _ => 1.0,
        };

        let size = Em::new(scale);
        let above = Em::new(if level == 1 { 1.8 } else { 1.44 }) / scale;
        let below = Em::new(0.75) / scale;