Repository navigation
Conversation
…ccidentally deleted before; now `complete_code` needs to handle the case of a standalone `Hash` itself.
…left most child can be other than Ident|MathIdent
|
I've looked this over and I'm happy with it 👍 |
laurmaedje
left a comment
There was a problem hiding this comment.
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:
typst/crates/typst-ide/src/complete.rs
Lines 318 to 326 in e8755ea
And then, the return
fromposition 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-idereturns 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), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
For the delimiters (brackets and dollars in particular), it's also not entirely clear to me which they should belong to.
There was a problem hiding this comment.
Hmm, I'm not sure. It seems that there's room for choice if there's always a Math node within the Equation node.
There was a problem hiding this comment.
Given that eval("$...$", mode: "math") won't work, I think it should be code or markup depending parent.
| if self.parent().and_then(|parent| parent.mode()) | ||
| == Some(SyntaxMode::Math) |
There was a problem hiding this comment.
can this actually happen without a hash? don't we generate MathIdent in math?
There was a problem hiding this comment.
IIUC, this at least happens at r in case $arrow.r$, which parent is a FieldAccess. Not sure are there any other cases.
| SyntaxKind::Parenthesized => Some(SyntaxMode::Code), | ||
| SyntaxKind::Array => Some(SyntaxMode::Code), | ||
| SyntaxKind::Dict => Some(SyntaxMode::Code), | ||
| SyntaxKind::Named => Some(SyntaxMode::Code), |
There was a problem hiding this comment.
This can also occur in a math call. Would that be a problem?
There was a problem hiding this comment.
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.
| 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, | ||
| }) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Ah, I find out it runs into However, there still another problem, whether we want to completely use $ attach(|) $shall we complete |
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
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 Notice originally the similar case 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 Notice that the later change will cause no completion for named parameters of math functions such as If those changes look good, I'll apply them later. |
|
@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 But I think it then does need some more changes there:
Some additional test cases that could be helpful:
|
| 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 => { |
There was a problem hiding this comment.
couldn't this just be as_parent() or am I missing something?
|
Hey @RungeCC, just wanted to know whether you're still interested in completing this. |
|
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. |
Fix #6312
Note about this PR:
Changes in
typst_syntaxmode) totypst_syntax::node::LinkedNode, following the suggestion Autocomplete fixes for math mode #6415 (comment), it returns anOption<SyntaxMode>. If some unexpected conditions are encountered, it will returnNone._) matchingself.kind(), leaf nodes and containers are equally handled. Some nodes can not determined on their own, I leave comments for each exception case.modeis enough except forIdent, which needs to check whether it's preceded by aHash(#), as in Autocomplete fixes for math mode #6415.FuncCall,ArgsandFieldAccess(that's also the main reason why auto-completion wasn't working before). IIUC, mode ofFuncCallorFieldAccessis determined by the left most child --calleeof aFuncCallor the leading term of aFieldAccess.mode, consider various cases, but not exhaustive (cover everySyntaxKind), since I'm not sure whether this is necessary and the current test function is already huge.Changes in
typst_idecomplete_codeis done beforecomplete_mathandcomplete_markup, so codes for check interpolation are all removed. Also, inglobalsandcomplete_math|markup|code, use abovemetionedLinkedList::modeto checkmode.