Snippet variable transform in some locations
Asked Answered
H

2

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?

Hoffert answered 5/3, 2018 at 20:52 Comment(0)
A
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

Ability answered 19/7, 2018 at 18:16 Comment(0)
B
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)

Brachial answered 23/3, 2018 at 16:40 Comment(0)
A
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

Ability answered 19/7, 2018 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.