visual-studio-codevscode-snippets

Snippet: transorm TM_DIRECTORY base dir from snake_case to PascalCase


I'm trying to get "base directory" from TM_DIRECTORY variable (which gives full path) to be transformed to PascalCase (which is in snake_case).

So, for /this/is/path/to/base_dir, I want to get BaseDir

This is what I've got so far:

${TM_DIRECTORY/(^.+\\/(.*)$)/${2:/capitalize}/g} from this

which gives me:

Base_dir for /this/is/path/to/base_dir

I feel like I have to somehow incorporate ${TM_DIRECTORY/((^[a-z])|_([a-z]))/${2:/upcase}${3:/upcase}/g} but don't know how.


Solution

  • vscode v1.106 is adding a new variable TM_DIRECTORY_BASE which makes your transform easier (plus \pascalcase might not have been available at the time of this answer.

    "${TM_DIRECTORY_BASE/(.*)/${1:/pascalcase}/}"
    

    Just capture everything, in your case base_dir and pascalCase it which produces BaseDir.


    Use this:

      "${TM_DIRECTORY/(^.+\\/(.*)$)/${2:/pascalcase}/}",
    

    the pascalcase option is unfortunately not documented.

    If that option wasn't available I would suggest this:

      "${TM_DIRECTORY/.*\\/(.*)_(.*)$/${1:/capitalize}${2:/capitalize}/}",
    

    the idea being to get the last directory in two capture groups, capitalize each and ignore the underscore separator (i.e., it doesn't appear in the transform part).

    To generalize that, if you had other name separators in your directory name besides the underscore you could use this [_-] in that middle part - just include other possible separators.