Skip to content

Fix math function tooltips/completions, and add MathFieldAccess and LinkedNode::mode - #8116

Merged
laurmaedje merged 7 commits into
typst:mainfrom
isuffix:ide-math-fixes
Apr 30, 2026
Merged

laurmaedje merged 7 commits into
typst:mainfrom
isuffix:ide-math-fixes

Conversation

@isuffix

@isuffix isuffix commented Apr 16, 2026 •

Copy link
Copy Markdown
Collaborator

This PR fixes a number of issues with IDE functionality in Typst and adds two major refactorings to simplify future maintenance of these features.

The major refactorings are the addition of the MathFieldAccess syntax kind and the LinkedNode::mode method. MathFieldAccess is meant to further distinguish math from code in the syntax tree (in the same vein as Ident vs. MathIdent), which both simplifies the implementation of LinkedNode::mode and will be useful for future math syntax refactorings. The implementation of LinkedNode::mode supersedes PR #7407, but I've changed the implementation to use a data-oriented approach by having the LinkedNode method match on an enum, ModeSearch, which is only dependent on the SyntaxKind.

However, I only implemented the refactorings after having resolved most of the issues via the prior commits. I did this so that I could better understand where those errors were actually coming from and so we could more clearly see the benefits of the refactorings to code clarity without them also affecting behavior. Although the addition of LinkedNode::mode did allow for fixing completions for named and spread arguments in math which I couldn't find a way to resolve beforehand.

Comment thread crates/typst-syntax/src/kind.rs Resolved
@laurmaedje laurmaedje added syntax About syntax, parsing, etc. ide Autocompletion, tooltips, formatting, etc. waiting-on-review This PR is waiting to be reviewed. refactor A refactoring. fix A bug fix. labels Apr 16, 2026

@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.

Nice work! Just a few comments.

On the mode suggestions for a couple of the "boundary" syntax nodes, I'm not entirely certain, but I feel like it's not quite consistent as is.

Comment thread crates/typst-eval/src/vm.rs Resolved
Comment thread crates/typst-ide/src/tooltip.rs Resolved
Comment thread crates/typst-ide/src/complete.rs Resolved
Comment thread crates/typst-eval/src/call.rs Resolved
Comment thread crates/typst-ide/src/complete.rs Resolved
Comment thread crates/typst-syntax/src/kind.rs Resolved
Comment thread crates/typst-syntax/src/kind.rs Resolved
Comment thread crates/typst-syntax/src/kind.rs Resolved
Comment thread crates/typst-syntax/src/kind.rs Resolved
Comment thread crates/typst-syntax/src/node.rs Resolved
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Apr 21, 2026
Comment thread crates/typst-syntax/src/kind.rs Resolved
@isuffix
isuffix force-pushed the ide-math-fixes branch 2 times, most recently from f003f80 to ba26acc Compare April 22, 2026 16:56
@isuffix

isuffix commented Apr 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

I think I've resolved all the non-mode related comments. I also pushed a refactor that didn't change any behavior, but adds use ModeAfter::*; and use SyntaxMode::{...}; statements to mode_search to make it easier to read/edit.

For the various mode comments, I think what we really want out of the method is a way to determine the SyntaxMode that we would be in immediately after a given node. To me this is justified by the idea that returning anything but Code for # would be ludicrous; like what would be the use of such a method? I did consider that we could add a Side parameter, or maybe define mode in terms of a cursor position instead of on the LinkedNode, but those all feel a little cumbersome for helping with completions.

This would also resolve what to return when something is embedded with a hash, like #ident: we return Code because immediately after ident you can write .field and stay in code mode, which is exactly the completion we want to offer. And while we could make a new distinction between atomic/non-atomic expressions, I don't think it would be worth it.

To clarify this, how would you feel about renaming LinkedNode::mode to LinkedNode::mode_after? And then ModeSearch could be ModeAfter and SytnaxKind::mode_search could be mode_after, although I feel like parent_mode could just stay the same.

Finally, for Label/Raw/Equation/Ident (and also Str), I think we're just missing a new enum variant for Embeddable syntax kinds. Those kinds can all exist in either markup or math on their own, but are also available directly in code, or embedded as code into markup/math with a hashtag. And I think the resolution for them is to say that they inherit from their parent unless they're embedded into markup/math with a hashtag. Then with that addition, for dollar signs we can start to distinguish opening dollar signs from closing ones, where opening dollars will return Math (because after them we're in math mode) and closing dollars will return the mode of the overall Equation.

@laurmaedje

Copy link
Copy Markdown
Member

I think what you say makes sense. I was only thinking about the modes in the abstract, not with their concrete use for completions in mind. I'm on board with the changes.

One thing I don't fully get yet is how Ident relates to Label/Raw/Equation. Isn't Ident like any other expression? It requires a hash to be in markup, while label/raw/equation don't. In math, idents don't but that would be MathIdent then.

@isuffix

isuffix commented Apr 23, 2026

Copy link
Copy Markdown
Collaborator Author

Ident differs from Label/Raw/Equation/Str in that it can't be a top-level expression in markup or math, but unlike say, Float, Ident is available outside of code mode when in a Named kind under MathArgs. This makes it somewhat similar to how Colon is mainly code-mode, but can be part of markup under TermItem. However Ident can't just use Parent because it can still be embedded with a hashtag like the other embeddable kinds.

@isuffix
isuffix force-pushed the ide-math-fixes branch 2 times, most recently from f2a860e to ea58a33 Compare April 23, 2026 17:08
@isuffix

isuffix commented Apr 23, 2026

Copy link
Copy Markdown
Collaborator Author

Updated with those changes. I also realized that there were issues with a few other kinds like ContentBlock (mainly code, but can be markup under Ref) and Space (needs special handling due to how we use it at the edges of Equation), so I updated those and added tests for everything I could think of.

Also, I expanded the code | math | markup comments to include the parent kind which produces each other mode. There's a large explanation comment at the top, but LMK it's perhaps too much info or would be too hard to keep up-to-date.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Apr 27, 2026
Comment thread crates/typst-syntax/src/kind.rs Outdated
Self::Strong => Known(Markup),
Self::Emph => Known(Markup),
Self::Raw => Embeddable, // code/markup: expr
Self::RawLang => Parent, // code/markup: Raw

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.

After the RawLang, are we really in code or markup? We're still in the raw block after all. So perhaps we could handle this like we handle comments, by returning None from LinkedNode::mode_after.

The other raw parts are also a bit tricky.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think returning None makes sense for the internal kinds. For now, I'll switch to doing so except for the overall Raw kind and the closing raw delimiter.

However, you should also know that tinymist has a very similar construct as an InterpretMode enum with variants for Code/Math/Markup and Raw/String/Comment (defined in tinymist-analysis/src/syntax/matcher.rs:316). If you want, we could try to unify with them by outputting a similar enum, but their code would probably benefit from mode_after regardless.

Also note that Raw does trigger some special completions for tinymist, but I can't find an instance of them using String/Comment to produce any particular kind of completion.

Comment thread crates/typst-syntax/src/kind.rs Outdated

Self::Shebang => Comment,
Self::LineComment => Comment,
Self::BlockComment => 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.

Aren't we, in principle, back in the parent mode right after a block comment? Though changing this would also have an effect on the autocomplete changes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I guess we kind of are, but I don't think we can do any better without adding a representation for the delimiter of a comment or changing the whole mode_after interface to use cursors instead of whole nodes. So I think it's probably best to just not do anything for this :/

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 don't quite follow. What would be wrong about setting BlockComment to Parent?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Nothing wrong with it, it would just affect autocompletion if we did.

let _ = complete_comments(&mut ctx)
|| complete_field_accesses(&mut ctx)
// Getting the syntax mode also ensures we are not in a comment.
let mode = ctx.leaf.mode_after()?;

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 wonder if the "mode after" framing will cause any issues when attempting to trigger completions at a position that is not on a node boundary.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think there are really only a few edge cases like raw/strings/comments which would be problematic and could be handled by special completions before accessing mode_after.

@isuffix isuffix Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

But since we don't have any of those completions ourselves, I think it's fine for now?

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Apr 28, 2026
@isuffix
isuffix force-pushed the ide-math-fixes branch 2 times, most recently from 211e557 to beb6160 Compare April 28, 2026 19:03
@isuffix

isuffix commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Rebased off main and updated to return None for the bodies of raw text. I also reverted one of the comments in the first commit that would conflict with #8188

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Apr 30, 2026
@laurmaedje
laurmaedje merged commit 031f539 into typst:main Apr 30, 2026
8 checks passed
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Apr 30, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Thank you!

@isuffix
isuffix deleted the ide-math-fixes branch April 30, 2026 12:55
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request May 14, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 15, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 15, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 24, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request May 24, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jun 1, 2026
ParaN3xus added a commit to ParaN3xus/typstyle that referenced this pull request Jun 4, 2026
QuadnucYard added a commit to typstyle-rs/typstyle that referenced this pull request Jun 6, 2026
commit 65301b2
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Fri Jun 5 00:57:12 2026 +0000

    build: update tinymist to 0.14.23

commit 3ac7c69
Author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Date:   Thu Jun 4 23:30:34 2026 +0000

    build: update tinymist to 0.14.23-rc1

commit 4f5efbf
Author: paran3xus <[email protected]>
Date:   Thu Jun 4 12:41:19 2026 +0000

    feat: add compatibility for typst/typst#7865 and typst/typst#8116

commit cb25b2e
Author: paran3xus <[email protected]>
Date:   Sat May 16 10:06:29 2026 +0000

    fix: adapt typstyle-consistency to typst/typst#7964

commit ae25cb3
Author: paran3xus <[email protected]>
Date:   Sat May 16 10:06:22 2026 +0000

    fix: adapt today to typst/typst#7691

commit c5702de
Author: paran3xus <[email protected]>
Date:   Sat May 16 05:14:33 2026 +0000

    build: patch typst-bundle

commit aeffa37
Author: paran3xus <[email protected]>
Date:   Fri May 15 05:09:16 2026 +0000

    build: bump tinymist-world to nightly (remote branch)

commit 8ea80b1
Author: paran3xus <[email protected]>
Date:   Thu May 14 05:08:26 2026 +0000

    build: bump typst to nightly (local patch)

    a
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin pushed a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 13, 2026
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
This PR is part of the Typst 0.15 nightly cleanup stack. It sits on top
of the path-model group and keeps this slice focused on analyzer, query,
debug, lint, eval, and typlite API adaptation.

## Upstream Typst Changes

- [`typst/typst#7542`](typst/typst#7542) changes
function internals. Tinymist updates signature, definition, hover, and
rename paths that inspect function internals.
- [`typst/typst#7543`](typst/typst#7543) moves
call sites toward `to_element`-style element handling. Tinymist updates
field access, completion kind inference, and syntax type checking
accordingly.
- [`typst/typst#7808`](typst/typst#7808) changes
parameter metadata. Tinymist updates signature help, built-in type
metadata, scope completion, and type completion to the new parameter
representation.
- [`typst/typst#8215`](typst/typst#8215)
requests the standard library from `World` once upfront. Tinymist
updates tracked values, upstream helpers, global analysis, DAP request
evaluation, and eval engine construction to use the preloaded library
path.
- [`typst/typst#7865`](typst/typst#7865),
[`typst/typst#8116`](typst/typst#8116), and
`typst/typst@5326cd297` adjust math call, math field, and prime syntax.
Tinymist updates syntax matchers, coverage instrumentation, debugger
instrumentation, lint behavior, expression parsing, and query fixtures.
- [`typst/typst#7490`](typst/typst#7490) and
`typst/typst@cfdd4238b` change font metadata APIs. Tinymist updates
document metrics and symbol glyph handling.
- `typst/typst@0f04fd176` changes diagnostic hints into spanned values.
Tinymist updates diagnostic hint rendering.
- [`typst/typst#7897`](typst/typst#7897)
introduces import item placeholders and related attach/text API changes.
Tinymist updates import completions and math text/attach matching.
- Typst 0.15 makes `typst::ROUTINES` private, adds the library argument
to `typst_eval::eval`, and protects engine introspection. Tinymist
updates `typst-shim` eval for both nightly and default feature builds.

## Tinymist Changes

- Ports analysis and query code to the updated function, element,
parameter, font, and math syntax APIs.
- Updates debug coverage and debugger instrumentation for the new math
call/field syntax shapes.
- Updates lint behavior so function-call and math-call handling stay
aligned.
- Updates DAP and `crityp` to the preloaded engine library path.
- Updates `typst-shim` eval and syntax compatibility modules so both
`nightly` and default feature builds compile against the current Typst
0.15 dependency set.
- Refreshes targeted query and typlite fixtures affected by these API
and syntax behavior changes.

---------

Co-authored-by: paran3xus <[email protected]>
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
This PR is part of the Typst 0.15 nightly cleanup stack. It sits on top
of the path-model group and keeps this slice focused on analyzer, query,
debug, lint, eval, and typlite API adaptation.

- [`typst/typst#7542`](typst/typst#7542) changes
function internals. Tinymist updates signature, definition, hover, and
rename paths that inspect function internals.
- [`typst/typst#7543`](typst/typst#7543) moves
call sites toward `to_element`-style element handling. Tinymist updates
field access, completion kind inference, and syntax type checking
accordingly.
- [`typst/typst#7808`](typst/typst#7808) changes
parameter metadata. Tinymist updates signature help, built-in type
metadata, scope completion, and type completion to the new parameter
representation.
- [`typst/typst#8215`](typst/typst#8215)
requests the standard library from `World` once upfront. Tinymist
updates tracked values, upstream helpers, global analysis, DAP request
evaluation, and eval engine construction to use the preloaded library
path.
- [`typst/typst#7865`](typst/typst#7865),
[`typst/typst#8116`](typst/typst#8116), and
`typst/typst@5326cd297` adjust math call, math field, and prime syntax.
Tinymist updates syntax matchers, coverage instrumentation, debugger
instrumentation, lint behavior, expression parsing, and query fixtures.
- [`typst/typst#7490`](typst/typst#7490) and
`typst/typst@cfdd4238b` change font metadata APIs. Tinymist updates
document metrics and symbol glyph handling.
- `typst/typst@0f04fd176` changes diagnostic hints into spanned values.
Tinymist updates diagnostic hint rendering.
- [`typst/typst#7897`](typst/typst#7897)
introduces import item placeholders and related attach/text API changes.
Tinymist updates import completions and math text/attach matching.
- Typst 0.15 makes `typst::ROUTINES` private, adds the library argument
to `typst_eval::eval`, and protects engine introspection. Tinymist
updates `typst-shim` eval for both nightly and default feature builds.

- Ports analysis and query code to the updated function, element,
parameter, font, and math syntax APIs.
- Updates debug coverage and debugger instrumentation for the new math
call/field syntax shapes.
- Updates lint behavior so function-call and math-call handling stay
aligned.
- Updates DAP and `crityp` to the preloaded engine library path.
- Updates `typst-shim` eval and syntax compatibility modules so both
`nightly` and default feature builds compile against the current Typst
0.15 dependency set.
- Refreshes targeted query and typlite fixtures affected by these API
and syntax behavior changes.

---------

Co-authored-by: paran3xus <[email protected]>
Myriad-Dreamin added a commit to Myriad-Dreamin/tinymist that referenced this pull request Jun 15, 2026
This PR is part of the Typst 0.15 nightly cleanup stack. It sits on top
of the path-model group and keeps this slice focused on analyzer, query,
debug, lint, eval, and typlite API adaptation.

- [`typst/typst#7542`](typst/typst#7542) changes
function internals. Tinymist updates signature, definition, hover, and
rename paths that inspect function internals.
- [`typst/typst#7543`](typst/typst#7543) moves
call sites toward `to_element`-style element handling. Tinymist updates
field access, completion kind inference, and syntax type checking
accordingly.
- [`typst/typst#7808`](typst/typst#7808) changes
parameter metadata. Tinymist updates signature help, built-in type
metadata, scope completion, and type completion to the new parameter
representation.
- [`typst/typst#8215`](typst/typst#8215)
requests the standard library from `World` once upfront. Tinymist
updates tracked values, upstream helpers, global analysis, DAP request
evaluation, and eval engine construction to use the preloaded library
path.
- [`typst/typst#7865`](typst/typst#7865),
[`typst/typst#8116`](typst/typst#8116), and
`typst/typst@5326cd297` adjust math call, math field, and prime syntax.
Tinymist updates syntax matchers, coverage instrumentation, debugger
instrumentation, lint behavior, expression parsing, and query fixtures.
- [`typst/typst#7490`](typst/typst#7490) and
`typst/typst@cfdd4238b` change font metadata APIs. Tinymist updates
document metrics and symbol glyph handling.
- `typst/typst@0f04fd176` changes diagnostic hints into spanned values.
Tinymist updates diagnostic hint rendering.
- [`typst/typst#7897`](typst/typst#7897)
introduces import item placeholders and related attach/text API changes.
Tinymist updates import completions and math text/attach matching.
- Typst 0.15 makes `typst::ROUTINES` private, adds the library argument
to `typst_eval::eval`, and protects engine introspection. Tinymist
updates `typst-shim` eval for both nightly and default feature builds.

- Ports analysis and query code to the updated function, element,
parameter, font, and math syntax APIs.
- Updates debug coverage and debugger instrumentation for the new math
call/field syntax shapes.
- Updates lint behavior so function-call and math-call handling stay
aligned.
- Updates DAP and `crityp` to the preloaded engine library path.
- Updates `typst-shim` eval and syntax compatibility modules so both
`nightly` and default feature builds compile against the current Typst
0.15 dependency set.
- Refreshes targeted query and typlite fixtures affected by these API
and syntax behavior changes.

---------

Co-authored-by: paran3xus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A bug fix. ide Autocompletion, tooltips, formatting, etc. refactor A refactoring. syntax About syntax, parsing, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typst-ide: No autocomplete suggestions in math functions

2 participants