local Content = require("dap.hover.content") local log = require("log") ---@class ow.dap.hover.Window ---@field NAMESPACE string ---@field NS_ID integer ---@field max_width? integer ---@field max_height? integer ---@field winid integer ---@field bufnr integer ---@field augroup integer ---@field session dap.Session ---@field root ow.dap.hover.Node local Window = {} Window.__index = Window Window.NAMESPACE = "dap.hover.Window" Window.NS_ID = vim.api.nvim_create_namespace(Window.NAMESPACE) local function setup_highlights() vim.api.nvim_set_hl(0, "DapHoverPrefix", { link = "@comment", }) vim.api.nvim_set_hl(0, "DapHoverExpandMarker", { link = "@comment", }) end setup_highlights() vim.api.nvim_create_autocmd("ColorScheme", { callback = setup_highlights, }) ---@type ow.dap.hover.Window? local instance = nil ---Focus the hover window if one is currently shown. ---@return boolean focused true if a window was shown and is now focused function Window.focus_if_shown() if instance and vim.api.nvim_win_is_valid(instance.winid) then vim.api.nvim_set_current_win(instance.winid) return true end return false end function Window:destroy() if vim.api.nvim_win_is_valid(self.winid) then vim.api.nvim_win_close(self.winid, true) end vim.api.nvim_del_augroup_by_id(self.augroup) instance = nil end ---@return integer function Window:compute_width() local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, true) local max_width = 1 for _, line in ipairs(lines) do local line_width = vim.api.nvim_strwidth(line) if self.max_width and line_width >= self.max_width then max_width = self.max_width break end max_width = math.max(max_width, line_width) end return max_width end ---@return integer function Window:compute_height() return vim.api.nvim_win_text_height(self.winid, { max_height = self.max_height, }).all end ---@param callback fun() function Window:update_buffer(callback) local prev_scrolloff = vim.wo[self.winid].scrolloff vim.wo[self.winid].scrolloff = 0 vim.bo[self.bufnr].modifiable = true callback() vim.bo[self.bufnr].modifiable = false vim.wo[self.winid].scrolloff = prev_scrolloff end ---@async ---@param node ow.dap.hover.Node ---@param start_line integer 1-indexed line number ---@param line_count integer number of lines to replace function Window:refresh_tree(node, start_line, line_count) local content = Content.new() node:format_into(content) local lines = content:get_lines() self:update_buffer(function() vim.api.nvim_buf_set_lines( self.bufnr, start_line - 1, start_line - 1 + line_count, true, lines ) end) content:apply_highlights(Window.NS_ID, self.bufnr, start_line - 1) vim.api.nvim_win_set_config(self.winid, { width = self:compute_width(), }) vim.api.nvim_win_set_config(self.winid, { height = self:compute_height(), }) end function Window:toggle_node() coroutine.wrap(function() local ok, err = xpcall(function() local lnum = vim.api.nvim_win_get_cursor(self.winid)[1] local node = self.root:at(lnum) if not node or not node:is_expandable() then return end if not node.is_expanded then local success = node:load_children(self.session) if not success then return end end local prev_size = node:size() node.is_expanded = not node.is_expanded self:refresh_tree(node, lnum, prev_size) end, debug.traceback) if not ok then log.error("Expansion failed:\n%s", err) end end)() end function Window:collapse_parent() coroutine.wrap(function() local ok, err = xpcall(function() if self:goto_parent() then self:toggle_node() end end, debug.traceback) if not ok then log.error("Collapse failed:\n%s", err) end end)() end function Window:expand_all_at_cursor() coroutine.wrap(function() local ok, err = xpcall(function() local lnum = vim.api.nvim_win_get_cursor(self.winid)[1] local node = self.root:at(lnum) if not node or not node:is_expandable() then return end local prev_size = node:size() node:expand_all(self.session) self:refresh_tree(node, lnum, prev_size) end, debug.traceback) if not ok then log.error("Expand all failed:\n%s", err) end end)() end function Window:collapse_all_at_cursor() coroutine.wrap(function() local ok, err = xpcall(function() local lnum = vim.api.nvim_win_get_cursor(self.winid)[1] local node = self.root:at(lnum) if not node or not node:is_expandable() then return end local prev_size = node:size() node:collapse_all() self:refresh_tree(node, lnum, prev_size) end, debug.traceback) if not ok then log.error("Collapse all failed:\n%s", err) end end)() end ---@return boolean success if parent line was found function Window:goto_parent() local lnum = vim.api.nvim_win_get_cursor(self.winid)[1] local node = self.root:at(lnum) if not node or not node.parent then return false end local parent_line = self.root:index_of(node.parent) if parent_line then vim.api.nvim_win_set_cursor(self.winid, { parent_line, 0 }) return true end return false end function Window:yank_value() local lnum = vim.api.nvim_win_get_cursor(self.winid)[1] local node = self.root:at(lnum) if not node then return end vim.fn.setreg('"', node.item.value) vim.fn.setreg("+", node.item.value) end ---@async ---@param session dap.Session ---@param root ow.dap.hover.Node function Window.show(session, root) local win = instance or setmetatable({}, Window) instance = win win.session = session win.root = root local prev_buf = vim.api.nvim_get_current_buf() win.bufnr = vim.api.nvim_create_buf(false, true) win.winid = vim.api.nvim_open_win(win.bufnr, false, { relative = "cursor", width = 1, height = 1, row = 1, col = 0, border = "rounded", style = "minimal", }) vim.wo[win.winid].wrap = false win:refresh_tree(win.root, 1, 1) win.augroup = vim.api.nvim_create_augroup(Window.NAMESPACE, { clear = true }) vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter" }, { group = win.augroup, buffer = prev_buf, once = true, callback = function() win:destroy() end, }) vim.api.nvim_create_autocmd("BufEnter", { group = win.augroup, callback = function(arg) if arg.buf ~= win.bufnr then win:destroy() return true end end, }) vim.api.nvim_create_autocmd("WinClosed", { group = win.augroup, once = true, pattern = tostring(win.winid), callback = function() win:destroy() end, }) -- Toggle expand/collapse vim.keymap.set("n", "", function() win:toggle_node() end, { buffer = win.bufnr, nowait = true }) vim.keymap.set("n", "", function() win:toggle_node() end, { buffer = win.bufnr, nowait = true }) -- Collapse vim.keymap.set("n", "", function() win:collapse_parent() end, { buffer = win.bufnr, nowait = true }) vim.keymap.set("n", "", function() win:collapse_parent() end, { buffer = win.bufnr, nowait = true }) -- Tree operations vim.keymap.set("n", "E", function() win:expand_all_at_cursor() end, { buffer = win.bufnr, nowait = true }) vim.keymap.set("n", "C", function() win:collapse_all_at_cursor() end, { buffer = win.bufnr, nowait = true }) -- Navigation vim.keymap.set("n", "p", function() win:goto_parent() end, { buffer = win.bufnr, nowait = true }) -- Quick actions vim.keymap.set("n", "q", function() win:destroy() end, { buffer = win.bufnr, nowait = true }) vim.keymap.set("n", "", function() win:destroy() end, { buffer = win.bufnr, nowait = true }) -- Yank operations vim.keymap.set("n", "y", function() win:yank_value() end, { buffer = win.bufnr, nowait = true }) end return Window