microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.18k stars 29.29k forks source link

Snippet variables kebabcase transform #145654

Open RayPS opened 2 years ago

RayPS commented 2 years ago

Currently we have upcase, downcase, capitalize, camelcase, pascalcase transforms.

Example for adding kebabcase transform:

TM_FILENAME_BASE is UserProfileItem

"${TM_FILENAME_BASE/(.*)/${1:/kebabcase}/}"

Result:

user-profile-item
Also suggesting other cases that could be useful. Case Result Already Have
camelcase userProfileItem
pascalcase UserProfileItem
kebabcase user-profile-item
snakecase user_profile_item
dotcase user.profile.item
slashcase user/profile/item
titlecase User Profile Item
sentencecase User profile item
ArturoDent commented 2 years ago

In the meantime as a workaround, this extension Find and Transform use the built-in editor.action.transformToKebabcase command (in a keybinding, not a snippet) together with any snippet variable, like ${fileBasenameNoExtension} to accomplish what you want. For example, this keybinding (in keybindings.json) would insert the kebabed-case version of ${fileBasenameNoExtension} at the cursor:

{
  "key": "alt+s",         // whatever keybinding you want 
  "command": "findInCurrentFile",
  "args": {
    "replace": "${fileBasenameNoExtension}",
    "postCommands": ["editor.action.transformToKebabcase", "cancelSelection"]
  },
}
xianghongai commented 1 year ago

The following rules are not implemented by chatGPT. Need official native support:

Input:

Output: foo-bar-baz-qux

    "kebab-case": {
        "prefix": "kc",
        // "${CLIPBOARD/(\\w+)?[-_\\s]+(\\w+)/${1:/downcase}-${2:/downcase}/g}"
        // "${CLIPBOARD/(\\s+)|([A-Z]+)|[^\\w\\s]/${1:/downcase}-${2:/downcase}/g}"
        // "${CLIPBOARD/([A-Z]+)|([^\\w\\s])|([\\s_]+)/${1:/downcase}${2:+-}${3:/downcase}/g}"
        // "${CLIPBOARD/([^\\w\\s])|([\\s_]+)|([A-Z]+[^A-Z\\s]+)/${1:+-}${2:+-}${3:/downcase}/g}"       // "${CLIPBOARD/(\\s+)|([A-Z]+)|([\\s_/\\.-]+)|([^\\w\\s])|([a-z]+[^\\w\\s]*)/${1:+-}${2:+-}${3:+-}${4:+-}${5:/downcase}/gi}"
        // "${CLIPBOARD/[^a-zA-Z0-9]+/-/g}"
        "body": [
            "${CLIPBOARD/([A-Z]+|[A-Z]?[a-z]+)(?=$|\\W)/${1:/downcase}-/g}"
        ]
    },