简体   繁体   English

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

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

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.我试图键绑定一个宏以将 python 文本发送到调试控制台并将焦点返回到 Visual Studio Code 中的编辑器。 This is what I have tried:这是我试过的:

settings.json : settings.json

{
    "macros": {
        "selectionToReplAndReturnToEditor": [
            "editor.debug.action.selectionToRepl",
            "workbench.action.focusActiveEditorGroup"
        ]
    }
}

keybindings.json : 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会在调试控制台中执行文本,但不会将焦点返回到编辑器。 Ctrl + Enter followed by Alt + F9 does that, but of course, I would like to bind a single key. Ctrl + Enter后跟Alt + F9可以做到这一点,但当然,我想绑定一个键。 Am I doing something wrong?难道我做错了什么? Do I need some wait time in the macro?我需要在宏中等待一些时间吗? How can I achieve that?我怎样才能做到这一点?

@bers answer is a God-send. @bers 的回答是天赐之物。 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 .要将内容发送到调试器的集成 REPL,您需要名为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.jsonkeybindings.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.jsonsettings.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",
      ]
    },
  ]

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

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.我试图键绑定一个宏以将 python 文本发送到调试控制台并将焦点返回到 Visual Studio Code 中的编辑器。 This is what I have tried:这是我尝试过的:

settings.json : settings.json

{
    "macros": {
        "selectionToReplAndReturnToEditor": [
            "editor.debug.action.selectionToRepl",
            "workbench.action.focusActiveEditorGroup"
        ]
    }
}

keybindings.json : 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确实会在调试控制台中执行文本,但不会将焦点返回到编辑器。 Ctrl + Enter followed by Alt + F9 does that, but of course, I would like to bind a single key. Ctrl + Enter后跟Alt + F9 可以做到这一点,但当然,我想绑定一个键。 Am I doing something wrong?难道我做错了什么? Do I need some wait time in the macro?我需要在宏中等待一些时间吗? How can I achieve that?我怎样才能做到这一点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM