GithubHelp home page GithubHelp logo

Add "context" by regex about hsnips HOT 7 CLOSED

draivin avatar draivin commented on September 2, 2024
Add "context" by regex

from hsnips.

Comments (7)

draivin avatar draivin commented on September 2, 2024 1

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

from hsnips.

yfzhao20 avatar yfzhao20 commented on September 2, 2024

and context does not work in the new version πŸ˜‚. I don't know how to make it work.

from hsnips.

draivin avatar draivin commented on September 2, 2024

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. πŸ™‚

from hsnips.

yfzhao20 avatar yfzhao20 commented on September 2, 2024

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!πŸ˜€

from hsnips.

draivin avatar draivin commented on September 2, 2024

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.

from hsnips.

draivin avatar draivin commented on September 2, 2024

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!

from hsnips.

yfzhao20 avatar yfzhao20 commented on September 2, 2024

thanks! I will try it later

from hsnips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.