[FEATURE] persist dap breakpoints
Would it be possible to persist breakpoints similar to https://github.com/Weissle/persistent-breakpoints.nvim ?
Storing breakpoints isn't a feature of Neovim's mksession so it's out of scope for auto-session at the moment. That said, auto-session provides hooks, once a session is loaded, you could call persistent-breakpoint's API and load your breakpoints automatically.
related: https://github.com/mfussenegger/nvim-dap/issues/198
@rmagatti I'd like to make it easier to store custom data as part of a session. Currently, we have save_extra_cmds but it's cumbersome if you're not just calling functions. You could return code that calls a function with your custom data but it's finicky because it's in vimscript.
I'm thinking we could add something like:
---@alias save_extra_data_fn fun(session_name:string): extra_data:string
---@alias restore_extra_data_fn fun(session_name:string, extra_data:string)
---@field save_extra_data? save_extra_data_fn Extra data that should be saved with the session. Will be passed to restore_extra_data on restore
---@field restore_extra_data? restore_extra_data_fn Called when there's custom data saved for a session
Internally, I think we can just save an extra_command that when executed, will call restore_extra_data
That would make it really easy to implement this kind of thing. We could then add a recipe to the readme/wiki
Here's a slightly improved version of the linked code (use buffer names instead of ids) implemented using the above api:
save_extra_data = function(_)
local ok, breakpoints = pcall(require, 'dap.breakpoints')
if not ok or not breakpoints then return end
local bps = {}
local breakpoints_by_buf = breakpoints.get()
for buf, buf_bps in pairs(breakpoints_by_buf) do
bps[vim.api.nvim_buf_get_name(buf)] = buf_bps
end
if vim.tbl_isempty(bps) then return end
local extra_data = {
breakpoints = bps,
}
return vim.fn.json_encode(extra_data)
end,
restore_extra_data = function(_, extra_data)
local json = vim.fn.json_decode(extra_data)
if json.breakpoints then
local ok, breakpoints = pcall(require, 'dap.breakpoints')
if not ok or not breakpoints then return end
vim.notify('restoring breakpoints')
for buf_name, buf_bps in pairs(json.breakpoints) do
for _, bp in pairs(buf_bps) do
local line = bp.line
local opts = {
condition = bp.condition,
log_message = bp.logMessage,
hit_condition = bp.hitCondition,
}
breakpoints.set(opts, vim.fn.bufnr(buf_name), line)
end
end
end
end,
That mechanism could also be extended to include other things beyond just breakpoints.
If you want to try it out, you can update your config to test with my fork:
return {
-- 'rmagatti/auto-session',
'cameronr/auto-session',
branch = 'feature/save-extra-data',
These changes are now in main and there's a recipe in the readme so I'm to close this as completed.