Allow specifying arguments to launch DAP.

main
Joshua Potter 2023-11-23 05:09:45 -07:00
parent e75acc62d3
commit 54510be14f
1 changed files with 72 additions and 5 deletions

View File

@ -1,13 +1,76 @@
local M = {} local M = {}
local dap = require('dap')
local dap_ui = require('dap.ui')
local dap_ui_widgets = require('dap.ui.widgets')
local function query_launch()
local command = vim.fn.input('Executable> ', vim.fn.getcwd() .. '/', 'file')
vim.api.nvim_echo({ { '', 'None' } }, false, {})
local parts = vim.split(command, '%s+', { trimempty = true })
if not parts[1] then
vim.api.nvim_err_writeln('Invalid command specification.')
return
end
return parts[1], {unpack(parts, 2, #parts)}
end
-- Adaptation of https://github.com/mfussenegger/nvim-dap/blob/e154fdb6d70b3765d71f296e718b29d8b7026a63/lua/dap.lua#L413.
local function select_config_and_run()
local filetype = vim.api.nvim_buf_get_option(0, 'filetype')
local configs = dap.configurations[filetype] or {}
assert(
vim.tbl_islist(configs),
string.format(
'`dap.configurations.%s` must be a list of configurations, got %s',
filetype,
vim.inspect(configs)
)
)
if not configs[1] then
local msg = 'No configuration found for `%s`. You need to add configs ' ..
'to `dap.configurations.%s` (See `:h dap-configuration`)'
vim.api.nvim_err_writeln(string.format(msg, filetype, filetype))
return
end
local opts = {}
opts.filetype = opts.filetype or filetype
dap_ui.pick_if_many(
configs,
'Configuration: ',
function(c) -- Label function
return c.name
end,
function(c) -- Callback
if not c then
vim.api.nvim_err_writeln('No configuration selected.')
return
end
local copy = vim.deepcopy(c)
if copy.request == 'launch' then
local program, args = query_launch()
copy.program = program
copy.args = args
end
dap.run(copy, opts)
end
)
end
local function sidebar_new(widget) local function sidebar_new(widget)
return require('dap.ui.widgets').sidebar(widget, { width = 40 }, '') return dap_ui_widgets.sidebar(widget, { width = 40 }, '')
end end
local function sidebar_is_open(sidebar) local function sidebar_is_open(sidebar)
return sidebar.win and vim.api.nvim_win_is_valid(sidebar.win) return sidebar.win and vim.api.nvim_win_is_valid(sidebar.win)
end end
-- Setup buffer-local DAP mappings. This function is expected to be called in
-- a filetype plugin, e.g. `nvim/after/ftplugin/c.lua`.
function M.buffer_map() function M.buffer_map()
local function set_nnoremap(key, func) local function set_nnoremap(key, func)
vim.keymap.set( vim.keymap.set(
@ -18,9 +81,6 @@ function M.buffer_map()
) )
end end
local dap = require('dap')
local dap_ui_widgets = require('dap.ui.widgets')
local sidebars = { local sidebars = {
expression = sidebar_new(dap_ui_widgets.expression), expression = sidebar_new(dap_ui_widgets.expression),
frames = sidebar_new(dap_ui_widgets.frames), frames = sidebar_new(dap_ui_widgets.frames),
@ -50,8 +110,15 @@ function M.buffer_map()
end end
end end
set_nnoremap('<localleader>', dap.continue) set_nnoremap('<localleader>', select_config_and_run)
set_nnoremap('b', dap.toggle_breakpoint) set_nnoremap('b', dap.toggle_breakpoint)
set_nnoremap('c', function()
if dap.status() == '' then
vim.api.nvim_err_writeln('No active session.')
return
end
dap.continue()
end)
set_nnoremap('d', dap.down) set_nnoremap('d', dap.down)
set_nnoremap('i', dap.step_into) set_nnoremap('i', dap.step_into)
set_nnoremap('n', dap.step_over) set_nnoremap('n', dap.step_over)