Skip to content

Commit

Permalink
rearrange event handling and start introducing exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
igorlfs committed Dec 8, 2024
1 parent 800a156 commit 918344c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lua/dap-view.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- Connect hooks to listen to DAP events
require("dap-view.events")

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

local M = {}
Expand Down
7 changes: 1 addition & 6 deletions lua/dap-view/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local setup = require("dap-view.setup")
local util = require("dap-view.util")
local state = require("dap-view.state")
local settings = require("dap-view.settings")
local events = require("dap-view.events")
local globals = require("dap-view.globals")

local api = vim.api
Expand Down Expand Up @@ -62,11 +61,7 @@ M.open = function()
settings.set_options()
settings.set_keymaps()

-- TODO perhaps there's a better spot to handle
-- but currently only works if it's here
events.listen_breakpoints()

state.current_section = setup.config.winbar.default_section
state.current_section = state.current_section or setup.config.winbar.default_section
winbar.set_winbar(state.current_section)

-- Properly handle exiting the window
Expand Down
17 changes: 8 additions & 9 deletions lua/dap-view/breakpoints/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local winbar = require("dap-view.winbar")
local util = require("dap-view.util")
local state = require("dap-view.state")
local globals = require("dap-view.globals")
local vendor = require("dap-view.breakpoints.vendor")
local extmarks = require("dap-view.extmarks")
local treesitter = require("dap-view.treesitter")
Expand All @@ -20,15 +20,15 @@ local highlight_file_name_and_line_number = function(row, len_path, len_lnum)

api.nvim_buf_set_extmark(
state.bufnr,
M.namespace,
globals.NAMESPACE,
row,
0,
{ end_col = len_path, hl_group = "qfFileName" }
)

api.nvim_buf_set_extmark(
state.bufnr,
M.namespace,
globals.NAMESPACE,
row,
lnum_start,
{ end_col = lnum_start + len_lnum, hl_group = "qfLineNr" }
Expand Down Expand Up @@ -76,16 +76,15 @@ local populate_buf_with_breakpoints = function()
line_count = line_count + 1
end
end
end

-- TODO: this should not be happening
-- Remove the last line, as it's empty
api.nvim_buf_set_lines(state.bufnr, -2, -1, false, {})
-- Remove the last line, as it's empty (for some reason)
api.nvim_buf_set_lines(state.bufnr, -2, -1, false, {})
end
end

M.show = function()
state.current_section = "breakpoints"
winbar.update_winbar(state.current_section)
winbar.update_winbar("breakpoints")

populate_buf_with_breakpoints()
end

Expand Down
44 changes: 37 additions & 7 deletions lua/dap-view/events.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
local M = {}
local dap = require("dap")
local state = require("dap-view.state")
local breakpoints = require("dap-view.breakpoints")
local actions = require("dap-view.actions")

M.listen_breakpoints = function()
-- TODO a better approach could be to hijack the "toggle breakpoint" command
-- and use some autocmds (eg, BufLeave/BufEnter for our buffer)
require("dap").listeners.after.setBreakpoints["dap-view"] = function()
require("dap-view.breakpoints").show()
dap.listeners.after.setBreakpoints["dap-view"] = function()
breakpoints.show()
end

dap.listeners.after.launch["dap-view"] = function()
if state.exceptions_options == nil then
return
end

local filters = vim.iter(state.exceptions_options)
:filter(function(x)
return x.enabled
end)
:map(function(x)
return x.exception_filter.filter
end)
:totable()

dap.set_exception_breakpoints(filters)
end

return M
dap.listeners.after.initialize["dap-view"] = function(session, _)
state.exceptions_options = vim.iter(session.capabilities.exceptionBreakpointFilters)
:map(function(filter)
return { enabled = filter.default, exception_filter = filter }
end)
:totable()
end

dap.listeners.before.event_terminated.dapui_config = function()
actions.close()
end

dap.listeners.before.event_exited.dapui_config = function()
actions.close()
end
1 change: 1 addition & 0 deletions lua/dap-view/globals.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
return {
MAIN_BUF_NAME = "dap-view://main",
NAMESPACE = vim.api.nvim_create_namespace("dap-view"),
}
6 changes: 6 additions & 0 deletions lua/dap-view/state.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
---@class ExceptionsOption
---@field exception_filter dap.ExceptionBreakpointsFilter[]
---@field enabled boolean

---@class State
---@field bufnr? integer
---@field winnr? integer
---@field current_section? SectionType
---@field exceptions_options? ExceptionsOption[]
local M = {
bufnr = nil,
winnr = nil,
current_section = nil,
exceptions_options = nil,
}

return M
7 changes: 4 additions & 3 deletions lua/dap-view/winbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ M.set_winbar = function(selected_section)
winbar_info[selected_section].action()
end

---@param selected_section SectionType
M.update_winbar = function(selected_section)
set_winbar_opt(selected_section)
---@param section_name SectionType
M.update_winbar = function(section_name)
state.current_section = section_name
set_winbar_opt(state.current_section)
end

return M

0 comments on commit 918344c

Please sign in to comment.