如何将焦点返回到VS Code宏中的编辑器将Python文本发送到Debug Console?

时间:2018-03-23 19:25:11

标签: python macros visual-studio-code

我尝试对宏进行键绑定以将Python文本发送到调试控制台,并将焦点返回到Visual Studio Code中的编辑器。这就是我的尝试:

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"
    }
]

现在, Ctrl + Enter 会在调试控制台中执行文本,但不会将焦点返回到编辑器。 Ctrl + 输入后跟 Alt + F9 这样做,但当然,我想绑定一个键。难道我做错了什么?我需要在宏中等待一段时间吗?我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

这可以使用其他扩展名进行工作:

"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",
        ]
    },
}

答案 1 :(得分:0)

谢谢!它对我来说并不完全有效。 我猜在焦点文本编辑器中选择的代码和实际光标之间存在差异。  我应该问这个。

答案 2 :(得分:0)

@bers的答案是天赐之物。这是完整的解决方案。

在这里我们需要做一些事情:

  1. 要将内容发送到调试器的集成REPL,您需要执行名为editor.debug.action.selectionToRepl的操作。
  2. 然后,您需要弄清楚如何将焦点返回到活动编辑器。这是多命令扩展的来源。
  3. 最后,您需要对您的键绑定进行条件设置,以便仅在debugMode打开时才激活它。

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": "multiCommand.selectionToReplAndReturnToEditor",
    "when": "editorTextFocus && editorHasSelection && editorLangId == 'python' && inDebugMode"
  }

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",
      ]
    },
  ]