本文介绍了在 Visual Studio Code (vscode) 上包装选择片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在触发时创建一个片段,它将围绕给定的文本.目前我的片段是:

{函数创建助手":{"前缀": "_w",身体": ["公共函数$TM_SELECTED_TEXT(){","$1","}",],"description": "根据文本选择创建一个函数"}}

结果:

我要做的是:

  1. 选择文本.
  2. 写前缀 (_w)
  3. 按 Tab

结果:

公共函数(){}

但我在期待

公共函数person(){}

关于如何制作此代码段或如何正确触发它的任何想法?

解决方案

参见


旧答案:

如果您使用热键触发它,您可以使用 $TM_SELECTED_TEXT:

{key":cmd+k 1",命令":editor.action.insertSnippet",当":editorTextFocus",参数":{//"langId": "csharp",名称":函数创建助手"}}

I want to create an snippet when triggered it will surround the given text. Currently my snippet is:

{
  "Function Creator Helper": {
    "prefix": "_w",
    "body": [
      "public function $TM_SELECTED_TEXT () {",
      "  $1",
      "}",
    ],
    "description": "Creates a function given the text selection"
  }
}

This results on:

What I do is:

  1. Select the text.
  2. Write the prefix (_w)
  3. Press Tab

This results on:

public function  () {

}

But I was expecting

public function person () {

}

Any ideas on how can I make this snippet or how can I triggered it correctly?

解决方案

See https://stackoverflow.com/a/48676522/836330 Your example will work, as of vscode v1.49, as you have it. Vscode snippets have been updated to "remember" your selected text even though you seemingly overwrite it with your snippet prefix.


Older answer:

You can use $TM_SELECTED_TEXT if you trigger it with a hotkey:

{
  "key": "cmd+k 1",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    //  "langId": "csharp",
    "name": "Function Creator Helper"
  }
}

这篇关于在 Visual Studio Code (vscode) 上包装选择片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:28