lukaszpolowczyk
lukaszpolowczyk

Reputation: 755

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?

Upvotes: 0

Views: 1058

Answers (2)

Mark
Mark

Reputation: 182771

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

Upvotes: 1

Stepan Burguchev
Stepan Burguchev

Reputation: 391

You might be looking for something like this:

    "Redux_Action_Creator": {
        "prefix": "act",
        "body": [
            "export const ${TM_SELECTED_TEXT} = (${1}) => ({",
            "\ttype: '${TM_SELECTED_TEXT/(^[a-z$]+).*/${1:/upcase}/g}${TM_SELECTED_TEXT/[a-z$]*([A-Z0-9][a-z$]+)/_${1:/upcase}/g}'",
            "\t$0",
            "});",
            ""
        ],
        "description": "[Surrounding] Export action creator returning plain action"
    }

This snippet must be called via 'insert snippet' command, having the action creator name selected (example usage: http://recordit.co/o8u3lvPsIe).

Note: The use of TM_SELECTED_TEXT is necessary as VSCode doesn't support transforming snippet placeholders at the moment (see feature request: https://github.com/Microsoft/vscode/issues/34683)

Upvotes: 1

Related Questions