hsnips icon indicating copy to clipboard operation
hsnips copied to clipboard

Context (scope) matching

Open hinell opened this issue 1 year ago • 0 comments

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!

hinell avatar Oct 29 '22 19:10 hinell