Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for command input type in launch.json #1082

Open
mltoo opened this issue Nov 4, 2023 · 0 comments
Open

Support for command input type in launch.json #1082

mltoo opened this issue Nov 4, 2023 · 0 comments

Comments

@mltoo
Copy link

mltoo commented Nov 4, 2023

Problem Statement

VSCode provides support for a command type for inputs in launch.json files, which is similar to pickString, except a VSCode command is specified to dynamically populate the set of options in the dropdown[1]. I would like to have similar functionality with nvim-dap.

[1] https://code.visualstudio.com/docs/editor/variables-reference#_input-variables

Possible Solutions

Obviously in neovim we don't have access to VSCode commands, so in order to be able to share launch.json files with VSCode users, I think the best way to do this would be to allow users to specify implementations (e.g. in lua) of VSCode commands for nvim-dap to use.

I have written a PR which allows the user to provide an (optional) additional argument command_callbacks to load_launchjs. This is a table indexed by names of VSCode commands, each providing a callback function, with the option to provide a 'default' callback in command_callbacks[1], to allow the user to handle parsing/dispatching commands themselves/hand this off to some other plugin. The callback functions are passed the name of the the command called, as well as the args section of the input from the launch.json. Callbacks should return a table with strings to appear in the dropdown.

Here is a quick-and-dirty example of how you could use this for a 'pick a unit test to run' launch configuration, in a c++/googletest project. This uses the shellCommand.execute VSCode command from [2], which lets you run a shell command by calling a VSCode command:

Example launch.json: (The command here simply gets a list of tests from gtest (i.e. TestGroup.TestName), separated by newlines)

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Run specific test",
            "program": "build/tests",
            "cwd": "${workspaceFolder}",
            "stopOnEntry": false,
            "args": [ "--gtest_filter=${input:pickTest}"]
        }
    ],
    "inputs": [
        {
            "id": "pickTest",
	    "type": "command",
	    "command": "shellCommand.execute",
	    "args": {
		    "command": "build/tests --gtest_list_tests | grep '.*\\.$' | while read group; do build/tests --gtest_list_tests --gtest_filter=\"$group*\" | grep '  .*' | cut -c3- | awk -v g=$group '{print g$1}' ; done"
	    }
        }
    ]
}

Example command_callbacks providing a (thrown together and feature-incomplete) implementation of shellCommand.execute:

local command_callbacks = {}
command_callbacks['shellCommand.execute'] =  function(command, args)
    local lines = {}
    for line in string.gmatch(vim.fn.system(args["command"]), "[^\r\n]+") do
        table.insert(lines, line)
    end
    return lines
end

require('dap.ext.vscode').load_launchjs(nil, { cppdbg = { 'cpp' } }, command_callbacks)

Running this gives you a dropdown that looks a bit like this, with each of the tests to select from:
image

[2] https://marketplace.visualstudio.com/items?itemName=augustocdias.tasks-shell-input

Considered Alternatives

This was something that I put together fairly quickly mostly for my own use, and I'm open to reworking it if you don't think this is the right approach (It definitely needs adding to the documentation if you agree to merge it in any case). I do think that this is a feature worth having in nvim-dap upstream in some form though, so I'd be really interested to hear what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant