Skip to content

Commit

Permalink
feat(breakpoints): jump to a breakpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
igorlfs committed Dec 5, 2024
1 parent 85e7bce commit 90da63c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
17 changes: 10 additions & 7 deletions lua/dap-view/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local settings = require("dap-view.settings")
local events = require("dap-view.events")
local globals = require("dap-view.globals")

local api = vim.api

---@class Actions
local M = {}

Expand All @@ -19,11 +21,11 @@ end

M.close = function()
if state.winnr then
vim.api.nvim_win_close(state.winnr, true)
api.nvim_win_close(state.winnr, true)
state.winnr = nil
end
if state.bufnr then
vim.api.nvim_buf_delete(state.bufnr, { force = true })
api.nvim_buf_delete(state.bufnr, { force = true })
state.bufnr = nil
end
end
Expand All @@ -35,7 +37,7 @@ end
M.open = function(config)
M.close()

local bufnr = vim.api.nvim_create_buf(false, false)
local bufnr = api.nvim_create_buf(false, false)

assert(bufnr ~= 0, "Failed to create dap-view buffer")

Expand All @@ -44,11 +46,11 @@ M.open = function(config)
-- TODO move this to close?
local prev_buf = util.get_buf(globals.MAIN_BUF_NAME)
if prev_buf then
vim.api.nvim_buf_delete(prev_buf, { force = true })
api.nvim_buf_delete(prev_buf, { force = true })
end
vim.api.nvim_buf_set_name(bufnr, globals.MAIN_BUF_NAME)
api.nvim_buf_set_name(bufnr, globals.MAIN_BUF_NAME)

local winnr = vim.api.nvim_open_win(bufnr, false, {
local winnr = api.nvim_open_win(bufnr, false, {
split = "below",
win = 0,
height = 15,
Expand All @@ -59,6 +61,7 @@ M.open = function(config)
state.winnr = winnr

settings.set_options()
settings.set_keymaps()

-- TODO perhaps there's a better spot to handle
-- but currently only works if it's here
Expand All @@ -68,7 +71,7 @@ M.open = function(config)
winbar.set_winbar(winbar_config.default_section, winbar_config.sections)

-- Properly handle exiting the window
vim.api.nvim_create_autocmd({ "BufDelete", "WinClosed" }, {
api.nvim_create_autocmd({ "BufDelete", "WinClosed" }, {
buffer = state.bufnr,
once = true,
callback = M.close,
Expand Down
71 changes: 71 additions & 0 deletions lua/dap-view/breakpoints/actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local M = {}

local state = require("dap-view.state")

local api = vim.api

M._jump_to_breakpoint = function()
local line = vim.fn.getline(".")

if not line or line == "" then
vim.notify("No valid line under the cursor", vim.log.levels.ERROR)
return
end

local file, line_num = line:match("^(.-)|(%d+)|")
if not file or not line_num then
vim.notify("Invalid format: " .. line, vim.log.levels.ERROR)
return
end

line_num = tonumber(line_num)
if not line_num then
vim.notify("Invalid line number: " .. line_num, vim.log.levels.ERROR)
return
end

local abs_path = vim.fn.fnamemodify(file, ":p")
if not vim.uv.fs_stat(abs_path) then
vim.notify("File not found: " .. abs_path, vim.log.levels.ERROR)
return
end

local windows = api.nvim_tabpage_list_wins(0)

-- TODO this simply finds the first suitable window
-- A better approach could try to match the paths to avoid jumping around
-- Or perhaps there's a way to respect the 'switchbuf' option
local prev_or_new_window = vim.iter(windows)
:filter(function(w)
local buf = api.nvim_win_get_buf(w)
return api.nvim_get_option_value("buftype", { buf = buf }) == ""
end)
:find(function(w)
return w
end)

if not prev_or_new_window then
local max_height = api.nvim_win_get_height(state.winnr)

prev_or_new_window = api.nvim_open_win(0, true, {
split = "above",
win = 0,
height = max_height - 15,
})
end

api.nvim_win_call(prev_or_new_window, function()
local bufnr = vim.fn.bufnr(abs_path)
if bufnr == -1 then
vim.cmd("buffer " .. abs_path)
else
vim.cmd("buffer " .. bufnr)
end
end)

api.nvim_win_set_cursor(prev_or_new_window, { line_num, 0 })

api.nvim_set_current_win(prev_or_new_window)
end

return M
4 changes: 3 additions & 1 deletion lua/dap-view/breakpoints/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ local populate_buf_with_breakpoints = function()
api.nvim_buf_set_lines(state.bufnr, -2, -1, false, {})
end

M.show = function() populate_buf_with_breakpoints() end
M.show = function()
populate_buf_with_breakpoints()
end

return M
31 changes: 20 additions & 11 deletions lua/dap-view/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ local state = require("dap-view.state")
local M = {}

M.set_options = function()
vim.wo[state.winnr][0].scrolloff = 0
vim.wo[state.winnr][0].wrap = false
vim.wo[state.winnr][0].number = false
vim.wo[state.winnr][0].relativenumber = false
vim.wo[state.winnr][0].winfixheight = true
vim.wo[state.winnr][0].cursorlineopt = "line"
vim.wo[state.winnr][0].cursorline = true
vim.wo[state.winnr][0].statuscolumn = ""
vim.wo[state.winnr][0].foldcolumn = "0"
local win = vim.wo[state.winnr][0]
win.scrolloff = 0
win.wrap = false
win.number = false
win.relativenumber = false
win.winfixheight = true
win.cursorlineopt = "line"
win.cursorline = true
win.statuscolumn = ""
win.foldcolumn = "0"
win.winfixbuf = true

vim.bo[state.bufnr].buftype = "nofile"
vim.bo[state.bufnr].swapfile = false
local buf = vim.bo[state.bufnr]
buf.buftype = "nofile"
buf.swapfile = false
end

M.set_keymaps = function()
vim.keymap.set("n", "<CR>", function()
require("dap-view.breakpoints.actions")._jump_to_breakpoint()
end, { buffer = state.bufnr })
end

return M

0 comments on commit 90da63c

Please sign in to comment.