fix(dap.hover): don't show null pointers as expandable

This commit is contained in:
2025-09-28 13:36:02 +02:00
parent 0916fa1cb8
commit 154de39931
3 changed files with 29 additions and 24 deletions
+21 -11
View File
@@ -1,4 +1,5 @@
---@class ow.dap.hover.Node ---@class ow.dap.hover.Node
---@field lang string
---@field item ow.dap.Item ---@field item ow.dap.Item
---@field parent ow.dap.hover.Node? ---@field parent ow.dap.hover.Node?
---@field children ow.dap.hover.Node[] ---@field children ow.dap.hover.Node[]
@@ -9,9 +10,11 @@ Node.__index = Node
---@param item ow.dap.Item ---@param item ow.dap.Item
---@param parent ow.dap.hover.Node? ---@param parent ow.dap.hover.Node?
---@param lang string
---@return ow.dap.hover.Node ---@return ow.dap.hover.Node
function Node.new(item, parent) function Node.new(item, parent, lang)
return setmetatable({ return setmetatable({
lang = lang,
item = item, item = item,
parent = parent, parent = parent,
children = {}, children = {},
@@ -26,9 +29,14 @@ function Node:is_container()
or false or false
end end
function Node:is_c_lang()
return self.lang == "c" or self.lang == "cpp"
end
---@return boolean ---@return boolean
function Node:is_c_pointer() function Node:is_c_pointer()
return self:is_container() return self:is_c_lang()
and self:is_container()
and self.item.type:match( and self.item.type:match(
"%*%s*[const%s]*[volatile%s]*[restrict%s]*$" "%*%s*[const%s]*[volatile%s]*[restrict%s]*$"
) )
@@ -69,7 +77,7 @@ end
---@return boolean ---@return boolean
function Node:is_c_array_element() function Node:is_c_array_element()
return self.item.name:match("^%[?%d+%]?$") ~= nil return self:is_c_lang() and self.item.name:match("^%[?%d+%]?$") ~= nil
end end
---@return string ---@return string
@@ -124,10 +132,14 @@ function Node:get_full_expression()
return expr return expr
end end
---@param session dap.Session ---@return boolean
function Node:is_expandable()
return self:is_container() and not self:is_c_null_pointer()
end
---@param content ow.dap.hover.Content ---@param content ow.dap.hover.Content
function Node:format_into(session, content) function Node:format_into(content)
if self:is_container() then if self:is_expandable() then
local marker = self.is_expanded and "-" or "+" local marker = self.is_expanded and "-" or "+"
content:add(marker .. " ", "@comment") content:add(marker .. " ", "@comment")
else else
@@ -140,15 +152,13 @@ function Node:format_into(session, content)
end end
local text local text
if session.filetype == "c" or session.filetype == "cpp" then if self:is_c_lang() then
text = self:format_c() text = self:format_c()
else else
error( error(string.format("Formatting for %s not implemented", self.lang))
string.format("Formatting for %s not implemented", session.filetype)
)
end end
content:add_with_treesitter(text, session.filetype) content:add_with_treesitter(text, self.lang)
if self.item.value == "" then if self.item.value == "" then
content:add("...", "@comment") content:add("...", "@comment")
+6 -11
View File
@@ -4,6 +4,7 @@ local Node = require("ow.dap.hover.node")
local log = require("ow.log") local log = require("ow.log")
---@class ow.dap.hover.Tree ---@class ow.dap.hover.Tree
---@field lang string
---@field session dap.Session ---@field session dap.Session
---@field root ow.dap.hover.Node? ---@field root ow.dap.hover.Node?
local Tree = {} local Tree = {}
@@ -13,6 +14,7 @@ Tree.__index = Tree
---@return ow.dap.hover.Tree ---@return ow.dap.hover.Tree
function Tree.new(session) function Tree.new(session)
return setmetatable({ return setmetatable({
lang = session.filetype,
session = session, session = session,
root = nil, root = nil,
}, Tree) }, Tree)
@@ -21,7 +23,7 @@ end
---@async ---@async
---@param item ow.dap.Item ---@param item ow.dap.Item
function Tree:build(item) function Tree:build(item)
self.root = Node.new(item, nil) self.root = Node.new(item, nil, self.lang)
if self.root:is_container() then if self.root:is_container() then
self:load_children(self.root) self:load_children(self.root)
@@ -49,7 +51,7 @@ function Tree:load_children(node)
for i, var in ipairs(resp.variables) do for i, var in ipairs(resp.variables) do
local item = Item.from_var(var) local item = Item.from_var(var)
local child = Node.new(item, node) local child = Node.new(item, node, self.lang)
child.is_last_child = (i == #resp.variables) child.is_last_child = (i == #resp.variables)
if item.name:match("^%d+$") then if item.name:match("^%d+$") then
@@ -79,7 +81,7 @@ end
---@param node ow.dap.hover.Node ---@param node ow.dap.hover.Node
---@param content ow.dap.hover.Content ---@param content ow.dap.hover.Content
function Tree:render_subtree(node, content) function Tree:render_subtree(node, content)
node:format_into(self.session, content) node:format_into(content)
if node.is_expanded then if node.is_expanded then
for _, child in ipairs(node.children) do for _, child in ipairs(node.children) do
@@ -177,14 +179,7 @@ end
---@param node ow.dap.hover.Node ---@param node ow.dap.hover.Node
---@return boolean success ---@return boolean success
function Tree:expand_all_children(node) function Tree:expand_all_children(node)
if not node:is_container() then if not node:is_expandable() then
return true
end
if
(self.session.filetype == "c" or self.session.filetype == "cpp")
and node:is_c_null_pointer()
then
return true return true
end end
+2 -2
View File
@@ -260,7 +260,7 @@ function Window:toggle_node()
coroutine.wrap(function() coroutine.wrap(function()
local ok, err = xpcall(function() local ok, err = xpcall(function()
local info = self:get_current_node_info() local info = self:get_current_node_info()
if not info:is_valid() or not info.node:is_container() then if not info:is_valid() or not info.node:is_expandable() then
return return
end end
@@ -308,7 +308,7 @@ function Window:expand_all_at_cursor()
coroutine.wrap(function() coroutine.wrap(function()
local ok, err = xpcall(function() local ok, err = xpcall(function()
local info = self:get_current_node_info() local info = self:get_current_node_info()
if not info:is_valid() or not info.node:is_container() then if not info:is_valid() or not info.node:is_expandable() then
return return
end end