5

按下回车键并且代码段退出其编辑模式后,是否可以替换 Visual Studio 代码段文字中的文本?

例如,给定这样的片段:

 public void $name$
 {
   $end$
 }

如果我输入 $name$ 为:

 My function name

是否可以让 Visual Studio 将其更改为:

 My_function_name

或者

 MyFunctionName
4

2 回答 2

7

多年后,对于仍然遇到此问题的任何人,都有一个答案:

"Replace Whitespaces": {
    "prefix": "wrap2",
    "body": [
        "${TM_SELECTED_TEXT/[' ']/_/gi}",
    ],
    "description": "Replace all whitespaces of highlighted Text with underscores"
},

将此添加到您的用户片段中。或者,您可以添加这样的键盘快捷键:

{
    "key": "ctrl+shift+y",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
        "snippet": "${TM_SELECTED_TEXT/[' ']/_/gi}"            
    }
},

希望这可以帮助将来遇到此问题的任何人

于 2020-04-03T10:08:25.243 回答
1

这很棒。我用它来用引号将东西包装在一个函数中。如果选择有引号,它可以删除引号。在片段部分,它似乎分解为:

TM_SELECTED_TEXT - this is the input
[ ' '] - regex find
_ - regex replace
gi - global flag for regex

所以我想要的是 change: "User logged in"into:<%= gettext("User logged in") %> 为此在我使用的片段中:

"body": ["<%= gettext(\"${TM_SELECTED_TEXT/['\"']//gi}\") %>"],

注意:您需要对正则表达式中的引号进行转义,因此:“变为\”。

于 2020-09-27T05:30:47.287 回答