bers
bers

Reputation: 5823

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. 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

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. This can be done using a "prelaunchTask" for each, but non-zero return codes create error message that I don't like.
  3. Use editor-dependent key mappings ("when": "editorLangId == 'python'"), but which command for debugging to start and how to pass the launch configuration? There is vscode.startDebug which takes arguments (https://github.com/microsoft/vscode/issues/4615), but I cannot keybind to that. And then there is workbench.action.debug.start which seems to ignore arguments. And then there is vscode.commands.executeCommand to which I cannot keybind, either.
  4. Use a multi-command extension to bind a key to something like "set debug configuration, then press F5". But how to do that? Etc. etc.

Upvotes: 0

Views: 235

Answers (1)

bers
bers

Reputation: 5823

After much fiddling, here is one solution following idea 2:

settings.json (launch configs could be put into 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$'",
        },
    ],
}

Upvotes: 0

Related Questions