Search code examples
visual-studio-codecode-snippets

Snippet variable transform in some locations


For example:

export const setVisibilityFilter = (filter) => ({
  type: 'SET_VISIBILITY_FILTER',
  filter
})

The name of the function setVisibilityFilter has camelCase. The same in the written version in uppercase characters separated by underscores: SET_VISIBILITY_FILTER

I want to make a snippet out of it. I want that after entering any text in the snippet variable, in the second place the uppercase characters separated by underscores were created from this text.

So that I do not have to write it for the second time, only in large letters ...

Is it possible in a snippet and how to do it?


Solution

  • vscode 1.25 did add transforms on snippet tabstops, so no need for a pre-selection. This version works, and with any number of camelCase words, like "camelCase", "camelCaseCaseCase", etc.

    "test3": {
        "prefix": "seps",
        "body": [
            "export const $1 = (filter) => ({",
                "type: '${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}${2:+_}$3${4:/upcase}/g}',",
                "filter",
            "})"
        ],
        "description": "underscore separators"
    }
    

    See my similar answer convert from camelCase to all CAPS_WITH_SEPARATORS