draivin / hsnips

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

`${snip.tabstop()}` in global function throws "snip is not defined" error. #104

Closed yfzhao20 closed 2 years ago

yfzhao20 commented 2 years ago

I'm modifying my hsnips script to fit HyperSnips 0.2.7. Now I meet a new problem.

test code:

global
    function test(index){
        var result = ""
        for (var i = 0; i < index ; i++){
            result += ("Test" +`${snip.tabstop(i+1)}` +"it! \n")
        }
        return result
    }
endglobal

snippet `test(\d)` "test" iA
``rv=test(m[1])``
endsnippet

Then create test.md and then input test3. Wanted result is

Test | it! 
Test | it! 
Test | it! 

However it throws "snip is not defined" error.

But if I move the definition of function into snippet block, like that:

snippet `test(\d)` "test" iA
``
    function test(index){
        var result = ""
        for (var i = 0; i < index ; i++){
            result += ("Test " +`${snip.tabstop(i+1)}` +" it! \n")
        }
        return result
    }
rv=test(m[1])
``
endsnippet

Then Everything will be OK.

So how to fix that? I think snip cannot be used in global block, but I cannot find the solution. 🧐

Thanks!

yfzhao20 commented 2 years ago

I know how to solve that. Just set one more parameter and then throw snip into it in snippet blocks:

global
    function test(snippet, index){
        var result = ""
        for (var i = 0; i < index ; i++){
            result += ("Test " +`${snippet.tabstop(i+1)}` +" it! \n")
        }
        return result
    }
endglobal

snippet `test(\d)` "test" iA
``rv=test(snip, m[1])``
endsnippet

image