Add :DapNew command start new session

If no args: calls dap.continue({ new = true })
If args: calls dap.run for each configuration where name=arg

E.g.: `:DapNew celery django`

If config names contain spaces they need to be escaped
This commit is contained in:
Mathias Fussenegger 2024-05-31 20:29:38 +02:00 committed by Mathias Fußenegger
parent 09b54e03d4
commit c18767707d

View file

@ -1,4 +1,3 @@
local api = vim.api
if not api.nvim_create_user_command then
return
@ -27,6 +26,48 @@ cmd('DapTerminate', function() require('dap').terminate() end, { nargs = 0 })
cmd('DapLoadLaunchJSON', function() require('dap.ext.vscode').load_launchjs() end, { nargs = 0 })
cmd('DapRestartFrame', function() require('dap').restart_frame() end, { nargs = 0 })
local function dapnew(args)
local dap = require("dap")
local fargs = args.fargs
if not next(fargs) then
dap.continue({ new = true })
return
end
local bufnr = api.nvim_get_current_buf()
require("dap.async").run(function()
for _, get_configs in pairs(dap.providers.configs) do
local configs = get_configs(bufnr)
for _, config in ipairs(configs) do
if vim.tbl_contains(fargs, config.name) then
dap.run(config)
end
end
end
end)
end
cmd("DapNew", dapnew, {
nargs = "*",
desc = "Start one or more new debug sessions",
complete = function ()
local bufnr = api.nvim_get_current_buf()
local dap = require("dap")
local candidates = {}
local done = false
require("dap.async").run(function()
for _, get_configs in pairs(dap.providers.configs) do
local configs = get_configs(bufnr)
for _, config in ipairs(configs) do
local name = config.name:gsub(" ", "\\ ")
table.insert(candidates, name)
end
end
done = true
end)
vim.wait(2000, function() return done == true end)
return candidates
end
})
if api.nvim_create_autocmd then
local launchjson_group = api.nvim_create_augroup('dap-launch.json', { clear = true })