我试图在选择一行时为键盘快捷键设置不同的命令,在选择多行时设置另一种命令。我试图在默认键绑定中查找示例,但没有成功。有什么办法可以做到这一点?我应该在键绑定文件中使用什么when
参数,也许还有另一种方法?
答案 0 :(得分:1)
在编辑器中有2个when
上下文有关选择
editorHasSelection
editorHasMultipleSelections
如果您正在寻找一个选择是单个选择但多行的情况,那么您将不知道所选择的内容。
修改
我写了一个扩展程序,可以解决上下文中的键绑定问题。
使用Extra Context并定义extraContext:editorSelectionHasMultipleLines
与when
子句一起使用。
一个例子:
在settings.json
中:
"multiCommand.commands": [
{
"command": "multiCommand.terminalSingleLine",
"sequence": [
{ "command": "workbench.action.terminal.sendSequence",
"args": { "text": "echo Single Line\u000D" }
}
]
},
{
"command": "multiCommand.terminalMultipleLine",
"sequence": [
{ "command": "workbench.action.terminal.sendSequence",
"args": { "text": "echo Multiple Lines\u000D" }
}
]
}
]
在keybindings.json
中:
{
"key": "ctrl+k f5", // or any other key combo
"command": "multiCommand.terminalSingleLine",
"when": "editorTextFocus && !extraContext:editorSelectionHasMultipleLines"
},
{
"key": "ctrl+k f5",
"command": "multiCommand.terminalMultipleLine",
"when": "editorTextFocus && extraContext:editorSelectionHasMultipleLines"
}