简体   繁体   English

如何使用 Visual Studio Code 中的相同键开始调试 R 或 Python 代码?

[英]How can I start debugging R or Python code using the same key in Visual Studio Code?

Trying to avoid the XY problem, my problem at the very high level: I work in a VS Code workspace with R and Python code files, and I often need to debug one or the other.试图避免 XY 问题,我的问题处于非常高的级别:我在 VS Code 工作区中使用 R 和 Python 代码文件工作,我经常需要调试其中一个。 Currently I have different debug launch configurations saved, and I need to switch between them manually - that is a pain.目前我保存了不同的调试启动配置,我需要手动在它们之间切换——这很痛苦。 I would like to use F5 to我想用 F5 来

  • launch the Python debugger when the editor is active in a .py file当编辑器在.py文件中处于活动状态时,启动 Python 调试器
  • launch the R debugger when the editor is active in a .R file当编辑器在.R文件中处于活动状态时,启动 R 调试器

I see many technical ways of doing that, but all have their roadblocks (some of which may just poor documentation):我看到许多技术方法可以做到这一点,但都有他们的障碍(其中一些可能只是糟糕的文档):

  1. Create my own extension with a dynamic debug configuration that determines the type of the active editor and starts the correct debug configuration.使用动态调试配置创建我自己的扩展,该配置确定活动编辑器的类型并启动正确的调试配置。 (A lot of effort required.) (需要很多努力。)
  2. Use a "compound" launch configuration, starting both R and Python launch configurations, and stopping all but one.使用"compound"启动配置,启动 R 和 Python 启动配置,并停止除一个之外的所有启动配置。 This can be done using a "prelaunchTask" for each, but non-zero return codes create error message that I don't like.这可以使用每个"prelaunchTask"来完成,但非零返回码会创建我不喜欢的错误消息。
  3. Use editor-dependent key mappings ( "when": "editorLangId == 'python'" ), but which command for debugging to start and how to pass the launch configuration?使用与编辑器相关的键映射( "when": "editorLangId == 'python'" ),但是要启动哪个调试命令以及如何传递启动配置? There is vscode.startDebug which takes arguments ( https://github.com/microsoft/vscode/issues/4615 ), but I cannot keybind to that.vscode.startDebug需要 arguments ( https://github.com/microsoft/vscode/issues/4615 ),但我不能绑定到那个。 And then there is workbench.action.debug.start which seems to ignore arguments.然后是workbench.action.debug.start似乎忽略了 arguments。 And then there is vscode.commands.executeCommand to which I cannot keybind, either.然后是vscode.commands.executeCommand ,我也无法对其进行键绑定。
  4. Use a multi-command extension to bind a key to something like "set debug configuration, then press F5".使用多命令扩展将键绑定到“设置调试配置,然后按 F5”之类的内容。 But how to do that?但是怎么做呢? Etc. etc.等等等等。

After much fiddling, here is one solution following idea 2:经过一番摆弄,这是遵循想法 2 的一种解决方案:

settings.json (launch configs could be put into launch.json ): settings.json (启动配置可以放入launch.json ):

{
    "debug.onTaskErrors": "abort",
    "launch": {
        "compounds": [
            {
                "name": "Python/R: Current File",
                "configurations": [
                    "Python: Current File",
                    "R: Current File",
                ],
            },
        ],
        "configurations": [
            {
                "name": "Python: Current File",
                "type": "python",
                "preLaunchTask": "check_ext py",
                "request": "launch",
                // ...
            },
            {
                "name": "R: Current File",
                "type": "R-Debugger",
                "preLaunchTask": "check_ext R",
                "request": "launch",
                // ...
            }
        ],
    },
}

tasks.json : tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "check_ext py",
            "type": "shell",
            "command": "echo ${fileExtname} | grep -i ^.py$",
        },
        {
            "label": "check_ext R",
            "type": "shell",
            "command": "echo ${fileExtname} | grep -i '^.R$'",
        },
    ],
}

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

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