如何在 VS Code 宏中将焦点返回到编辑器,将 Python 文本发送到调试控制台?

ber*_*ers 2 python macros visual-studio-code

我尝试按键绑定宏以将 python 文本发送到调试控制台并将焦点返回到 Visual Studio Code 中的编辑器。这是我尝试过的:

settings.json

{
    "macros": {
        "selectionToReplAndReturnToEditor": [
            "editor.debug.action.selectionToRepl",
            "workbench.action.focusActiveEditorGroup"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

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"
    }
]
Run Code Online (Sandbox Code Playgroud)

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

epi*_*ang 5

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

Run Code Online (Sandbox Code Playgroud)

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",
      ]
    },
  ]
Run Code Online (Sandbox Code Playgroud)