【问题标题】:How can I start debugging R or Python code using the same key in Visual Studio Code?如何在 Visual Studio Code 中使用相同的键开始调试 R 或 Python 代码?
【发布时间】:2021-05-20 11:10:20
【问题描述】:

试图避免 XY 问题,我的问题处于非常高的水平:我在一个带有 R 和 Python 代码文件的 VS Code 工作区中工作,我经常需要调试其中一个。目前我保存了不同的调试启动配置,我需要手动在它们之间切换——这很痛苦。我想用F5来

  • 当编辑器在 .py 文件中处于活动状态时启动 Python 调试器
  • 当编辑器在 .R 文件中处于活动状态时启动 R 调试器

我看到很多技术方法可以做到这一点,但都有他们的障碍(其中一些可能只是糟糕的文档):

  1. 使用动态调试配置创建我自己的扩展,该配置确定活动编辑器的类型并启动正确的调试配置。 (需要付出很多努力。)
  2. 使用"compound" 启动配置,同时启动 R 和 Python 启动配置,并停止除一个之外的所有配置。这可以使用"prelaunchTask" 来完成,但非零返回码会创建我不喜欢的错误消息。
  3. 使用依赖于编辑器的键映射("when": "editorLangId == 'python'"),但调试启动哪个命令以及如何通过启动配置?有vscode.startDebug 接受参数(https://github.com/microsoft/vscode/issues/4615),但我不能绑定到那个。然后是workbench.action.debug.start,它似乎忽略了论点。然后是vscode.commands.executeCommand,我也无法对其进行键绑定。
  4. 使用多命令扩展将键绑定到“设置调试配置,然后按 F5”之类的内容。但是怎么做呢? 等等等等。

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    经过一番折腾,下面是思路 2 的一种解决方案:

    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:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 2018-01-20
      • 2020-07-30
      • 1970-01-01
      • 2021-01-09
      相关资源
      最近更新 更多