Snippet variable transform in some locations

倖福魔咒の 提交于 2020-01-03 05:17:07

问题


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?


回答1:


vscode 1.25 did add transforms on snippet placeholders, 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




回答2:


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)



来源:https://stackoverflow.com/questions/49119284/snippet-variable-transform-in-some-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!