mirror of
https://github.com/typst/typst
synced 2025-05-20 03:55:29 +08:00
Delay errors for all show rules (#3323)
This commit is contained in:
parent
6a9866dc80
commit
302b870321
@ -635,7 +635,6 @@ struct DisplayElem {
|
|||||||
impl Show for Packed<DisplayElem> {
|
impl Show for Packed<DisplayElem> {
|
||||||
#[typst_macros::time(name = "counter.display", span = self.span())]
|
#[typst_macros::time(name = "counter.display", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let location = self.location().unwrap();
|
let location = self.location().unwrap();
|
||||||
let counter = self.counter();
|
let counter = self.counter();
|
||||||
let numbering = self
|
let numbering = self
|
||||||
@ -665,7 +664,6 @@ impl Show for Packed<DisplayElem> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
state.display(engine, &numbering)
|
state.display(engine, &numbering)
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,9 +44,7 @@ struct LocateElem {
|
|||||||
impl Show for Packed<LocateElem> {
|
impl Show for Packed<LocateElem> {
|
||||||
#[typst_macros::time(name = "locate", span = self.span())]
|
#[typst_macros::time(name = "locate", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let location = self.location().unwrap();
|
let location = self.location().unwrap();
|
||||||
Ok(self.func().call(engine, [location])?.display())
|
Ok(self.func().call(engine, [location])?.display())
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -388,14 +388,12 @@ struct DisplayElem {
|
|||||||
impl Show for Packed<DisplayElem> {
|
impl Show for Packed<DisplayElem> {
|
||||||
#[typst_macros::time(name = "state.display", span = self.span())]
|
#[typst_macros::time(name = "state.display", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let location = self.location().unwrap();
|
let location = self.location().unwrap();
|
||||||
let value = self.state().at(engine, location)?;
|
let value = self.state().at(engine, location)?;
|
||||||
Ok(match self.func() {
|
Ok(match self.func() {
|
||||||
Some(func) => func.call(engine, [value])?.display(),
|
Some(func) => func.call(engine, [value])?.display(),
|
||||||
None => value.display(),
|
None => value.display(),
|
||||||
})
|
})
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,7 +225,6 @@ impl Show for Packed<BibliographyElem> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let span = self.span();
|
let span = self.span();
|
||||||
let works = Works::generate(engine.world, engine.introspector).at(span)?;
|
let works = Works::generate(engine.world, engine.introspector).at(span)?;
|
||||||
let references = works
|
let references = works
|
||||||
@ -242,9 +241,7 @@ impl Show for Packed<BibliographyElem> {
|
|||||||
Packed::new(GridCell::new(prefix.clone().unwrap_or_default()))
|
Packed::new(GridCell::new(prefix.clone().unwrap_or_default()))
|
||||||
.spanned(span),
|
.spanned(span),
|
||||||
);
|
);
|
||||||
cells.push(
|
cells.push(Packed::new(GridCell::new(reference.clone())).spanned(span));
|
||||||
Packed::new(GridCell::new(reference.clone())).spanned(span),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
seq.push(VElem::new(row_gutter).with_weakness(3).pack());
|
seq.push(VElem::new(row_gutter).with_weakness(3).pack());
|
||||||
@ -269,7 +266,6 @@ impl Show for Packed<BibliographyElem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(content)
|
Ok(content)
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +152,6 @@ pub struct CiteGroup {
|
|||||||
impl Show for Packed<CiteGroup> {
|
impl Show for Packed<CiteGroup> {
|
||||||
#[typst_macros::time(name = "cite", span = self.span())]
|
#[typst_macros::time(name = "cite", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let location = self.location().unwrap();
|
let location = self.location().unwrap();
|
||||||
let span = self.span();
|
let span = self.span();
|
||||||
Works::generate(engine.world, engine.introspector)
|
Works::generate(engine.world, engine.introspector)
|
||||||
@ -160,9 +159,6 @@ impl Show for Packed<CiteGroup> {
|
|||||||
.citations
|
.citations
|
||||||
.get(&location)
|
.get(&location)
|
||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| bail!(span, "failed to format citation (this is a bug)"))
|
||||||
bail!(span, "failed to format citation (this is a bug)")
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,6 @@ impl Packed<FootnoteElem> {
|
|||||||
impl Show for Packed<FootnoteElem> {
|
impl Show for Packed<FootnoteElem> {
|
||||||
#[typst_macros::time(name = "footnote", span = self.span())]
|
#[typst_macros::time(name = "footnote", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let loc = self.declaration_location(engine).at(self.span())?;
|
let loc = self.declaration_location(engine).at(self.span())?;
|
||||||
let numbering = self.numbering(styles);
|
let numbering = self.numbering(styles);
|
||||||
let counter = Counter::of(FootnoteElem::elem());
|
let counter = Counter::of(FootnoteElem::elem());
|
||||||
@ -135,7 +134,6 @@ impl Show for Packed<FootnoteElem> {
|
|||||||
let loc = loc.variant(1);
|
let loc = loc.variant(1);
|
||||||
// Add zero-width weak spacing to make the footnote "sticky".
|
// Add zero-width weak spacing to make the footnote "sticky".
|
||||||
Ok(HElem::hole().pack() + sup.linked(Destination::Location(loc)))
|
Ok(HElem::hole().pack() + sup.linked(Destination::Location(loc)))
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,13 +95,11 @@ impl Show for Packed<LinkElem> {
|
|||||||
let body = self.body().clone();
|
let body = self.body().clone();
|
||||||
let linked = match self.dest() {
|
let linked = match self.dest() {
|
||||||
LinkTarget::Dest(dest) => body.linked(dest.clone()),
|
LinkTarget::Dest(dest) => body.linked(dest.clone()),
|
||||||
LinkTarget::Label(label) => engine
|
LinkTarget::Label(label) => {
|
||||||
.delayed(|engine| {
|
|
||||||
let elem = engine.introspector.query_label(*label).at(self.span())?;
|
let elem = engine.introspector.query_label(*label).at(self.span())?;
|
||||||
let dest = Destination::Location(elem.location().unwrap());
|
let dest = Destination::Location(elem.location().unwrap());
|
||||||
Ok(Some(body.clone().linked(dest)))
|
body.clone().linked(dest)
|
||||||
})
|
}
|
||||||
.unwrap_or(body),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(linked.styled(TextElem::set_hyphenate(Hyphenate(Smart::Custom(false)))))
|
Ok(linked.styled(TextElem::set_hyphenate(Hyphenate(Smart::Custom(false)))))
|
||||||
|
@ -163,7 +163,6 @@ impl Synthesize for Packed<RefElem> {
|
|||||||
impl Show for Packed<RefElem> {
|
impl Show for Packed<RefElem> {
|
||||||
#[typst_macros::time(name = "ref", span = self.span())]
|
#[typst_macros::time(name = "ref", span = self.span())]
|
||||||
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||||
Ok(engine.delayed(|engine| {
|
|
||||||
let target = *self.target();
|
let target = *self.target();
|
||||||
let elem = engine.introspector.query_label(target);
|
let elem = engine.introspector.query_label(target);
|
||||||
let span = self.span();
|
let span = self.span();
|
||||||
@ -200,10 +199,7 @@ impl Show for Packed<RefElem> {
|
|||||||
let numbering = refable
|
let numbering = refable
|
||||||
.numbering()
|
.numbering()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
eco_format!(
|
eco_format!("cannot reference {} without numbering", elem.func().name())
|
||||||
"cannot reference {} without numbering",
|
|
||||||
elem.func().name()
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.hint(eco_format!(
|
.hint(eco_format!(
|
||||||
"you can enable {} numbering with `#set {}(numbering: \"1.\")`",
|
"you can enable {} numbering with `#set {}(numbering: \"1.\")`",
|
||||||
@ -234,7 +230,6 @@ impl Show for Packed<RefElem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(content.linked(Destination::Location(loc)))
|
Ok(content.linked(Destination::Location(loc)))
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,18 +92,17 @@ pub fn realize(
|
|||||||
meta = prepare(engine, &mut target, &mut map, styles)?;
|
meta = prepare(engine, &mut target, &mut map, styles)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the step.
|
// Apply a step, if there is one.
|
||||||
let mut output = match step {
|
let mut output = match step {
|
||||||
// Apply a user-defined show rule.
|
Some(step) => {
|
||||||
Some(Step::Recipe(recipe, guard)) => show(engine, target, recipe, guard)?,
|
// Errors in show rules don't terminate compilation immediately. We
|
||||||
|
// just continue with empty content for them and show all errors
|
||||||
// If the verdict picks this step, the `target` is guaranteed
|
// together, if they remain by the end of the introspection loop.
|
||||||
// to have a built-in show rule.
|
//
|
||||||
Some(Step::Builtin) => {
|
// This way, we can ignore errors that only occur in earlier
|
||||||
target.with::<dyn Show>().unwrap().show(engine, styles.chain(&map))?
|
// iterations and also show more useful errors at once.
|
||||||
|
engine.delayed(|engine| show(engine, target, step, styles.chain(&map)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing to do.
|
|
||||||
None => target,
|
None => target,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -122,12 +121,12 @@ struct Verdict<'a> {
|
|||||||
prepared: bool,
|
prepared: bool,
|
||||||
/// A map of styles to apply to the element.
|
/// A map of styles to apply to the element.
|
||||||
map: Styles,
|
map: Styles,
|
||||||
/// An optional transformation step to apply to the element.
|
/// An optional show rule transformation to apply to the element.
|
||||||
step: Option<Step<'a>>,
|
step: Option<ShowStep<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An optional transformation step to apply to an element.
|
/// An optional show rule transformation to apply to the element.
|
||||||
enum Step<'a> {
|
enum ShowStep<'a> {
|
||||||
/// A user-defined transformational show rule.
|
/// A user-defined transformational show rule.
|
||||||
Recipe(&'a Recipe, RecipeIndex),
|
Recipe(&'a Recipe, RecipeIndex),
|
||||||
/// The built-in show rule.
|
/// The built-in show rule.
|
||||||
@ -200,7 +199,7 @@ fn verdict<'a>(
|
|||||||
// If we find a matching, unguarded replacement show rule,
|
// If we find a matching, unguarded replacement show rule,
|
||||||
// remember it, but still continue searching for potential
|
// remember it, but still continue searching for potential
|
||||||
// show-set styles that might change the verdict.
|
// show-set styles that might change the verdict.
|
||||||
step = Some(Step::Recipe(recipe, index));
|
step = Some(ShowStep::Recipe(recipe, index));
|
||||||
|
|
||||||
// If we found a show rule and are already prepared, there is
|
// If we found a show rule and are already prepared, there is
|
||||||
// nothing else to do, so we can just break.
|
// nothing else to do, so we can just break.
|
||||||
@ -215,7 +214,7 @@ fn verdict<'a>(
|
|||||||
|
|
||||||
// If we found no user-defined rule, also consider the built-in show rule.
|
// If we found no user-defined rule, also consider the built-in show rule.
|
||||||
if step.is_none() && target.can::<dyn Show>() {
|
if step.is_none() && target.can::<dyn Show>() {
|
||||||
step = Some(Step::Builtin);
|
step = Some(ShowStep::Builtin);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there's no nothing to do, there is also no verdict.
|
// If there's no nothing to do, there is also no verdict.
|
||||||
@ -286,21 +285,30 @@ fn prepare(
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply a user-defined show rule.
|
/// Apply a step.
|
||||||
fn show(
|
fn show(
|
||||||
engine: &mut Engine,
|
engine: &mut Engine,
|
||||||
target: Content,
|
target: Content,
|
||||||
recipe: &Recipe,
|
step: ShowStep,
|
||||||
index: RecipeIndex,
|
styles: StyleChain,
|
||||||
) -> SourceResult<Content> {
|
) -> SourceResult<Content> {
|
||||||
match &recipe.selector {
|
match step {
|
||||||
|
// Apply a user-defined show rule.
|
||||||
|
ShowStep::Recipe(recipe, guard) => match &recipe.selector {
|
||||||
|
// If the selector is a regex, the `target` is guaranteed to be a
|
||||||
|
// text element. This invokes special regex handling.
|
||||||
Some(Selector::Regex(regex)) => {
|
Some(Selector::Regex(regex)) => {
|
||||||
// If the verdict picks this rule, the `target` is guaranteed
|
|
||||||
// to be a text element.
|
|
||||||
let text = target.into_packed::<TextElem>().unwrap();
|
let text = target.into_packed::<TextElem>().unwrap();
|
||||||
show_regex(engine, &text, regex, recipe, index)
|
show_regex(engine, &text, regex, recipe, guard)
|
||||||
}
|
}
|
||||||
_ => recipe.apply(engine, target.guarded(index)),
|
|
||||||
|
// Just apply the recipe.
|
||||||
|
_ => recipe.apply(engine, target.guarded(guard)),
|
||||||
|
},
|
||||||
|
|
||||||
|
// If the verdict picks this step, the `target` is guaranteed to have a
|
||||||
|
// built-in show rule.
|
||||||
|
ShowStep::Builtin => target.with::<dyn Show>().unwrap().show(engine, styles),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
tests/typ/compiler/delayed-error.typ
Normal file
11
tests/typ/compiler/delayed-error.typ
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// Test that errors in show rules are delayed: There can be multiple at once.
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 26-34 panicked with: "hey1"
|
||||||
|
#show heading: _ => panic("hey1")
|
||||||
|
|
||||||
|
// Error: 25-33 panicked with: "hey2"
|
||||||
|
#show strong: _ => panic("hey2")
|
||||||
|
|
||||||
|
= Hello
|
||||||
|
*strong*
|
Loading…
x
Reference in New Issue
Block a user