-
-
Save cmstead/7ad64539d13dfdb5a2591bc3be3c8eeb to your computer and use it in GitHub Desktop.
// If you want to convert a PascalCase variable to camelCase, you can do the following: | |
// Where `n` is the tab stop you want to reference | |
${n/^(.)(.*)$/${1:/downcase}${2}/} | |
// Example: ${1:This is my original} => ${1/^(.)(.*)$/${1:/downcase}${2}/} | |
// If you want to convert a camelCase variable to PascalCase, you can do the following: | |
// Where `n` is the tab stop you want to reference | |
${n/^(.)(.*)$/${1:/upcase}${2}/} | |
// Example: ${1:This is my original} => ${1/^(.)(.*)$/${1:/upcase}${2}/} | |
My only issue was forgetting to hit tab while designing the snippet as the transform is not applied until you attempt to navigate to the next tabstop. Thank you for this regex and options combination as it is a very common and useful one.
Seems like there is /camelcase and /pascalcase transform, which could make everything simpler.
But this acts like it's meant to convert snake_case to camelCase, not PascalCase to camelCase.
There is also /capitalize transform, which can be used for converting camelCase to PascalCase, but there is no opposite transform.
Therefore these do give expected result,
${1:snake_to_camel} => ${1/(.*)/${1:/camelcase}/} // result is "snakeToCamel"
${2:snake_to_pascal} => ${2/(.*)/${1:/pascalcase}/} // result is "SnakeToPascal"
${3:camelToPascal} => ${3/(.*)/${1:/capitalize}/} // result is "CamelToPascal"
But these do not give expected result.
${1:PascalToCamel} => ${1/(.*)/${1:/camelcase}/} // result is "pascaltocamel"
${2:camelToPascal} => ${2/(.*)/${1:/pascalcase}/} // result is "Cameltopascal"
What a mess.
thank you
Answer to vscode snippet transform file name in the snippet to camel case or pascal case variable
given file_name_foo.ext
file
${TM_FILENAME_BASE/^(.*)_foo$/${1:/pascalcase}/} ==> FileName
${TM_FILENAME_BASE/^(.*)_foo$/${1}/} ==> file_name
${TM_FILENAME_BASE/^(.*)_foo$/${1:/upcase}/} ==> FILE_NAME
:/camelcase
works as well.
Thanks for helping @cgbeutler!