0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.