Is it possible to have a snippet that considers the length of my input?

Viewed 277

I would like to define a snippet for comments like

//************
//*  foo A1  *
//************

where I enter foo A1 and it would create a line with (6+ len(${1}) asterisks etc. - is that doable and if so, how?

2 Answers

While I am a big proponent of HyperSnips (see

[VSCODE]: Is there a way to insert N times the same characters,

VS Code: how to make a python snippet that after string or expression hitting tab will transform it and

VSCode Advanced Custom Snippets for how to use it),

it is instructive to see how to do this with just the built-in snippet functionality in vscode. Here is a snippet that does what you want:

"Custom Comment": {
    "prefix": ["cc2"],    // whatever trigger you want, then tab, write your info and tab again
    "body": [
        "//***${1/./*/g}***",
        "//*  $1  *",
        "//***${1/./*/g}***"
    ]
},

That just adds 3 asterisks to the beginning and 3 to the end of your added comment, each character of which is replaced by an asterisk as well.

custom comment demo

You can use the extension HyperSnips

snippet comment "Comment" A
``rv = '//' + '*'.repeat(t[0].length + 6)``
//*  $1  *
``rv = '//' + '*'.repeat(t[0].length + 6)``
endsnippet
Related