Skip to content
Advertisement

Execute function in vscode snippet

Is it possible to run a function during expansion of snippet?

javascript.json – vscode snippet file:

function add() {
    return 2+2;
}

"Print to console": {
    "prefix": "log",
    "body": [
        "console.log("Addition", add());"
    ],
    "description": "Log output to console"
}

index.js – project file:

// I wrote "log" and intellisense currently provides
console.log("Addition", add());

// But I need
console.log("Addition", 4);

The obvious reason might be snippet is a json file, it’s not a javascript file so it can’t run code, just show syntax as it with cursor positions $1, $2 etc.

Or is it possible through a vscode plugin?

Advertisement

Answer

Using Hypersnips vscode extension as suggested by rioV8, I’ve a working hsnips which might help you.

all.hsnips OR javascript.hsnips:

global
function add() {
  return 2+2;
}
endglobal

snippet log "Log output to console"
``
rv = `console.log("Addition", ${add()})`
``
endsnippet
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement