From 154de39931b3ae77248902c4adfc407b746c2fec Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Sun, 28 Sep 2025 13:36:02 +0200 Subject: [PATCH] fix(dap.hover): don't show null pointers as expandable --- lua/ow/dap/hover/node.lua | 32 +++++++++++++++++++++----------- lua/ow/dap/hover/tree.lua | 17 ++++++----------- lua/ow/dap/hover/window.lua | 4 ++-- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lua/ow/dap/hover/node.lua b/lua/ow/dap/hover/node.lua index 73b8347..107e9b0 100644 --- a/lua/ow/dap/hover/node.lua +++ b/lua/ow/dap/hover/node.lua @@ -1,4 +1,5 @@ ---@class ow.dap.hover.Node +---@field lang string ---@field item ow.dap.Item ---@field parent ow.dap.hover.Node? ---@field children ow.dap.hover.Node[] @@ -9,9 +10,11 @@ Node.__index = Node ---@param item ow.dap.Item ---@param parent ow.dap.hover.Node? +---@param lang string ---@return ow.dap.hover.Node -function Node.new(item, parent) +function Node.new(item, parent, lang) return setmetatable({ + lang = lang, item = item, parent = parent, children = {}, @@ -26,9 +29,14 @@ function Node:is_container() or false end +function Node:is_c_lang() + return self.lang == "c" or self.lang == "cpp" +end + ---@return boolean function Node:is_c_pointer() - return self:is_container() + return self:is_c_lang() + and self:is_container() and self.item.type:match( "%*%s*[const%s]*[volatile%s]*[restrict%s]*$" ) @@ -69,7 +77,7 @@ end ---@return boolean 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 ---@return string @@ -124,10 +132,14 @@ function Node:get_full_expression() return expr 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 -function Node:format_into(session, content) - if self:is_container() then +function Node:format_into(content) + if self:is_expandable() then local marker = self.is_expanded and "-" or "+" content:add(marker .. " ", "@comment") else @@ -140,15 +152,13 @@ function Node:format_into(session, content) end local text - if session.filetype == "c" or session.filetype == "cpp" then + if self:is_c_lang() then text = self:format_c() else - error( - string.format("Formatting for %s not implemented", session.filetype) - ) + error(string.format("Formatting for %s not implemented", self.lang)) end - content:add_with_treesitter(text, session.filetype) + content:add_with_treesitter(text, self.lang) if self.item.value == "" then content:add("...", "@comment") diff --git a/lua/ow/dap/hover/tree.lua b/lua/ow/dap/hover/tree.lua index 6ea37a5..3733542 100644 --- a/lua/ow/dap/hover/tree.lua +++ b/lua/ow/dap/hover/tree.lua @@ -4,6 +4,7 @@ local Node = require("ow.dap.hover.node") local log = require("ow.log") ---@class ow.dap.hover.Tree +---@field lang string ---@field session dap.Session ---@field root ow.dap.hover.Node? local Tree = {} @@ -13,6 +14,7 @@ Tree.__index = Tree ---@return ow.dap.hover.Tree function Tree.new(session) return setmetatable({ + lang = session.filetype, session = session, root = nil, }, Tree) @@ -21,7 +23,7 @@ end ---@async ---@param item ow.dap.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 self:load_children(self.root) @@ -49,7 +51,7 @@ function Tree:load_children(node) for i, var in ipairs(resp.variables) do 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) if item.name:match("^%d+$") then @@ -79,7 +81,7 @@ end ---@param node ow.dap.hover.Node ---@param content ow.dap.hover.Content function Tree:render_subtree(node, content) - node:format_into(self.session, content) + node:format_into(content) if node.is_expanded then for _, child in ipairs(node.children) do @@ -177,14 +179,7 @@ end ---@param node ow.dap.hover.Node ---@return boolean success function Tree:expand_all_children(node) - if not node:is_container() then - return true - end - - if - (self.session.filetype == "c" or self.session.filetype == "cpp") - and node:is_c_null_pointer() - then + if not node:is_expandable() then return true end diff --git a/lua/ow/dap/hover/window.lua b/lua/ow/dap/hover/window.lua index 2689ad2..0e366c2 100644 --- a/lua/ow/dap/hover/window.lua +++ b/lua/ow/dap/hover/window.lua @@ -260,7 +260,7 @@ function Window:toggle_node() coroutine.wrap(function() local ok, err = xpcall(function() 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 end @@ -308,7 +308,7 @@ function Window:expand_all_at_cursor() coroutine.wrap(function() local ok, err = xpcall(function() 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 end