Skip to content

Fix show-set semantics - #3311

Merged
laurmaedje merged 1 commit into
mainfrom
show-set-fixes
Feb 1, 2024
Merged

laurmaedje merged 1 commit into
mainfrom
show-set-fixes

Conversation

@laurmaedje

@laurmaedje laurmaedje commented Jan 31, 2024 •

Copy link
Copy Markdown
Member

This pull request fixes three long-standing issues with show-set rules. This comes with a change in semantics.

Show-set order

To first recap the current (old) semantics: A show x: set y(..) rule is pretty much equivalent to show x: it => { set y(..); it }. While consistent, this leads to a couple of problems. Those all fundamentally boil down to the fact that transformational show rules need to be applied one by one: The most local show rule for an element is executed first. If it doesn't use it, no further show rules are executed. If it does use it, this it represents the output of the second-most-local show rule (which is executed lazily once it is discovered in the content produced by the first show rule). Executing show rules in this inward-out fashion makes sense because it allows user to extend or replace the behaviour of outer/built-in show rules.

Applied to show-set rules, the semantics are problematic though, as demonstrated below:

#show heading: set text(red)
#show heading: set text(blue)
= Heading

Desugaring this yields

#show heading: it => { set text(red); it }
#show heading: it => { set text(blue); it }
= Heading

After the inner show rule is executed, we have this:

#show heading: it => { set text(red); it }
#{
  set text(blue)
  [= Heading]
}

And after the outer show rule is executed, we have this:

#{
  set text(blue)
  {
    set text(red)
    [= Heading]
  } 
}

Now we can see the problem: The outermost show-set rule ends up producing the innermost content and thus, following Typst's usual rules, it takes precedence. As a result, show-set rules apply in inverse order.

While consistent and simple, the current semantics fundamentally force us to consider show-set rules in inwards-out order. To fix the problem outlined above, this PR implements new semantics for show-set which depart from the desugaring shown above. Instead of considering show-set rules one-by-one like normal show rules, all show-set rules are taken into account immediately as soon as Typst visits an element for the first time. As a result, we can properly respect their order and the snippet shown above just works, fixing #351.

Fields available in show rules and queries

This also has another benefit: It means that a styled element only has one well-defined, canonical state of its fields that can be exposed to its consumers in show rules and queries. Where in the old system, the element would "gain" new fields rule-by-rule, going through various intermediate states, there was no proper time to materialize fields configured via set rules into the element itself.1 As a result, only a few "blessed" fields were available in show rules and queries: Synthesized fields, which were originally designed to be used for synthesizing new fields from others (e.g. raw.lines), but were then also abused to make fields available in queries.

In the new system, we can simply materialize the element into its canonical state immediately after applying show-set rules. 2 This makes all fields available in show rules and queries and fixes #2193. The concept of synthesized fields remains, but is now only used for what it was originally intended to do.

Show-set on the same element

Another effect of the change discussed in the previous section is that show-set rules on the same element now just work. Rules such as

#show heading.where(level: 1): set heading(numbering: "(I)")

would previously not work for synthesized fields, because at the time where the show-set rule was applied, the heading's numbering was already materialized into the element from the existing styles. In the new system, the show-set rules are taking into account before materializing the element into its canonical state, fixing #2459.

Limitations

Note that, as a result of these new semantics, the following code will not have the desired effect:

#show heading: it => {
  set heading(numbering: "(I)")
  it
}

For the reasons laid out above (in particular those about an element's canonical state), I consider this acceptable. We're trading off some potential power for having a system that behaves in well-defined manner.

The only typical use case for rules like this I can think of would be if the show rule contained a conditional. However, those use cases should in my opinion rather be covered by show-set rules in combination with more powerful selectors. Similarly, previous discussions about potentially exposing mutable fields on elements in show rules also runs into the issue of what an element's canonical representation is, so I'd like to avoid those in favor of more powerful selectors, too.

I should also note that internal dependencies between show-set rules such as

#show heading.where(level: 1): set heading(numbering: "(I)")
#show heading.where(numbering: "(I)"): set text(red)

are not supported in this PR. This could be implemented in a future PR via fixpoint iteration (to be independent of the order) if that's considered desirable.

Result

Fixes #351
Fixes #2193
Fixes #2459

Footnotes

  1. I did experiment with a lazy approach for a while where fields wouldn't ever need to be materialized in-place, but that complicates things a lot and again raises the issue of what the canonical state of an element provided by query() should be. ↩

  2. To be able to match show-set rules before materializing fields, selector matching needed to be made style-aware. This was implemented in https://github.com/typst/typst/pull/3290. For figure.where(kind: table) to work, I unfortunately also had to do a throw-away pre-synthesis before matching because kind would be auto by default. That's somewhat unfortunate. A somewhat cleaner, but way more complex alternative would be to introduce "computed" fields that are only determined on-demand, but I felt that this wouldn't be worth it, at least not now. ↩

@laurmaedje
laurmaedje added this pull request to the merge queue Feb 1, 2024
Merged via the queue into main with commit 7d33436 Feb 1, 2024
@laurmaedje
laurmaedje deleted the show-set-fixes branch February 1, 2024 13:33
@laurmaedje laurmaedje mentioned this pull request Feb 2, 2024
1 task done
Dherse pushed a commit to Dherse/typst that referenced this pull request Feb 4, 2024
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant