draivin / hsnips

HyperSnips: a powerful snippet engine for VS Code, inspired by vim's UltiSnips
MIT License
154 stars 25 forks source link

Context (scope) matching #134

Closed hinell closed 1 year ago

hinell commented 1 year ago

Currently we don't have a sane context matching syntax, so I wrote a small script that can be used to match snippets against major languages:


global

    function ctxmatcher (scope,name,c) {
        let v = c.scopes.some(txCntx => txCntx.startsWith(scope));
        return v
    };

    let langs = [
          [ "source.js"         , "javascript"    ]
        , [ "source.ts"         , "typescript"    ]
        , [ "source.shell"      , "shell"         ]
        , [ "source.c"          , "clang"         ]
        , [ "source.cpp"        , "cpp"           ]
        , [ "source.py"         , "py"            ]
        , [ "source.batchfile"  , "batch"         ]
        , [ "text.html.markdown", "md"            ]
    ];

    for (let i=0; i < langs.length - 1; i++) {
        let [ scope, name ] = langs[i];
        if(this[name] !== null){
            this[name] = ctxmatcher.bind(this, scope, name)
        }
    };
    // Use it like:
    // context javascript(context)
    // snippet ... 
    // endsnippet

endglobal
# Example how to use
context javascript(context)
snippet hsnip.test.js "Test scope" Wi
    This snippet is expanded only in JavaScript!
endsnippet

Extend the langs = [...] array for more languages. Hope it helps!

draivin commented 1 year ago

Thanks for the snippet, but I do believe this is more appropriate as a discussion topic!