How to return focus to editor in VS Code macro sending Python text to Debug Console?

孤街醉人 提交于 2020-01-25 07:15:12

问题


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:

  1. to send stuff to the debugger's integrated REPL, you need the action called editor.debug.action.selectionToRepl.
  2. then you need to figure out how to return focus to the active editor. This is where the multi-command extension comes in.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!