4

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?

MBaas
  • 7,248
  • 6
  • 44
  • 61

2 Answers2

6

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

Mark
  • 143,421
  • 24
  • 428
  • 436
  • 2
    Beautiful. Native. +1. “Instructive” should say “better” :) extensions are great, but not downloading one is better – soulshined Aug 17 '20 at 06:00
  • Thank you - very nice, indeed - especially because it's native - as @soulshined said ;) – MBaas Aug 17 '20 at 06:53
  • It is good to know HyperSnips because it is more powerful than the native snippets - able to run javascript on input for example and replace with the result. – Mark Aug 17 '20 at 23:07
2

You can use the extension HyperSnips

snippet comment "Comment" A
``rv = '//' + '*'.repeat(t[0].length + 6)``
//*  $1  *
``rv = '//' + '*'.repeat(t[0].length + 6)``
endsnippet
rioV8
  • 24,506
  • 3
  • 32
  • 49