Remove guards for built-in elements

The only recursive built-in show rule was the one for equations and that one was unnecessary.
This commit is contained in:
Laurenz 2024-01-24 15:55:13 +01:00
parent a3684352ea
commit 6ab04d80f3
4 changed files with 9 additions and 38 deletions

View File

@ -159,11 +159,6 @@ impl Content {
self.inner.guards.contains(&guard)
}
/// Whether no show rule was executed for this content so far.
pub fn is_pristine(&self) -> bool {
self.inner.guards.is_empty()
}
/// Whether this content has already been prepared.
pub fn is_prepared(&self) -> bool {
self.inner.prepared

View File

@ -331,9 +331,4 @@ pub enum Behaviour {
/// Guards content against being affected by the same show rule multiple times.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Guard {
/// The nth recipe from the top of the chain.
Nth(usize),
/// The [base recipe](Show) for a kind of element.
Base(Element),
}
pub struct Guard(pub usize);

View File

@ -3,8 +3,8 @@ use std::num::NonZeroUsize;
use crate::diag::{bail, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
elem, Content, Finalize, Guard, NativeElement, Packed, Resolve, Show, Smart,
StyleChain, Synthesize,
elem, Content, Finalize, NativeElement, Packed, Resolve, Smart, StyleChain,
Synthesize,
};
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
use crate::layout::{
@ -47,7 +47,6 @@ use crate::World;
#[elem(
Locatable,
Synthesize,
Show,
Finalize,
LayoutSingle,
LayoutMath,
@ -120,17 +119,6 @@ impl Synthesize for Packed<EquationElem> {
}
}
impl Show for Packed<EquationElem> {
#[typst_macros::time(name = "math.equation", span = self.span())]
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let mut realized = self.clone().pack().guarded(Guard::Base(EquationElem::elem()));
if self.block(styles) {
realized = AlignElem::new(realized).pack().spanned(self.span());
}
Ok(realized)
}
}
impl Finalize for Packed<EquationElem> {
fn finalize(&self, realized: Content, style: StyleChain) -> Content {
let mut realized = realized;

View File

@ -71,11 +71,7 @@ pub fn realize_block<'a>(
/// Whether the target is affected by show rules in the given style chain.
pub fn applicable(target: &Content, styles: StyleChain) -> bool {
if target.needs_preparation() {
return true;
}
if target.can::<dyn Show>() && target.is_pristine() {
if target.needs_preparation() || target.can::<dyn Show>() {
return true;
}
@ -84,7 +80,7 @@ pub fn applicable(target: &Content, styles: StyleChain) -> bool {
// Find out whether any recipe matches and is unguarded.
for recipe in styles.recipes() {
if recipe.applicable(target) && !target.is_guarded(Guard::Nth(n)) {
if !target.is_guarded(Guard(n)) && recipe.applicable(target) {
return true;
}
n -= 1;
@ -136,8 +132,8 @@ pub fn realize(
// Find an applicable show rule recipe.
for recipe in styles.recipes() {
let guard = Guard::Nth(n);
if recipe.applicable(target) && !target.is_guarded(guard) {
let guard = Guard(n);
if !target.is_guarded(guard) && recipe.applicable(target) {
if let Some(content) = try_apply(engine, target, recipe, guard)? {
return Ok(Some(content));
}
@ -146,11 +142,8 @@ pub fn realize(
}
// Apply the built-in show rule if there was no matching recipe.
let guard = Guard::Base(target.func());
if !target.is_guarded(guard) {
if let Some(showable) = target.with::<dyn Show>() {
return Ok(Some(showable.show(engine, styles)?));
}
if let Some(showable) = target.with::<dyn Show>() {
return Ok(Some(showable.show(engine, styles)?));
}
Ok(None)