nvim-dap
nvim-dap copied to clipboard
Jump to next/previous breakpoint
Problem Statement
Often I want to move or disable a breakpoint. It would be great to have a function I could map as a motion to to jump straight to that breakpoint.
Possible Solutions
Map to something (like ]b) so that I can immediately jump to where my next breakpoint is. From there I can disable it and move it one line down, for example, or modify the line that I am inspecting.
Considered Alternatives
Could parse it out of breakpoints list myself, but I think this would be generally useful and I think it fits with a vim philosophy
:DapBreakpoints or :lua require('dap').list_breakpoints() adds the breakpoints to the quickfix list, so you can use :cnext and :cprev to jump between breakpoints.
I hadn't considered using cnext or cprev, thanks. However, I still think there should be a more specific solution. Having to clear the previous qf list and re-populate it just to jump to a breakpoint feels like very poor UX for me, where the alternative could be a single function call that leaves my qf list alone. (Is there a reason you chose qf rather than location list? The latter seems like it would have been a better suited list to me)
Having to clear the previous qf list and re-populate it just to jump to a breakpoint feels like very poor UX for me
Have the same feeling.
[...] It would be great to have a function I could map as a motion to jump straight to [...].
I think these are some examples of what OP called "[...] map as a motion to to jump" from the keymaps I use:
]gto jump to the next git hunk. (underlying command provided by lewis6991/gitsigns.nvim)]ato (underlying command provided by stevearc/aerial.nvim)]dto jump to the next (file) diagnostics. (by built-invim.diagnostic)
It will be very nice if you could provide something similar to these.
@mfussenegger Am I correct to say that the current solution for https://github.com/mfussenegger/nvim-dap/discussions/941 is:
- Open the quickfix list by
:lua require('dap').list_breakpoints(). - Jump to the breakpoint of interest.
- Execute
run_to_cursor.
Is it possible to achieve the same result with one command, e.g. maybe run_to_breakpoint?
I made a small function that directly uses the breakpoint data to go to the next breakpoint, without the quickfix list as intermediary. If you are at a breakpoint, it goes to the next breakpoint, otherwise, it goes to the first breakpoint. You can bind it to something like ]p and [p.
---@param dir "next"|"prev"
local function gotoBreakpoint(dir)
local breakpoints = require("dap.breakpoints").get()
if #breakpoints == 0 then
vim.notify("No breakpoints set", vim.log.levels.WARN)
return
end
local points = {}
for bufnr, buffer in pairs(breakpoints) do
for _, point in ipairs(buffer) do
table.insert(points, { bufnr = bufnr, line = point.line })
end
end
local current = {
bufnr = vim.api.nvim_get_current_buf(),
line = vim.api.nvim_win_get_cursor(0)[1],
}
local nextPoint
for i = 1, #points do
local isAtBreakpointI = points[i].bufnr == current.bufnr and points[i].line == current.line
if isAtBreakpointI then
local nextIdx = dir == "next" and i + 1 or i - 1
if nextIdx > #points then nextIdx = 1 end
if nextIdx == 0 then nextIdx = #points end
nextPoint = points[nextIdx]
break
end
end
if not nextPoint then nextPoint = points[1] end
vim.cmd(("buffer +%s %s"):format(nextPoint.line, nextPoint.bufnr))
end
If @mfussenegger is open to it, I can make a PR for this.
If @mfussenegger is open to it, I can make a PR for this.
I won't be adding anything in that direction until the other breakpoint variants are implemented (data, function, etc.) because it will likely influence the API, and I don't want to deal with BWC hassle in that regard. That's all kinda on my roadmap for a nvim-dap 1.0
the dap.breakpoints module is btw. not stable, so it can break with any commit without a deprecation warning/cycle. Only the documented functions are safe to use.