Wrap Selection Snippet on Visual Studio Code (vscode)

I want to create a fragment at startup, it will surround the given text. Currently, my snippet:

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

It leads to:

Wrapping snippet

What I am doing is:

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

It leads to:

public function  () {

}

But I was expecting

public function person () {

}

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

+6
source share
5 answers

${TM_SELECTED_TEXT}selected text ${TM_SELECTED_TEXT}as ${TM_SELECTED_TEXT}, not how $TM_SELECTED_TEXT.

edit: as commented below, this is not the case for this particular use case

+5
source

. , F1, Insert Snippet, .

+2

${TM_SELECTED_TEXT} .

${selectedText} : https://github.com/Microsoft/vscode/pull/39483#issuecomment-383552677

:

"JS Block Quote": {
    "prefix": "c2",
    "body": [
        "/* ${selectedText} */",
    ],
    "description": "JS Block Quote"
}

: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables

. ${selectedText} . ${CLIPBOARD}. :(

0

Mitches:

"JS Block Quote": {
    "prefix": "c2",
    "body": [
        "/* $TM_SELECTED_TEXT */",
    ],
    "description": "JS Block Quote" }

: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables .

vscode v1.30.2

0

You can use it $TM_SELECTED_TEXTif you run it using the hotkey:

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

Or you don’t need to choose what you want to wrap if you use TM_CURRENT_LINE- assuming you want to wrap the entire string.

0
source

Source: https://habr.com/ru/post/1664356/


All Articles