问题
I have tried to key-bind a macro to send python text to the Debug Console and return focus to the editor in Visual Studio Code. This is what I have tried:
- Installed the vscode-python extension
- Installed the macros extension
settings.json
:
{
"macros": {
"selectionToReplAndReturnToEditor": [
"editor.debug.action.selectionToRepl",
"workbench.action.focusActiveEditorGroup"
]
}
}
keybindings.json
:
[
{
"key": "alt+f9",
"command": "workbench.action.focusActiveEditorGroup",
},
{
"key": "alt+f10",
"command": "workbench.debug.action.focusRepl",
},
{
"key": "ctrl+enter",
"command": "macros.selectionToReplAndReturnToEditor",
"when": "editorTextFocus && editorHasSelection && editorLangId == 'python' && inDebugMode"
}
]
Now, Ctrl+Enter does execute text in the Debug Console, but does not return focus to the editor. Ctrl+Enter followed by Alt+F9 does that, but of course, I would like to bind a single key. Am I doing something wrong? Do I need some wait time in the macro? How can I achieve that?
回答1:
This works, using a different extension:
"multiCommand.commands": [ // requires vscode:extension/ryuta46.multi-command
{ // ctrl+enter, editorTextFocus && editorHasSelection && editorLangId == 'python' && inDebugMode
"command": "multiCommand.selectionToReplAndReturnToEditor",
"sequence": [
"editor.debug.action.selectionToRepl",
"workbench.action.focusActiveEditorGroup",
]
},
}
回答2:
Thanks! it doesn't work completely for me. I am guessing there is a difference between the selected code in the focused text editor, and the actual cursor. I should ask that.
回答3:
@bers answer is a God-send. here is the full solution.
There are a few things that we need to do here:
- to send stuff to the debugger's integrated REPL, you need the action called
editor.debug.action.selectionToRepl
. - then you need to figure out how to return focus to the active editor. This is where the multi-command extension comes in.
- Finally, you need to condition your keybinding, so that this only gets activated when debugMode is on.
in keybindings.json
{
"key": "cmd+enter",
"command": "workbench.action.terminal.runSelectedText",
"when": "editorHasSelection && editorTextFocus && !inDebugMode"
},
{
"key": "cmd+enter",
// This needs to be the command you define above.
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.selectionToReplAndReturnToEditor" },
"when": "editorTextFocus && editorHasSelection && editorLangId == 'python' && inDebugMode"
},
In settings.json
"multiCommand.commands": [ // requires vscode:extension/ryuta46.multi-command
{ // ctrl+enter, editorTextFocus && editorHasSelection && editorLangId == 'python' && inDebugMode
"command": "multiCommand.selectionToReplAndReturnToEditor",
"sequence": [
"editor.debug.action.selectionToRepl",
"workbench.action.focusActiveEditorGroup",
]
},
]
来源:https://stackoverflow.com/questions/49457051/how-to-return-focus-to-editor-in-vs-code-macro-sending-python-text-to-debug-cons