Is it possible to run a function during expansion of snippet?
javascript.json – vscode snippet file:
JavaScript
x
12
12
1
function add() {
2
return 2+2;
3
}
4
5
"Print to console": {
6
"prefix": "log",
7
"body": [
8
"console.log("Addition", add());"
9
],
10
"description": "Log output to console"
11
}
12
index.js – project file:
JavaScript
1
6
1
// I wrote "log" and intellisense currently provides
2
console.log("Addition", add());
3
4
// But I need
5
console.log("Addition", 4);
6
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:
JavaScript
1
12
12
1
global
2
function add() {
3
return 2+2;
4
}
5
endglobal
6
7
snippet log "Log output to console"
8
``
9
rv = `console.log("Addition", ${add()})`
10
``
11
endsnippet
12