fix(dap.hover): cleanup

This commit is contained in:
2025-09-27 00:22:18 +02:00
parent 80a563dc9c
commit f8f0cd8f89
3 changed files with 304 additions and 88 deletions
+115 -85
View File
@@ -1,35 +1,62 @@
-- DAP hover implementation with tree-based display
local Content = require("ow.dap.hover.content") local Content = require("ow.dap.hover.content")
local Item = require("ow.dap.item") local Item = require("ow.dap.item")
local Tree = require("ow.dap.hover.tree") local Tree = require("ow.dap.hover.tree")
local log = require("ow.log") local log = require("ow.log")
---@class ow.dap.hover.Window ---@class ow.dap.hover.Window
---@field current_win? integer Currently active hover window ID ---@field NAMESPACE string
---@field ns_id integer Namespace for extmarks ---@field max_width? integer
---@field tree ow.dap.hover.Tree? Current tree formatter ---@field max_height? integer
---@field winid? integer
---@field bufnr? integer
---@field NS_ID integer
---@field augroup? integer
---@field tree ow.dap.hover.Tree?
local Window = {} local Window = {}
Window.__index = Window
Window.MAX_WIDTH = nil Window.NAMESPACE = "ow.dap.hover.Window"
Window.MAX_HEIGHT = nil Window.NS_ID = vim.api.nvim_create_namespace(Window.NAMESPACE)
Window.ns_id = vim.api.nvim_create_namespace("ow.dap.hover")
---Close any existing hover window local instance = nil
function Window.close()
if Window.current_win and vim.api.nvim_win_is_valid(Window.current_win) then ---@return ow.dap.hover.Window
vim.api.nvim_win_close(Window.current_win, true) function Window.get_instance()
end if not instance then
Window.current_win = nil instance = setmetatable({
Window.tree = nil max_width = nil,
max_height = nil,
winid = nil,
bufnr = nil,
augroup = nil,
}, Window)
end
return instance
end
function Window:close()
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
end
if self.augroup then
vim.api.nvim_del_augroup_by_id(self.augroup)
end
self.augroup = nil
self.winid = nil
self.bufnr = nil
self.tree = nil
end end
---@param lines string[]
---@return integer ---@return integer
function Window.compute_width(lines) function Window:compute_width()
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, true)
local max_width = 1 local max_width = 1
for _, line in ipairs(lines) do for _, line in ipairs(lines) do
if Window.MAX_WIDTH and #line >= Window.MAX_WIDTH then if self.max_width and #line >= self.max_width then
max_width = Window.MAX_WIDTH max_width = self.max_width
break break
end end
max_width = math.max(max_width, #line) max_width = math.max(max_width, #line)
@@ -38,20 +65,24 @@ function Window.compute_width(lines)
return max_width return max_width
end end
---Create and display hover window with tree content ---@return integer
function Window:compute_height()
local text_height = vim.api.nvim_win_text_height(self.winid, {}).all
return math.min(self.max_height or text_height, text_height)
end
---@param lines string[] ---@param lines string[]
---@param content ow.dap.hover.Content ---@param content ow.dap.hover.Content
function Window.show(lines, content) function Window:show(lines, content)
-- Create buffer local prev_buf = vim.api.nvim_get_current_buf()
local orig_buf = vim.api.nvim_get_current_buf() self.bufnr = vim.api.nvim_create_buf(false, true)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.api.nvim_set_option_value("modifiable", false, { buf = buf })
-- Create window (initially hidden for size calculation) vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
local win = vim.api.nvim_open_win(buf, false, { vim.bo[self.bufnr].modifiable = false
self.winid = vim.api.nvim_open_win(self.bufnr, false, {
relative = "cursor", relative = "cursor",
width = Window.compute_width(lines), width = self:compute_width(),
height = 1, height = 1,
row = 1, row = 1,
col = 0, col = 0,
@@ -60,84 +91,84 @@ function Window.show(lines, content)
hide = true, hide = true,
}) })
-- Calculate and apply final height vim.api.nvim_win_set_config(self.winid, {
local text_height = vim.api.nvim_win_text_height(win, {}).all height = self:compute_height(),
vim.api.nvim_win_set_config(win, {
height = math.min(Window.MAX_HEIGHT or text_height, text_height),
hide = false, hide = false,
}) })
-- Apply syntax highlighting content:apply_highlights(Window.NS_ID, self.bufnr, 0)
content:apply_highlights(Window.ns_id, buf, 0)
-- Store window reference self.augroup =
Window.current_win = win vim.api.nvim_create_augroup(Window.NAMESPACE, { clear = true })
-- Set up auto-close behavior
vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter" }, { vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter" }, {
buffer = orig_buf, group = self.augroup,
buffer = prev_buf,
once = true, once = true,
callback = Window.close, callback = self.close,
}) })
vim.api.nvim_create_autocmd("WinLeave", { vim.api.nvim_create_autocmd("BufEnter", {
buffer = buf, group = self.augroup,
once = true, callback = function(arg)
callback = Window.close, if arg.buf ~= self.bufnr then
self:close()
return true
end
end,
}) })
-- Set up expansion keymaps vim.keymap.set(
vim.keymap.set("n", "<CR>", function() "n",
Window.expand_at_cursor(buf) "<CR>",
end, { buffer = buf, nowait = true }) self.expand_at_cursor,
{ buffer = self.bufnr, nowait = true }
)
vim.keymap.set("n", "<Tab>", function() vim.keymap.set(
Window.expand_at_cursor(buf) "n",
end, { buffer = buf, nowait = true }) "<Tab>",
self.expand_at_cursor,
{ buffer = self.bufnr, nowait = true }
)
end end
---@param buf integer
---@param callback fun() ---@param callback fun()
function Window.update_buffer(buf, callback) function Window:update_buffer(callback)
local prev_scrolloff = vim.wo[Window.current_win].scrolloff local prev_scrolloff = vim.wo[self.winid].scrolloff
vim.wo[Window.current_win].scrolloff = 0 vim.wo[self.winid].scrolloff = 0
vim.bo[buf].modifiable = true vim.bo[self.bufnr].modifiable = true
callback() callback()
vim.bo[buf].modifiable = false vim.bo[self.bufnr].modifiable = false
vim.wo[Window.current_win].scrolloff = prev_scrolloff vim.wo[self.winid].scrolloff = prev_scrolloff
end end
---Expand/collapse item at cursor position function Window:expand_at_cursor()
---@param buf integer if not self.tree then
function Window.expand_at_cursor(buf)
if not Window.tree then
return return
end end
-- Re-render the tree
coroutine.wrap(function() coroutine.wrap(function()
local ok, err = xpcall(function() local ok, err = xpcall(function()
-- Toggle expansion local lnum = vim.api.nvim_win_get_cursor(self.winid)[1]
local lnum = vim.api.nvim_win_get_cursor(Window.current_win)[1] local node = self.tree:get_node_at_line(lnum)
local node = Window.tree:get_node_at_line(lnum)
if not node or not node:is_container() then if not node or not node:is_container() then
return return
end end
local prev_node_count = Window.tree:count_subtree_nodes(node) local prev_node_count = self.tree:count_subtree_nodes(node)
local success = Window.tree:toggle_node(node) local success = self.tree:toggle_node(node)
if not success then if not success then
return return
end end
local content = Content.new() local content = Content.new()
Window.tree:render_subtree(node, content) self.tree:render_subtree(node, content)
local lines = content:get_lines() local lines = content:get_lines()
Window.update_buffer(buf, function() self:update_buffer(function()
vim.api.nvim_buf_set_lines( vim.api.nvim_buf_set_lines(
buf, self.bufnr,
lnum - 1, lnum - 1,
lnum - 1 + prev_node_count, lnum - 1 + prev_node_count,
true, true,
@@ -145,19 +176,16 @@ function Window.expand_at_cursor(buf)
) )
end) end)
-- Apply highlights content:apply_highlights(Window.NS_ID, self.bufnr, lnum - 1)
content:apply_highlights(Window.ns_id, buf, lnum - 1)
-- Adjust window size vim.api.nvim_win_set_config(self.winid, {
local all_lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) width = self:compute_width(),
vim.api.nvim_win_set_config(Window.current_win, {
width = Window.compute_width(all_lines),
}) })
local text_height = local text_height =
vim.api.nvim_win_text_height(Window.current_win, {}).all vim.api.nvim_win_text_height(self.winid, {}).all
vim.api.nvim_win_set_config(Window.current_win, { vim.api.nvim_win_set_config(self.winid, {
height = math.min( height = math.min(
Window.MAX_HEIGHT or text_height, self.max_height or text_height,
text_height text_height
), ),
}) })
@@ -185,8 +213,9 @@ local function hover_eval(
col_nr, col_nr,
current_file current_file
) )
local win = Window.get_instance()
-- Close existing hover window -- Close existing hover window
Window.close() win:close()
-- Evaluate expression -- Evaluate expression
local eval_request = { local eval_request = {
@@ -217,18 +246,19 @@ local function hover_eval(
local lines = content:get_lines() local lines = content:get_lines()
-- Store formatter for expansion -- Store formatter for expansion
Window.tree = tree win.tree = tree
-- Show hover window -- Show hover window
Window.show(lines, content) win:show(lines, content)
end end
---Public hover function ---Public hover function
---@async ---@async
local function hover_async() local function hover_async()
-- Check if hover window is already open - focus it instead -- Check if hover window is already open - focus it instead
if Window.current_win and vim.api.nvim_win_is_valid(Window.current_win) then local win = Window.get_instance()
vim.api.nvim_set_current_win(Window.current_win) if win.winid and vim.api.nvim_win_is_valid(win.winid) then
vim.api.nvim_set_current_win(win.winid)
return return
end end
+190
View File
@@ -0,0 +1,190 @@
local Content = require("ow.dap.hover.content")
local log = require("ow.log")
---@class ow.dap.hover.Window
---@field NAMESPACE string
---@field max_width? integer
---@field max_height? integer
---@field winid? integer
---@field bufnr? integer
---@field NS_ID integer
---@field augroup? integer
---@field tree ow.dap.hover.Tree?
local Window = {}
Window.__index = Window
Window.NAMESPACE = "ow.dap.hover.Window"
Window.NS_ID = vim.api.nvim_create_namespace(Window.NAMESPACE)
local instance = nil
---@return ow.dap.hover.Window
function Window.get_instance()
if not instance then
instance = setmetatable({
max_width = nil,
max_height = nil,
winid = nil,
bufnr = nil,
augroup = nil,
}, Window)
end
return instance
end
function Window:close()
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
end
if self.augroup then
vim.api.nvim_del_augroup_by_id(self.augroup)
end
self.augroup = nil
self.winid = nil
self.bufnr = nil
self.tree = 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
if self.max_width and #line >= self.max_width then
max_width = self.max_width
break
end
max_width = math.max(max_width, #line)
end
return max_width
end
---@return integer
function Window:compute_height()
local text_height = vim.api.nvim_win_text_height(self.winid, {}).all
return math.min(self.max_height or text_height, text_height)
end
---@param lines string[]
---@param content ow.dap.hover.Content
function Window:show(lines, content)
local prev_buf = vim.api.nvim_get_current_buf()
self.bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
vim.bo[self.bufnr].modifiable = false
self.winid = vim.api.nvim_open_win(self.bufnr, false, {
relative = "cursor",
width = self:compute_width(),
height = 1,
row = 1,
col = 0,
border = "rounded",
style = "minimal",
hide = true,
})
vim.api.nvim_win_set_config(self.winid, {
height = self:compute_height(),
hide = false,
})
content:apply_highlights(Window.NS_ID, self.bufnr, 0)
self.augroup =
vim.api.nvim_create_augroup(Window.NAMESPACE, { clear = true })
vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter" }, {
group = self.augroup,
buffer = prev_buf,
once = true,
callback = function()
self:close()
end,
})
vim.api.nvim_create_autocmd("BufEnter", {
group = self.augroup,
callback = function(arg)
if arg.buf ~= self.bufnr then
self:close()
return true
end
end,
})
vim.keymap.set("n", "<CR>", function()
self:expand_at_cursor()
end, { buffer = self.bufnr, nowait = true })
vim.keymap.set("n", "<Tab>", function()
self:expand_at_cursor()
end, { buffer = self.bufnr, nowait = true })
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
function Window:expand_at_cursor()
if not self.tree then
return
end
coroutine.wrap(function()
local ok, err = xpcall(function()
local lnum = vim.api.nvim_win_get_cursor(self.winid)[1]
local node = self.tree:get_node_at_line(lnum)
if not node or not node:is_container() then
return
end
local prev_node_count = self.tree:count_subtree_nodes(node)
local success = self.tree:toggle_node(node)
if not success then
return
end
local content = Content.new()
self.tree:render_subtree(node, content)
local lines = content:get_lines()
self:update_buffer(function()
vim.api.nvim_buf_set_lines(
self.bufnr,
lnum - 1,
lnum - 1 + prev_node_count,
true,
lines
)
end)
content:apply_highlights(Window.NS_ID, self.bufnr, lnum - 1)
vim.api.nvim_win_set_config(self.winid, {
width = self:compute_width(),
})
local text_height = vim.api.nvim_win_text_height(self.winid, {}).all
vim.api.nvim_win_set_config(self.winid, {
height = math.min(self.max_height or text_height, text_height),
})
end, debug.traceback)
if not ok then
log.error("Expansion failed:\n%s", err)
end
end)()
end
return Window
-4
View File
@@ -1,5 +1,3 @@
-- DAP variable item representation
---@class ow.dap.Item ---@class ow.dap.Item
---@field name string ---@field name string
---@field type string ---@field type string
@@ -9,7 +7,6 @@
local Item = {} local Item = {}
Item.__index = Item Item.__index = Item
---Create a new item
---@param name string ---@param name string
---@param type string ---@param type string
---@param value string ---@param value string
@@ -26,7 +23,6 @@ function Item.new(name, type, value, variablesReference, depth)
}, Item) }, Item)
end end
---Create item from DAP variable
---@param var dap.Variable ---@param var dap.Variable
---@param depth integer ---@param depth integer
---@return ow.dap.Item ---@return ow.dap.Item