UPDATE
vscode v1.58 is adding a /camelcase
transform. which makes your use case very easy now.
"${1/(.)(.*)/${1:/upcase}${2:/camelcase}/}"
to go from blueberry_pie_with_a_cup_of_coffee
to BlueberryPieWithACupOfCoffee
"${1/(.*)/${1:/camelcase}/}"
// just capture the whole thing
to go from blueberry_pie_with_a_cup_of_coffee
to blueberryPieWithACupOfCoffee
vscode's implementation uses this regex: [a-zA-Z0-9]
so anything other than those characters will be seen as separators - and the following "word" will be capitalized.
Previous answer::
To transform an arbitrary number of "_" separated words into CamelCase try:
EDIT: In October, 2018 (but not yet added to snippet grammar documentation as of February, 2020) vscode added the /pascalcase
transform, see commit. I have modified the code below to use the /pascalcase
transform. It only works for the some_file => SomeFile
type of CamelCase though.
But it works with many characters as separators, these all work:
blueberry_pie_with_a_cup_of_coffee
blueberry-pie-with-a-cup-of-coffee
blueberry-pie-with_a-cup-of_coffee
blueberry-pie-with.a-cup-of.coffee
blueberry*pie-with.a*cup-of.coffee
blueberry*pie@with.a*cup1of.coffee
blueberry*pie@with.a*cup1of.coffee
"camelCase": {
"prefix": "_cc",
"body": [
// "${TM_FILENAME_BASE/([a-z]*)_+([a-z]*)/${1:/capitalize}${2:/capitalize}/g}"
"${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}"
],
"description": "Transform to camel case"
},
carrot_cake.txt
-> CarrotCake
blueberry_pie_with_a_cup_of_coffee.js
-> BlueberryPieWithACupOfCoffee
[I assume CamelCase
is the form you want, there are others, such as camelCase
.]
For camelCase
:
"${TM_FILENAME_BASE/([a-z]*)[-@_.*0-9]+([a-z]*)/$1${2:/capitalize}/g}"
put your desired list of separators in the [-@_.*0-9]+
part. The +
quantifier allows you to use carrot--cake
for example - multiple separators
between words. Thanks to the other answer for using the [list the separators]
part of the regex.
Note that the "g" flag at the end is doing most of that work for you in getting however many matches there are beyond the two explicitly captured.
I left the capture groups as ([a-z]*)
as you had them. You may want to use ([A-Za-z0-9]*)
for more flexibility.