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

Code before:

        // In case a user constructs an outline entry with an arbitrary element.
        let Some(location) = elem.location() else {
            if elem.can::<dyn Locatable>() && elem.can::<dyn Outlinable>() {
                bail!(
                    self.span(), "{} must have a location", elem.func().name();
                    hint: "try using a query or a show rule to customize the outline.entry instead",
                )
            } else {
                bail!(self.span(), "cannot outline {}", elem.func().name())
            }
        };

        // The body text remains overridable.
        seq.push(
            TextElem::new(
                match TextElem::dir_in(styles) {
                    crate::layout::Dir::RTL => "\u{202B}",
                    _ => "\u{202A}",
                }
                .into(),
            )
            .pack(),
        );
        seq.push(self.body().clone().linked(Destination::Location(location)));
        seq.push(TextElem::new("\u{202C}".into()).pack());

        // Add filler symbols between the section name and page number.
        if let Some(filler) = self.fill() {
            seq.push(SpaceElem::new().pack());
            seq.push(
                BoxElem::new()
                    .with_body(Some(filler.clone()))
                    .with_width(Fr::one().into())
                    .pack()
                    .spanned(self.span()),
            );
            seq.push(SpaceElem::new().pack());
        } else {
            seq.push(HElem::new(Fr::one().into()).pack());
        }

        // Add the page number.
        let page = self.page().clone().linked(Destination::Location(location));
        seq.push(page);

        Ok(Content::sequence(seq))
    }
}


Review comment (refactor):
I think this kind of isolation could be moved into a helper function, e.g. `crate::text::isolate`.

Code after:

        // In case a user constructs an outline entry with an arbitrary element.
        let Some(location) = elem.location() else {
            if elem.can::<dyn Locatable>() && elem.can::<dyn Outlinable>() {
                bail!(
                    self.span(), "{} must have a location", elem.func().name();
                    hint: "try using a query or a show rule to customize the outline.entry instead",
                )
            } else {
                bail!(self.span(), "cannot outline {}", elem.func().name())
            }
        };

        // The body text remains overridable.
        crate::text::isolate(
            self.body().clone().linked(Destination::Location(location)),
            styles,
            &mut seq,
        );

        // Add filler symbols between the section name and page number.
        if let Some(filler) = self.fill() {
            seq.push(SpaceElem::new().pack());
            seq.push(
                BoxElem::new()
                    .with_body(Some(filler.clone()))
                    .with_width(Fr::one().into())
                    .pack()
                    .spanned(self.span()),
            );
            seq.push(SpaceElem::new().pack());
        } else {
            seq.push(HElem::new(Fr::one().into()).pack());
        }

        // Add the page number.
        let page = self.page().clone().linked(Destination::Location(location));
        seq.push(page);

        Ok(Content::sequence(seq))
    }
}