summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/model/outline.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-07-09 10:16:36 +0200
committerGitHub <noreply@github.com>2025-07-09 08:16:36 +0000
commite5e1dcd9c01341d2cd3473ac94a70223d5966086 (patch)
treed648efad9463cb10270d55ba35210eeb1e91ee22 /crates/typst-library/src/model/outline.rs
parent0a3c6939dd274f40672484695d909c2cc0d0d755 (diff)
Target-specific native show rules (#6569)
Diffstat (limited to 'crates/typst-library/src/model/outline.rs')
-rw-r--r--crates/typst-library/src/model/outline.rs86
1 files changed, 9 insertions, 77 deletions
diff --git a/crates/typst-library/src/model/outline.rs b/crates/typst-library/src/model/outline.rs
index bb061fb7..4bda02ba 100644
--- a/crates/typst-library/src/model/outline.rs
+++ b/crates/typst-library/src/model/outline.rs
@@ -1,7 +1,7 @@
use std::num::NonZeroUsize;
use std::str::FromStr;
-use comemo::{Track, Tracked};
+use comemo::Tracked;
use smallvec::SmallVec;
use typst_syntax::Span;
use typst_utils::{Get, NonZeroExt};
@@ -10,7 +10,7 @@ use crate::diag::{bail, error, At, HintedStrResult, SourceResult, StrResult};
use crate::engine::Engine;
use crate::foundations::{
cast, elem, func, scope, select_where, Args, Construct, Content, Context, Func,
- LocatableSelector, NativeElement, Packed, Resolve, Show, ShowSet, Smart, StyleChain,
+ LocatableSelector, NativeElement, Packed, Resolve, ShowSet, Smart, StyleChain,
Styles,
};
use crate::introspection::{
@@ -20,8 +20,7 @@ use crate::layout::{
Abs, Axes, BlockBody, BlockElem, BoxElem, Dir, Em, Fr, HElem, Length, Region, Rel,
RepeatElem, Sides,
};
-use crate::math::EquationElem;
-use crate::model::{Destination, HeadingElem, NumberingPattern, ParElem, Refable};
+use crate::model::{HeadingElem, NumberingPattern, ParElem, Refable};
use crate::text::{LocalName, SpaceElem, TextElem};
/// A table of contents, figures, or other elements.
@@ -147,7 +146,7 @@ use crate::text::{LocalName, SpaceElem, TextElem};
///
/// [^1]: The outline of equations is the exception to this rule as it does not
/// have a body and thus does not use indented layout.
-#[elem(scope, keywords = ["Table of Contents", "toc"], Show, ShowSet, LocalName, Locatable)]
+#[elem(scope, keywords = ["Table of Contents", "toc"], ShowSet, LocalName, Locatable)]
pub struct OutlineElem {
/// The title of the outline.
///
@@ -249,44 +248,6 @@ impl OutlineElem {
type OutlineEntry;
}
-impl Show for Packed<OutlineElem> {
- #[typst_macros::time(name = "outline", span = self.span())]
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let span = self.span();
-
- // Build the outline title.
- let mut seq = vec![];
- if let Some(title) = self.title.get_cloned(styles).unwrap_or_else(|| {
- Some(TextElem::packed(Self::local_name_in(styles)).spanned(span))
- }) {
- seq.push(
- HeadingElem::new(title)
- .with_depth(NonZeroUsize::ONE)
- .pack()
- .spanned(span),
- );
- }
-
- let elems = engine.introspector.query(&self.target.get_ref(styles).0);
- let depth = self.depth.get(styles).unwrap_or(NonZeroUsize::MAX);
-
- // Build the outline entries.
- for elem in elems {
- let Some(outlinable) = elem.with::<dyn Outlinable>() else {
- bail!(span, "cannot outline {}", elem.func().name());
- };
-
- let level = outlinable.level();
- if outlinable.outlined() && level <= depth {
- let entry = OutlineEntry::new(level, elem);
- seq.push(entry.pack().spanned(span));
- }
- }
-
- Ok(Content::sequence(seq))
- }
-}
-
impl ShowSet for Packed<OutlineElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
let mut out = Styles::new();
@@ -363,7 +324,7 @@ pub trait Outlinable: Refable {
/// With show-set and show rules on outline entries, you can richly customize
/// the outline's appearance. See the
/// [section on styling the outline]($outline/#styling-the-outline) for details.
-#[elem(scope, name = "entry", title = "Outline Entry", Show)]
+#[elem(scope, name = "entry", title = "Outline Entry")]
pub struct OutlineEntry {
/// The nesting level of this outline entry. Starts at `{1}` for top-level
/// entries.
@@ -408,30 +369,6 @@ pub struct OutlineEntry {
pub parent: Option<Packed<OutlineElem>>,
}
-impl Show for Packed<OutlineEntry> {
- #[typst_macros::time(name = "outline.entry", span = self.span())]
- fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
- let span = self.span();
- let context = Context::new(None, Some(styles));
- let context = context.track();
-
- let prefix = self.prefix(engine, context, span)?;
- let inner = self.inner(engine, context, span)?;
- let block = if self.element.is::<EquationElem>() {
- let body = prefix.unwrap_or_default() + inner;
- BlockElem::new()
- .with_body(Some(BlockBody::Content(body)))
- .pack()
- .spanned(span)
- } else {
- self.indented(engine, context, span, prefix, inner, Em::new(0.5).into())?
- };
-
- let loc = self.element_location().at(span)?;
- Ok(block.linked(Destination::Location(loc)))
- }
-}
-
#[scope]
impl OutlineEntry {
/// A helper function for producing an indented entry layout: Lays out a
@@ -654,7 +591,8 @@ impl OutlineEntry {
.ok_or_else(|| error!("cannot outline {}", self.element.func().name()))
}
- fn element_location(&self) -> HintedStrResult<Location> {
+ /// Returns the location of the outlined element.
+ pub fn element_location(&self) -> HintedStrResult<Location> {
let elem = &self.element;
elem.location().ok_or_else(|| {
if elem.can::<dyn Locatable>() && elem.can::<dyn Outlinable>() {
@@ -730,8 +668,8 @@ fn query_prefix_widths(
}
/// Helper type for introspection-based prefix alignment.
-#[elem(Construct, Locatable, Show)]
-struct PrefixInfo {
+#[elem(Construct, Locatable)]
+pub(crate) struct PrefixInfo {
/// The location of the outline this prefix is part of. This is used to
/// scope prefix computations to a specific outline.
#[required]
@@ -753,9 +691,3 @@ impl Construct for PrefixInfo {
bail!(args.span, "cannot be constructed manually");
}
}
-
-impl Show for Packed<PrefixInfo> {
- fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
- Ok(Content::empty())
- }
-}