Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbol Aliases #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Symbol Aliases #27

wants to merge 3 commits into from

Conversation

T0mstone
Copy link
Collaborator

@T0mstone T0mstone commented Jan 20, 2025

This PR implements a new framework for defining aliases, i.e. symbols that copy other symbols, and uses that for all of the previously duplicated symbols in sym.

We don't intend to have a lot of these, but for the ones we do have, having a system like this is a big improvement to maintainability.
As a good illustration, this PR automatically adds the arrow variant from plus.circle to xor, which seems to have previously been overlooked.

Guide to creating Aliases

There are two types of aliases:

  • Shallow aliases copy a symbol/variant without any other variants. This is the equivalent of let alias = symbol(a.b.c) in typst.
  • Deep aliases copy a symbol, possibly with some pre-applied modifiers, and all further modifiers. This is the equivalent of let alias = a.b.c in typst.

A shallow alias is declared with

<alias> @= <other symbol name, possibly with modifiers>

A deep alias is the same, but additionally ends with .*.
Note that this isn't quite like a glob pattern, since e.g. a.b.* also matches a.b itself.

For some examples, see the diff of sym.txt.

About the Implementation

Properly handling aliases like top @= tack.b is a bit difficult since modifiers are commutative.
The implementation I wrote uses a fast-path prefix check first and then, if that fails, a slower and heavily allocating algorithm that goes through all modifiers one-by-one.
That way, an alias a @= b.d for a symbol b with a variant .c.d correctly copies that to a.c.

For the current symbols, the fast path would have been enough, but I didn't want to sacrifice correctness; This could otherwise come back to haunt us later.
And since the number of aliases will most likely be staying quite low, such a slow and inefficient fallback should be fine.

Shallow vs Deep Aliases in sym

The rules by which I decided the kind of alias for the pre-existing duplicates are basically the following:

  • Does the duplicate include all variants? Then it is a deep alias.
  • Otherwise, does one copy have variants and the other doesn't? Then the other is a shallow alias.
  • Otherwise, does it seem like there will never be any variants? (e.g. the Hebrew letters) Then also a shallow alias.
  • Otherwise, it is a deep alias. (see nabla) This is to stay open to the possibility of adding variants to it later.

Of course, as mentioned above, xor is a bit of an exception since it was missing one of the variants of plus.circle.

@MDLC01 MDLC01 added the meta Discussion about the structure of this repo label Jan 20, 2025
This avoids it visually colliding with the deprecation annotation from typst#19.
This was referenced Jan 21, 2025
@jenuori

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta Discussion about the structure of this repo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants