Skip to content

Fix that there're no autocomplete suggestions in math functions - #7407

Closed
RungeCC wants to merge 10 commits into
typst:mainfrom
RungeCC:fix-math-complete-missing
Closed

RungeCC wants to merge 10 commits into
typst:mainfrom
RungeCC:fix-math-complete-missing

Conversation

@RungeCC

@RungeCC RungeCC commented Nov 18, 2025 •

Copy link
Copy Markdown
Contributor

Fix #6312

Note about this PR:

Changes in typst_syntax

  • Add a new method (called mode) to typst_syntax::node::LinkedNode, following the suggestion Autocomplete fixes for math mode #6415 (comment), it returns an Option<SyntaxMode>. If some unexpected conditions are encountered, it will return None.
    • Also, I'm not sure if it shall be a method or a free function.
  • It's archieved by explicitly exhaustively (namely, never use _) matching self.kind(), leaf nodes and containers are equally handled. Some nodes can not determined on their own, I leave comments for each exception case.
    • For leaf node, checking their parents' mode is enough except for Ident, which needs to check whether it's preceded by a Hash(#), as in Autocomplete fixes for math mode #6415.
    • For container (node that could have children), there're three exceptions: FuncCall, Args and FieldAccess (that's also the main reason why auto-completion wasn't working before). IIUC, mode of FuncCall or FieldAccess is determined by the left most child -- callee of a FuncCall or the leading term of a FieldAccess.
  • Add a test for new mode, consider various cases, but not exhaustive (cover every SyntaxKind), since I'm not sure whether this is necessary and the current test function is already huge.
    • I put all testcases in a single test function, not sure if I should split them up, and if so, at what granularity.

Changes in typst_ide

  • Now, as metioned in Autocomplete fixes for math mode #6415 (comment), now complete_code is done before complete_math and complete_markup, so codes for check interpolation are all removed. Also, in globals and complete_math|markup|code, use abovemetioned LinkedList::mode to check mode.
  • A related test is added.

@RungeCC

RungeCC commented Nov 18, 2025

Copy link
Copy Markdown
Contributor Author

CC @cAttte, @IgKh.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. ide Autocompletion, tooltips, formatting, etc. fix A bug fix. labels Nov 18, 2025
@isuffix

isuffix commented Nov 30, 2025

Copy link
Copy Markdown
Collaborator

I've looked this over and I'm happy with it 👍

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

Thanks for the PR! The approach is quite nice.

I tested this with the web app and completions are unfortunately still not fully functional. In particular

  • This case:

    $ f(|) $

    Because ( is MathText, we run into this branch:

    // Behind existing atom or identifier: "$a|$" or "$abc|$".
    if matches!(
    ctx.leaf.kind(),
    SyntaxKind::Text | SyntaxKind::MathText | SyntaxKind::MathIdent
    ) {
    ctx.from = ctx.leaf.offset();
    math_completions(ctx);
    return true;
    }

    And then, the return from position is wrong (it's 3 instead of 4). And without the correct from position, the autocomplete does not work correctly in the web app.

  • This case:

    $ abs(|) $

    I'm not sure what's happening, but typst-ide returns zero completions.

I also left a few comments about edge cases of the mode detection.

SyntaxKind::EnumMarker => Some(SyntaxMode::Markup),
SyntaxKind::TermItem => Some(SyntaxMode::Markup),
SyntaxKind::TermMarker => Some(SyntaxMode::Markup),
SyntaxKind::Equation => Some(SyntaxMode::Math),

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'm not sure this is correct. The contained Math node is definitely math, but the equation itself should probably be classified Markup/Code depending on the parent?

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.

For the delimiters (brackets and dollars in particular), it's also not entirely clear to me which they should belong to.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I'm not sure. It seems that there's room for choice if there's always a Math node within the Equation node.

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.

Given that eval("$...$", mode: "math") won't work, I think it should be code or markup depending parent.

Comment thread crates/typst-syntax/src/node.rs Resolved
Comment thread crates/typst-syntax/src/node.rs Resolved
Comment on lines +1029 to +1030
if self.parent().and_then(|parent| parent.mode())
== Some(SyntaxMode::Math)

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.

can this actually happen without a hash? don't we generate MathIdent in math?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IIUC, this at least happens at r in case $arrow.r$, which parent is a FieldAccess. Not sure are there any other cases.

Comment thread crates/typst-syntax/src/node.rs Resolved
Comment thread crates/typst-syntax/src/node.rs Resolved
Comment thread crates/typst-syntax/src/node.rs Outdated
SyntaxKind::Parenthesized => Some(SyntaxMode::Code),
SyntaxKind::Array => Some(SyntaxMode::Code),
SyntaxKind::Dict => Some(SyntaxMode::Code),
SyntaxKind::Named => Some(SyntaxMode::Code),

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.

This can also occur in a math call. Would that be a problem?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. Sorry about that I missed this and Spread (and also Dots), due to I misunderstand FuncCall in math.

IIUC, for both Named and Spread when in math they're always within some Args, so I think we should just take the parent mode.

Comment on lines +1059 to +1069
self.leftmost_leaf().map(|leaf| match leaf.kind() {
SyntaxKind::MathIdent => SyntaxMode::Math,
_ => SyntaxMode::Code,
})
}
SyntaxKind::FuncCall => {
self.leftmost_leaf().map(|leaf| match leaf.kind() {
// Eagerly return
SyntaxKind::MathIdent => SyntaxMode::Math,
_ => SyntaxMode::Code,
})

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.

this is probably fine in practice, but I wonder whether it's a bit overzealous. the leftmost leaf could be way down the hierarchy (though deep nesting is probably only possible in code and then it would not be a math ident. but I did a bit of a double-take here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Um, I also not sure about that could this nesting actually happens in math mode.

Or we could just look for the prev sibling and check is it a Hash.

Comment thread crates/typst-syntax/src/node.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 Dec 1, 2025
@RungeCC

RungeCC commented Dec 1, 2025 •

Copy link
Copy Markdown
Contributor Author
  • This case:
    $ abs(|) $

Ah, I find out it runs into complete_param and failed due to a missing branch MathIdent in resolve_global_callee. And my original testcase is actually wrong since $f(...)$ is always not a FuncCall.

However, there still another problem, whether we want to completely use complete_math even when complete_param is possible? For example, in

$ attach(|) $

shall we complete "b", "tb" etc or just do scope completion in math? I'm not sure.

1. add util test functions and add some more tests for corner cases
2. sort match arm of `mode` by SyntaxKind
3. refactor out some common part of arms, `as_parent`,
   `as_parent_or_default`.
4. fix incorrect handling of `Spread`, `Dots` and `Named`
5. fix some typos in comments
@RungeCC

RungeCC commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor Author
  • This case:
    $ f(|) $

I think this could be solve with:

-    if matches!(
-        ctx.leaf.kind(),
-        SyntaxKind::Text | SyntaxKind::MathText | SyntaxKind::MathIdent
-    )
+   // Ensure that we are not just behind a left parenthesis,
+   // which shall be handled in the "Anywhere" case.
+    if matches!(
+        ctx.leaf.kind(),
+        SyntaxKind::Text | SyntaxKind::MathText | SyntaxKind::MathIdent
+    ) && ctx.leaf.text() != "("

However, this seems weird to me, since before we never try to complete barely $f(|)$ (originally, this failed since we do not handle MathDelimited case in complete_math, f(x|) works since such a MathText is always grouped within some Math node), though we complete f(|) here f is a function defined somewhere above, but that's complete_params but not complete_math. On the other hand, it seems that we should also handle case like $f(x, |)$, here space is a MathText parent MathDelimited, and so on and so on, I'm not sure whether all of them shall be handled.

Notice originally the similar case $ f(x,|) $ also have a completion at from 5 (not 6), since , is a MathText and grouped with x in a Math node, so can successfully run into complete_math -> math_completion -> scope_completion. However, it seems there's no completion in webapp, is this also incorrect?

And this:

$ abs(|) $

can be solved with eagerly returned if in math:

    // If we find out the current piece to be complete it's in math mode,
    // it shall be completed in math, so quit for calling
    // `math_complete` later.
    if deciding.mode() == Some(SyntaxMode::Math) {
        return false;
    }

I'm not sure shall we also quit if we call complete_params in math mode.

Notice that the later change will cause no completion for named parameters of math functions such as attach, limits.

If those changes look good, I'll apply them later.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Dec 2, 2025
@IgKh

IgKh commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor

@RungeCC apologies in my delay in looking into this. I have also tested the PR (in my case by building Katvan against your branch) and it did not work, but I see you are already aware of this.

Regarding "true" math function calls, which are the topic of my original issue, I think that in general patching resolve_global_callee to make the case properly catch inside param_completions is probably the more correct thing to do - as it gives couple of benefits: named parameters as you have said, but also awareness of the number of positional parameters.

But I think it then does need some more changes there:

  • Positional parameter completions should probably use math_completions, as param_value_completions is missing many relevant completions.
  • In math function calls (only!) there is an additional allowed parameter separator, which is ; (the semicolon). It would need consideration at least in complete_params (as an additional deciding node), and in the end of param_completions (for "enriching" with a space). However I'm not super confident about just adding it outright in the same path that also handles code mode function calls, which is mostly what left my original stab at the issue as a draft.

Some additional test cases that could be helpful:

  • $sqrt(|)$ (One positional parameter)
  • $frac(1,|)$ (More than one positional parameter)
  • $vec(1,2,3,4,5,|)$ (Variadic)
  • $mat(1,2;|)$ and $mat(1,2; |)$ (The special array-forming syntax with a semicolon)

SyntaxKind::Named => Some(SyntaxMode::Code),
// Either in code or in math,
// it's in math only when it's within an `Args` who is in math.
SyntaxKind::Named => {

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.

couldn't this just be as_parent() or am I missing something?

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Dec 3, 2025
@laurmaedje

Copy link
Copy Markdown
Member

Hey @RungeCC, just wanted to know whether you're still interested in completing this.

@RungeCC

RungeCC commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

I'm very sorry that I haven't had time to work on this pull request recently, and I apologize for wasting the reviewers' time. I will close this PR.

@RungeCC RungeCC closed this Apr 13, 2026
@isuffix

isuffix commented Apr 13, 2026

Copy link
Copy Markdown
Collaborator

I'm very sorry that I haven't had time to work on this pull request recently, and I apologize for wasting the reviewers' time. I will close this PR.

You haven't wasted anyone's time! I'm probably going to rework this soon (with co-author credit) to get it over the finish line. The syntax mode accessor was a smart contribution; I think it's the right fix for this problem, and I wouldn't have come up with it myself.

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. waiting-on-author Pull request waits on author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typst-ide: No autocomplete suggestions in math functions

4 participants