skip to Main Content

I want VSCode to generate my classname based on my filename:

Filename: test-class-file.php should become: Test_Class_File
I found the following examples:

  1. "${TM_FILENAME_BASE/[-]/_/g}" Becomes: test_class_file
  2. "${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/}" Becomes: TestClassFile

The official docs Do not mention chaining or anything like that.
There also doesn’t appear to be a way to store something in a temp variable.
How can I combine these 2 into one transform/regex action?

2

Answers


  1. You can use

    "${TM_FILENAME_BASE/([^-])([^-]*)|(-)/${1:/upcase}${2:/downcase}${3:+_}/g}"
    

    This will work like this:

    • ([^-])([^-]*) – captures the first non-hyphen char into Group 1 and the rest of non-hyphen chars into Group 2
    • (-) – captures any -s into Group 3.

    When Group 1 matches, it is turned into uppercase, Group 2 contents is turned to lower, and every - is replaced with _.

    Login or Signup to reply.
  2. This is much simpler:

      "${TM_FILENAME_BASE/(-)|([^-]+)/${1:+_}${2:/capitalize}/g}"
    

    Just use the captitalize transform on the non-hyphen groups.

    Helpful link: VS Code snippet to capitalize each word in filename and tranform filename to Capitalize sentence with a regex and how can I transform a variable to title-case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search