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

Implement context matching #6

Closed
draivin opened this issue Oct 9, 2019 · 17 comments
Closed

Implement context matching #6

draivin opened this issue Oct 9, 2019 · 17 comments

Comments

@draivin
Copy link
Owner

draivin commented Oct 9, 2019

Implement context as in UltiSnips, as can be seen in the UltiSnips manual at the section UltiSnips-custom-context-snippets.

Maybe look into scope-info so we can provide that information to the context matchers.

@draivin
Copy link
Owner Author

draivin commented Jun 3, 2020

Apparently scope-info is not maintained anymore, so getting scope information is still blocked on microsoft/vscode#580.

@universemaster
Copy link

I have exactly the same use-case as @medwatt in #19 - implementing @gillescastel 's snippets in vscode.

Other extensions still use scopes via vscode-textmate see example here: restore-scope.

Can we not use the vscode-textmate module in this extension? Particularly interested in math scopes inside markdown.

@gruvw
Copy link

gruvw commented May 17, 2021

You might want to take a look at the LaTeX-Utilities VSCode extension that implemented context matching for math mode in latex. It works pretty well. See their documentation about it.

@draivin
Copy link
Owner Author

draivin commented May 18, 2021

Hey @gruvw, it seems LaTeX-Utilities has its own parsing logic specific to latex (see here). I'm not sure that is something we wish to implement.

@universemaster
Copy link

I really do think that latex snippets is the killer application this extension, though.

Almost all of my snippets relate to latex (well, Katex in markdown), and it's really annoying when they trigger outside of a math context.

Similarly, I've had to somewhat cripple some of my snippets to make them trigger only in a (likely) math context and often that makes my snippets less useful, and need more key presses.

@gruvw
Copy link

gruvw commented May 18, 2021

@draivin What about using their (LaTeX-Utilities) parsing logic in your extension ?

@universemaster
Copy link

Or, to make it more generic, maybe use the same logic as the vscode extension markdown+math uses (extension used by 160k people).

https://github.com/goessner/markdown-it-texmath/blob/4202e2f15fb30b776bc5acbab03296b27c2d2886/texmath.js#L163

@draivin
Copy link
Owner Author

draivin commented May 18, 2021

I think if we are really going down the custom parser route, we should at least try to do it as the late scope-info did, that extension read your language definition files, and completely reparsed the file on the extension side, giving access to exactly the same scope info as vscode has.

I'll think some more about this, and see how viable it would be to resurrect that extension.

@universemaster
Copy link

universemaster commented May 18, 2021

After considering this I have been reminded that when using Markdown+Math, the editor does not add a scope to display math if it has a line break. I presume this is a limitation of vscode? Note that math does render in the preview correctly.

See
https://github.com/goessner/mdmath/blob/master/syntaxes/dollars.json

This is recognised (a little unclearly) in the readme under "Is formula highlighting broken?" https://github.com/goessner/mdmath/blob/master/readme.md

So, for instance, Markdown+Math will apply a math scope for

This is display math without a linebreak $$\int_1^x$$

But Markdown+Math will not apply a math scope for the below, even though it is valid, and renders correctly in preview.

The following equation is relevant:

$$ \int_1^x $$

So, in conclusion, we cannot currently rely on other extensions adding the correct scope. This may be due to a limitation of vscode in applying a scope.

But, for now, if we are just reading the scope from the editor (by resurrecting scope-info) sometimes we will be writing math but the scope will not reflect this.

However, I'm not going to let the perfect be the enemy of the good. If we can have hsnips use the existing scopes in the editor, this will still be MUCH better than the current situation.

@draivin
Copy link
Owner Author

draivin commented May 18, 2021

This is not an issue on vscode's side, the problem (as is the case with all this scope stuff) is that mdmath can not access vscode's scope info, so it relies on its own regular expression parser, which is not perfect, as you noticed.

You can see that in the example you posted, vscode has the correct scope information (you can check the scope information by running the command Developer: Inspect Editor Tokens and Scopes), which is meta.embedded.block.katex.

@universemaster
Copy link

Inside the math of

The following equation is relevant:

$$ \int_1^x $$

I get the scope as

meta.paragraph.markdown
text.html.markdown

I have vscode version 1.55.2 (Universal)

Is yours coming from a different extension? Or are you using the latest vscode (I had to downgrade to last months)?

@draivin
Copy link
Owner Author

draivin commented May 18, 2021

Probably the extension that is supplying the markdown syntax definition files for mine is different than yours, (I think the extension supplying the syntax files in my case is "Markdown All in One", as seen here).

@universemaster
Copy link

That's correct. I have enabled "Markdown All in One" and now I get

support.function.katex
meta.function
meta.embedded.block.katex
markup.inserted.math.display.markdown
text.html.markdown

So, OK, we can make vscode recognise math scopes correctly.
https://github.com/yzhang-gh/vscode-markdown/tree/master/syntaxes

OK, so, I'm now all in favour of resurrecting scope-info!

@draivin
Copy link
Owner Author

draivin commented May 18, 2021

Hey guys, the latest version of vscode should have access to contexts and scopes, could you please try it and let me know how it goes?

Here is an example of how to use the new feature to expand the inv command inside LaTeX math blocks:

global
function math(context) {
    return context.scopes.some(s => s.startsWith("meta.math"));
}
endglobal

context math(context)
snippet inv "inverse" Ai
^{-1}
endsnippet

@gruvw
Copy link

gruvw commented May 18, 2021

Yes it seems to work well for me! Needs to be more tested though.

@gruvw
Copy link

gruvw commented May 18, 2021

I don't know if I should open a new issue, but I found a weird behavior in the new version:

For some reasons, this is not working anymore:

snippet a1 "Issue 1" A
Not working
endsnippet

snippet `a2` "Issue 2" A
Not working
endsnippet

snippet `a\/` "Issue 3" A
Not working
endsnippet

If the trigger is not ending with a [A-Za-z] character, it will not work.
Edit: ending with # or > works

@draivin
Copy link
Owner Author

draivin commented May 18, 2021

You should probably create a new issue, but I can't reproduce the problem with any of the 3 examples.

@draivin draivin closed this as completed May 18, 2021
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

No branches or pull requests

3 participants