Skip to content

Fix empty 2-d math args with whitespace/trivia - #3786

Merged
laurmaedje merged 5 commits into
typst:mainfrom
wrzian:math-2d-args-3774
Apr 19, 2024
Merged

laurmaedje merged 5 commits into
typst:mainfrom
wrzian:math-2d-args-3774

Conversation

@wrzian

@wrzian wrzian commented Mar 25, 2024

Copy link
Copy Markdown
Contributor

Fixes #3774

The issue was that maybe_wrap_in_math would generate an AST with an empty "Math" element in-between the arrays, which the AST kindly parsed as an expression as empty content.

The fix is to change exprs != 1 to exprs > 1 so that we don't wrap math elements around nothing.

Also added a new test to math/matrix/typ with some examples.

@wrzian

wrzian commented Mar 25, 2024 •

Copy link
Copy Markdown
Contributor Author

I realized that my initial commit failed to preserve empty content in arguments, so I reverted it and added new tests to expose the behavior change, and made a proper fix by still wrapping empty expressions in math mode, but after any trivia nodes, not before (by calling p.wrap_within() instead of p.wrap()).

See the test update in the most recent commit for how this change affects math args. The biggest effect is converting 2-d arrays with whitespace/trivia from parsing with the whitespace at the top level, i.e. $ func( ; , ; ) $ => ([], (), [], ([],)) to now parsing properly as nested arrays: (([], ), ([], [])).

@wrzian wrzian changed the title Fix 2-d math args with spaces after semicolons Fix empty 2-d math args with whitespace/trivia Mar 25, 2024
Comment thread crates/typst-syntax/src/parser.rs Resolved
@NeoFantom NeoFantom mentioned this pull request Apr 4, 2024
1 task done
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Apr 8, 2024
@wrzian

wrzian commented Apr 9, 2024

Copy link
Copy Markdown
Contributor Author

Note: I'm going to rebase and redo all the commits. I think #3584 is going to change the test outputs.

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Apr 9, 2024
@wrzian
wrzian force-pushed the math-2d-args-3774 branch from bbced28 to a74ed08 Compare April 10, 2024 20:14
@wrzian

wrzian commented Apr 10, 2024 •

Copy link
Copy Markdown
Contributor Author

Updated. I did a force push and took the opportunity to remove the reverted commit from the branch so it doesn't show up on main (ofc it's still visible in the PR history).

Again, note that the most relevant change to review is the diff between 95c6095 and 2a8673a.

I also combined the wrap calls to just be wrap_within in a new commit. Also, and this is out of scope, I would be interested in simplifying the parser interface somewhat (and adding comments/documentation) in a future PR if that would be desired. @laurmaedje, let me know if I should try to do that or just leave it as-is.

@laurmaedje

Copy link
Copy Markdown
Member

I decided to merge #3922 first because the whole spirit of that PR is to not merge any further large reference images, so you'll unfortunately have to merge/rebase again. Sorry about that!

I did a force push and took the opportunity to remove the reverted commit from the branch so it doesn't show up on main (ofc it's still visible in the PR history).

No need to worry about the history. I'll squash and merge anyway.

I also combined the wrap calls to just be wrap_within in a new commit.

👍

Also, and this is out of scope, I would be interested in simplifying the parser interface somewhat (and adding comments/documentation) in a future PR if that would be desired.

In principle yes, but let's best first discuss it a bit, so that we're on the same page about what changes would be desirable. If you're on the Discord, doing it there in #contributors would be simplest.

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Apr 16, 2024
@wrzian
wrzian force-pushed the math-2d-args-3774 branch from a74ed08 to 59ef625 Compare April 17, 2024 17:33
@wrzian

wrzian commented Apr 17, 2024

Copy link
Copy Markdown
Contributor Author

Ok, new update:

  • First commit fixes a typo in math/syntax.typ and prepares the new math/call.typ file with the calls to pi.
  • Second commit adds more tests without making changes
    • I found a few more weird interactions that I wanted to add tests for.
    • I'm not sure that these are all great tests and am open to feedback.
  • Third commit updates the parser and changes two tests: math-call-empty-2d-args-issue-3774 and math-call-2d-repr-structure.

Finally, I'll call out the pass-to-box test is really interesting, and probably not desired behavior. However it will almost certainly be fixed by whatever ends up happening with the var() changes in #1125 and #1779. Not sure if it should be added at all, or if it should, then maybe by a different PR. Let me know your thoughts.

@wrzian

wrzian commented Apr 17, 2024 •

Copy link
Copy Markdown
Contributor Author

Not 100% sure, but I think math-call-pass-to-box might also be related to #2342.

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Apr 17, 2024

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the tests a bit to replace reference images for repr checks with in-code assertions against strings. While doing that I stumbled upon math-call-2d-semicolon-priority which is a bit weird. I would expect both to render the same way. Do you know what's going on here?

Comment thread tests/suite/math/call.typ Resolved
@laurmaedje
laurmaedje enabled auto-merge April 19, 2024 14:28
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

@laurmaedje
laurmaedje added this pull request to the merge queue Apr 19, 2024
Merged via the queue into typst:main with commit 45245f0 Apr 19, 2024
laurmaedje added a commit that referenced this pull request May 16, 2024
laurmaedje added a commit that referenced this pull request May 17, 2024
@wrzian
wrzian deleted the math-2d-args-3774 branch June 3, 2024 01:28
@wrzian wrzian mentioned this pull request Jun 10, 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

Development

Successfully merging this pull request may close these issues.

Error on spaced arguments within 2D math array

2 participants