问题描述
如何创建自定义快捷方式来生成将在 vscode 中打印选定变量的代码?
[1] arr = [1,2,3,4] # 我在 'arr' 上按下鼠标左键双键[2] print(arr) # 然后我按<魔法快捷键>(例如 Ctrl+p)# 并且 vscode 自动生成 [2] 行
您可以通过 print()
提供您的快速调试方法.
如果不选择变量,而只是将光标放在行尾,则可以通过插入一个片段的简单键绑定来实现不需要宏.按键绑定:
{"key": "alt+w","command": "editor.action.insertSnippet",参数":{//使用光标行尾,无选择//输出:打印(arr)"snippet": "\n${TM_CURRENT_LINE/(\\s*)(\\w*)\\b.*/print($2)/}"}},
如果你想要这个输出print(arr": arr)
,使用这个键绑定:
{"key": "alt+w","command": "editor.action.insertSnippet",参数":{//使用光标行尾,无选择//输出:打印(arr":arr)"snippet": "\n${TM_CURRENT_LINE/(\\s*)(\\w*)\\b.*/print(\"$2\": $2)/}"}},
对于这些更简单的版本,变量必须是行中的第一个单词.
旧答案:
不幸的是,这似乎很难用一个简单的片段来完成.将在光标所在的位置插入一个新的片段 - 在你的场景下,将在你选择的变量上 - 然后第一行的其余部分仍然在片段之后.
使用允许您执行多个命令的宏扩展相对容易,例如
如演示 gif 所示,所选文本可以位于行上的任何位置,如果该行上紧邻 print()
语句下方的代码将插入您期望的位置.
注意:这会将您选择的变量保存到剪贴板,以便将其覆盖.
如果你的变量总是在行首并被选中,你可以使用更简单的宏:
"multiCommand.commands": [{"command": "multiCommand.printVariable",序列": [{"command": "editor.action.insertSnippet",参数":{//选择的变量在行首"snippet": "${TM_CURRENT_LINE}\nprint(${TM_SELECTED_TEXT})"}},"cursorEndSelect",//选择结束并删除editor.action.clipboardCutAction"]}]
How can I create a custom shortcut for generating code that will print a selected variable in vscode?
[1] arr = [1,2,3,4] # I press double left mouse button on 'arr'
[2] print(arr) # Then I press <magic shortcut> (Ctrl+p for example)
# And vscode generate [2] row automatically
You can provide your method for fast debugging by print()
.
If, instead of selecting the variable, you just put the cursor at the end of the line, you can do this with a simple keybinding that inserts a snippet and no need for a macro. Keybinding:
{
"key": "alt+w",
"command": "editor.action.insertSnippet",
"args": {
// works with cursor end of line, no selection
// output: print(arr)
"snippet": "\n${TM_CURRENT_LINE/(\\s*)(\\w*)\\b.*/print($2)/}"
}
},
If you want this output print("arr": arr)
, use this keybinding:
{
"key": "alt+w",
"command": "editor.action.insertSnippet",
"args": {
// works with cursor end of line, no selection
// output: print("arr": arr)
"snippet": "\n${TM_CURRENT_LINE/(\\s*)(\\w*)\\b.*/print(\"$2\": $2)/}"
}
},
For these simpler versions, the variable must be the first word in the line.
Older answer:
Unfortunately, this seems difficult to do with a simple snippet. A new snippet would be inserted where the cursor is - and under your scenario that would be on your chosen variable - and then the rest of that first line is still there after the snippet.
It is relatively easy to do with a macro extension which allows you to perform multiple commands, like multi-command or another.
After installing the extension, in your settings:
"multiCommand.commands": [
{
"command": "multiCommand.printVariable",
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
{
"command": "type",
"args": {
"text": "print("
}
},
"editor.action.clipboardPasteAction",
{
"command": "type",
"args": {
"text": ")"
}
},
]
}
},
and then set up some keybinding in keybindings.json:
{
"key": "alt+q",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.printVariable" },
// use the following if you wish to limit the command to python files
"when": "resourceExtname == .py"
},
As the demo gif shows, the selected text can be anywhere on the line and if there is code on the line immediately below the print()
statement will be inserted where you expect.
Caution: This will save your selected variable to the clipboard so that will be overwritten.
If your variable is always at the beginning of the line and selected, you can use the simpler macro:
"multiCommand.commands": [
{
"command": "multiCommand.printVariable",
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
// selected variable is at beginning of line
"snippet": "${TM_CURRENT_LINE}\nprint(${TM_SELECTED_TEXT})"
}
},
"cursorEndSelect", // select to end and delete
"editor.action.clipboardCutAction"
]
}
]
这篇关于如何创建打印变量的快捷方式(vscode)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!