最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vscode snippets - In VS Code, is there a way (or extension) to run a command ( I.E. 'editor.action.insertSnippet&#39

programmeradmin1浏览0评论

I have a snippet called Quick Log Write-Host defined like this:

"Quick Log Write-Host": {
    "prefix": ["quicklog"],
    "body": [
            "Write-Host -f Green \"`$TM_SELECTED_TEXT:\" $TM_SELECTED_TEXT"
    ],
    "description": "Quick Log"
},

I then have a keybinding defined to Ctrl+Q like this:

{
    "key"     :  "ctrl+q",
    "command" :  "editor.action.insertSnippet",
    "args"    :  { "name": "Quick Log Write-Host" },
    "when"    :  "editorTextFocus && editorLangId == powershell"
},

So now, when I select a variable and press Ctrl+Q, it transforms it into a logging string so I can easily inspect the value of that variable when I run my script. It's just a useful debugging tool.

A simple example:

$MyObjectCollection | % {
    $_
}

Now I select $_ and press Ctrl+Q, and the code becomes:

$MyObjectCollection | % {
    Write-Host -f Green "`$_:" $_
}

This is useful when I want to inspect a variable quickly.

But it doesn't work on multiple selections. I'd like to be able to select multiple lines and perform the same command on each line one at once.

What I want to do is start with this:

$HArrMemberType.GetType()
$HArrMemberType.GetType().BaseType
$HArrMemberType.GetType().FullName
$HArrMemberType.GetType().Name
$HArrMemberType.GetType().IsEnum
$HArrMemberType.GetType().IsValueType
$HArrMemberType.GetType().MemberType

Then select all of the above, press a shortcut, and have that shortcut transform my selection to:

Write-Host -f Green "`$HArrMemberType.GetType():" $HArrMemberType.GetType()
Write-Host -f Green "`$HArrMemberType.GetType().BaseType:" $HArrMemberType.GetType().BaseType
Write-Host -f Green "`$HArrMemberType.GetType().FullName:" $HArrMemberType.GetType().FullName
Write-Host -f Green "`$HArrMemberType.GetType().Name:" $HArrMemberType.GetType().Name
Write-Host -f Green "`$HArrMemberType.GetType().IsEnum:" $HArrMemberType.GetType().IsEnum
Write-Host -f Green "`$HArrMemberType.GetType().IsValueType:" $HArrMemberType.GetType().IsValueType
Write-Host -f Green "`$HArrMemberType.GetType().MemberType:" $HArrMemberType.GetType().MemberType

Does anyone know if this kind of transformation is possible without writing my own VS Code extension? Whether through an extension, or some native VS Code shortcut settings?

Any help would be greatly appreciated!

I have a snippet called Quick Log Write-Host defined like this:

"Quick Log Write-Host": {
    "prefix": ["quicklog"],
    "body": [
            "Write-Host -f Green \"`$TM_SELECTED_TEXT:\" $TM_SELECTED_TEXT"
    ],
    "description": "Quick Log"
},

I then have a keybinding defined to Ctrl+Q like this:

{
    "key"     :  "ctrl+q",
    "command" :  "editor.action.insertSnippet",
    "args"    :  { "name": "Quick Log Write-Host" },
    "when"    :  "editorTextFocus && editorLangId == powershell"
},

So now, when I select a variable and press Ctrl+Q, it transforms it into a logging string so I can easily inspect the value of that variable when I run my script. It's just a useful debugging tool.

A simple example:

$MyObjectCollection | % {
    $_
}

Now I select $_ and press Ctrl+Q, and the code becomes:

$MyObjectCollection | % {
    Write-Host -f Green "`$_:" $_
}

This is useful when I want to inspect a variable quickly.

But it doesn't work on multiple selections. I'd like to be able to select multiple lines and perform the same command on each line one at once.

What I want to do is start with this:

$HArrMemberType.GetType()
$HArrMemberType.GetType().BaseType
$HArrMemberType.GetType().FullName
$HArrMemberType.GetType().Name
$HArrMemberType.GetType().IsEnum
$HArrMemberType.GetType().IsValueType
$HArrMemberType.GetType().MemberType

Then select all of the above, press a shortcut, and have that shortcut transform my selection to:

Write-Host -f Green "`$HArrMemberType.GetType():" $HArrMemberType.GetType()
Write-Host -f Green "`$HArrMemberType.GetType().BaseType:" $HArrMemberType.GetType().BaseType
Write-Host -f Green "`$HArrMemberType.GetType().FullName:" $HArrMemberType.GetType().FullName
Write-Host -f Green "`$HArrMemberType.GetType().Name:" $HArrMemberType.GetType().Name
Write-Host -f Green "`$HArrMemberType.GetType().IsEnum:" $HArrMemberType.GetType().IsEnum
Write-Host -f Green "`$HArrMemberType.GetType().IsValueType:" $HArrMemberType.GetType().IsValueType
Write-Host -f Green "`$HArrMemberType.GetType().MemberType:" $HArrMemberType.GetType().MemberType

Does anyone know if this kind of transformation is possible without writing my own VS Code extension? Whether through an extension, or some native VS Code shortcut settings?

Any help would be greatly appreciated!

Share Improve this question edited Jan 20 at 4:54 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 19 at 21:48 fmotion1fmotion1 5319 silver badges22 bronze badges 1
  • you can use Multi Cursor. Place a cursor on each line, select the line and execute the key bind or type the snippet prefix – rioV8 Commented Jan 20 at 8:44
Add a comment  | 

1 Answer 1

Reset to default 2

If your text is contiguous like you showed then this keybinding will work:

  "key": "ctrl+q",
  "command": "runCommands",
  "args": {
    "commands": [
      "editor.action.insertCursorAtEndOfEachLineSelected",
      "cursorHomeSelect",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "name": "Quick Log Write-Host"
          // "snippet": "Write-Host -f Green \"`$TM_SELECTED_TEXT:\" $TM_SELECTED_TEXT"  // or this
        }
      }
    ]
  },
  "when": "editorTextFocus && editorLangId == powershell"
},

The key is to "split" the lines first, with the editor.action.insertCursorAtEndOfEachLineSelected command and then the snippet will be applied on a line-by-line basis.

If you are selecting text that is not on adjoining lines, just put a cursor at the end of each line you want to change.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论