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

Add "context" by regex #47

Closed
yfzhao20 opened this issue May 19, 2021 · 7 comments
Closed

Add "context" by regex #47

yfzhao20 opened this issue May 19, 2021 · 7 comments

Comments

@yfzhao20
Copy link

my friend from NJU gives a method to add math context. He uses regex to achieve that.

https://github.com/OrangeX4/hsnips

He add this code into extension.ts:

function isMathEnvironment(editor: vscode.TextEditor) {
        let text = editor.document.getText(new vscode.Range(new vscode.Position(0, 0), editor.selection.start))
        const reg = /(\\begin\{equation\}[^\$]*?\\end\{equation\})|(\\begin\{equation\*\}[^\$]*?\\end\{equation\*\})|(\\\[[^\$]*?\\\])|(\\\([^\$]*?\\\))|(\$\$[^\$]+\$\$)|(\$[^\$]+?\$)/g
        text = text.replace(reg, '')
        if (text.indexOf('$') == -1 && text.indexOf('\\(') == -1 && text.indexOf('\\[') == -1 && text.indexOf('\\begin{equation}') == -1 && text.indexOf('\\begin{equation\*}') == -1) {
            return false
        } else {
            return true
        }
    }

and changes something in other files. You can add a flag m add use that, like:

snippet inv "inv" iAm
^{-1}
endsnippet

we can find the extension "hypersnips for math" in VSCode market.

(I 'm not so familiar with Enelish🤣🤣🤣)

@yfzhao20
Copy link
Author

and context does not work in the new version 😂. I don't know how to make it work.

@draivin
Copy link
Owner

draivin commented May 19, 2021

Hi there, I'll suppose you're using LaTeX, if you're using markdown, please let me know.

If that is the case, the example given in the README should work for you, let me change it a little so you can see how to do it more generally:

Here we define two automatic snippets, inv and tt, that only work inside math mode

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

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

context math(context)
snippet tt "text" Ai
\text{$1}
endsnippet

Now let me explain how it works:

  • First of all, the global keyword starts a code block where you can define variables and functions that will be available for any javascript code in the current hsnips file.
  • After that, we have the context keyword: it should be followed by a javascript expression, and every time you type a character, that javascript expression is executed, and the following snippet will only be "active" in case the executed expression evaluated to true.
  • Inside the context expression, we have access to a variable called context, which contains information regarding the current scope, as can be seen in the README.

In this case, suppose we just typed tt inside a math block in LaTeX, what happens is that the context expression above the tt snippet will be called with the following context parameter

{
   scopes: [
    "support.class.math.block.environment.latex",
    "meta.math.block.latex",
    "text.tex.latex",
  ]
}

which in turn will call the math function defined above with this same parameter, and as you can see, the math function returns true in case any of the scopes starts with "meta.math", which in this case it does, so the tt snippet will be available, and as it is an automatic snippet it will run.

More generally, you can see the context.scopes that will be available in a given context expression by running the vscode command Developer: Inspect Editor Tokens and Scopes in the textmate scopes field.

Implementing context like this is more general, as we are not only restricted to math blocks inside LaTeX documents, we could create a context expression that would evaluate to true inside comments in javascript files for example.

If you have any further doubts about how to use contexts please let me know, I'll be closing this issue now. 🙂

@draivin draivin closed this as completed May 19, 2021
@yfzhao20
Copy link
Author

Thanks for that!

However I'm very sorry because I'm using markdown😂. Through Inspecting Editor Tokens and Scopes I can confirm that the scope of the math blocks is meta.embedded.block.katex:

textmate scopes :
    meta.embedded.block.katex
    markup.inserted.math.inline.markdown
    meta.paragraph.markdown
    markup.list.numbered.markdown
    text.html.markdown

provided by extension "markdown all in one". I only changed meta.math into meta.embedded like that:

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

, but nothing had happened🤔.

Anyway the explanation is great, thanks!😀

@draivin
Copy link
Owner

draivin commented May 19, 2021

Personally, I'd use s => s.includes("katex") in your case, as that is the name of the markdown LaTeX renderer. Please try it and let me know how it goes.

@draivin
Copy link
Owner

draivin commented May 19, 2021

I see the issue now, it seems to be an issue in the hscopes library, I'll take a look into it and get back to you!

@draivin
Copy link
Owner

draivin commented May 19, 2021

Should be fixed on the latest hscopes version, thanks for the report!

@yfzhao20
Copy link
Author

thanks! I will try it later

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

2 participants