Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 984
MBaas Avatar asked Oct 22 '25 22:10

MBaas


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

like image 192
Mark Avatar answered Oct 25 '25 21:10

Mark


You can use the extension HyperSnips

snippet comment "Comment" A
``rv = '//' + '*'.repeat(t[0].length + 6)``
//*  $1  *
``rv = '//' + '*'.repeat(t[0].length + 6)``
endsnippet
like image 23
rioV8 Avatar answered Oct 25 '25 20:10

rioV8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!