refactor: correctness fixes, API modernization, and cleanup

This commit is contained in:
2026-04-13 01:20:52 +02:00
parent 62e64182de
commit fc7060e0ed
12 changed files with 166 additions and 223 deletions
+6 -1
View File
@@ -70,7 +70,12 @@ function Content:add_with_treesitter(text, lang)
return
end
local tree = parser:parse()[1]
local trees = parser:parse()
if not trees then
return
end
local tree = trees[1]
if not tree then
return
end
+2
View File
@@ -93,6 +93,7 @@ function Node:index_of(target)
return search(self)
end
---@async
---@param session dap.Session
function Node:expand_all(session)
if not self:is_expandable() then
@@ -139,6 +140,7 @@ end
function Node:is_c_pointer()
return self:is_c_lang()
and self:is_container()
and self.item.type ~= nil
and self.item.type:match(
"%*%s*[const%s]*[volatile%s]*[restrict%s]*$"
)
+52 -59
View File
@@ -6,9 +6,9 @@ local log = require("log")
---@field NS_ID integer
---@field max_width? integer
---@field max_height? integer
---@field winid? integer
---@field bufnr? integer
---@field augroup? integer
---@field winid integer
---@field bufnr integer
---@field augroup integer
---@field session dap.Session
---@field root ow.dap.hover.Node
local Window = {}
@@ -32,36 +32,25 @@ vim.api.nvim_create_autocmd("ColorScheme", {
callback = setup_highlights,
})
---@type ow.dap.hover.Window?
local instance = nil
---@param session dap.Session
---@return ow.dap.hover.Window
function Window.get_instance(session)
if instance then
return instance
---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
instance = setmetatable({
max_width = nil,
max_height = nil,
winid = nil,
bufnr = nil,
augroup = nil,
session = session,
root = nil,
}, Window)
return instance
return false
end
function Window:destroy()
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
if 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
vim.api.nvim_del_augroup_by_id(self.augroup)
instance = nil
end
@@ -237,13 +226,17 @@ function Window:yank_value()
end
---@async
---@param session dap.Session
---@param root ow.dap.hover.Node
function Window:show(root)
self.root = root
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()
self.bufnr = vim.api.nvim_create_buf(false, true)
self.winid = vim.api.nvim_open_win(self.bufnr, false, {
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,
@@ -252,85 +245,85 @@ function Window:show(root)
border = "rounded",
style = "minimal",
})
vim.wo[self.winid].wrap = false
vim.wo[win.winid].wrap = false
self:refresh_tree(self.root, 1, 1)
win:refresh_tree(win.root, 1, 1)
self.augroup =
win.augroup =
vim.api.nvim_create_augroup(Window.NAMESPACE, { clear = true })
vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter" }, {
group = self.augroup,
group = win.augroup,
buffer = prev_buf,
once = true,
callback = function()
self:destroy()
win:destroy()
end,
})
vim.api.nvim_create_autocmd("BufEnter", {
group = self.augroup,
group = win.augroup,
callback = function(arg)
if arg.buf ~= self.bufnr then
self:destroy()
if arg.buf ~= win.bufnr then
win:destroy()
return true
end
end,
})
vim.api.nvim_create_autocmd("WinClosed", {
group = self.augroup,
group = win.augroup,
once = true,
pattern = tostring(self.winid),
pattern = tostring(win.winid),
callback = function()
self:destroy()
win:destroy()
end,
})
-- Toggle expand/collapse
vim.keymap.set("n", "<CR>", function()
self:toggle_node()
end, { buffer = self.bufnr, nowait = true })
win:toggle_node()
end, { buffer = win.bufnr, nowait = true })
vim.keymap.set("n", "<Tab>", function()
self:toggle_node()
end, { buffer = self.bufnr, nowait = true })
win:toggle_node()
end, { buffer = win.bufnr, nowait = true })
-- Collapse
vim.keymap.set("n", "<S-Tab>", function()
self:collapse_parent()
end, { buffer = self.bufnr, nowait = true })
win:collapse_parent()
end, { buffer = win.bufnr, nowait = true })
vim.keymap.set("n", "<BS>", function()
self:collapse_parent()
end, { buffer = self.bufnr, nowait = true })
win:collapse_parent()
end, { buffer = win.bufnr, nowait = true })
-- Tree operations
vim.keymap.set("n", "E", function()
self:expand_all_at_cursor()
end, { buffer = self.bufnr, nowait = true })
win:expand_all_at_cursor()
end, { buffer = win.bufnr, nowait = true })
vim.keymap.set("n", "C", function()
self:collapse_all_at_cursor()
end, { buffer = self.bufnr, nowait = true })
win:collapse_all_at_cursor()
end, { buffer = win.bufnr, nowait = true })
-- Navigation
vim.keymap.set("n", "p", function()
self:goto_parent()
end, { buffer = self.bufnr, nowait = true })
win:goto_parent()
end, { buffer = win.bufnr, nowait = true })
-- Quick actions
vim.keymap.set("n", "q", function()
self:destroy()
end, { buffer = self.bufnr, nowait = true })
win:destroy()
end, { buffer = win.bufnr, nowait = true })
vim.keymap.set("n", "<Esc>", function()
self:destroy()
end, { buffer = self.bufnr, nowait = true })
win:destroy()
end, { buffer = win.bufnr, nowait = true })
-- Yank operations
vim.keymap.set("n", "y", function()
self:yank_value()
end, { buffer = self.bufnr, nowait = true })
win:yank_value()
end, { buffer = win.bufnr, nowait = true })
end
return Window