VSCode片段,将由下划线分隔的小写字符串转换为CamelCase?

时间:2019-12-02 18:41:25

标签: json regex visual-studio-code camelcasing vscode-snippets

我正在 VSCode 中编写自定义代码段,以帮助我轻松定义自定义类方法。我需要能够输入字符串'formatted_like_this',并让regex在某些地方转换该字符串,使其变为'FormattedLikeThis'?

要在php.json中编写的自定义代码段:(请参阅“需要在这里输入正则表达式”以了解我苦苦挣扎的地方)

"New Custom Class Method For Variable": {
    "prefix": "contcmpffv",
    "body": [
        "protected $$1 = null;",
        "public function get${NEED HELP WITH REGEX HERE}()",
        "{",
        "\t$0",
        "}"
    ],
    "description": "Controller Class Method Public Function For Variable"
}

我想要的工作流程: 1.键入contcmpffv 2.提示匹配代码段时,按Enter键 2.代码段提示我是$ 1

所需的输出(在提示您输入$ 1时输入“ test_input_string”):

protected $test_input_string = null;
public function getTestInputString()
{
    *cursor resolves here (due to $0)*
}

1 个答案:

答案 0 :(得分:2)

尝试:

"body": [
    "protected $$1 = null;",
    "public function get${1/(.*)/${1:/pascalcase}/}()",
    "{",
    "\t$0",
    "}"
],

它使用未记录的pascalcase转换-已经存在了一段时间。在这种情况下,它会为您完成所有工作。

如果没有pascalcase,可以使用以下方法:

"public function get${1/([^_]*)_*/${1:/capitalize}/g}()",