fix(dap.hover): cleanup
This commit is contained in:
+6
-20
@@ -3,14 +3,13 @@ local Tree = require("ow.dap.hover.tree")
|
|||||||
local Window = require("ow.dap.hover.window")
|
local Window = require("ow.dap.hover.window")
|
||||||
local log = require("ow.log")
|
local log = require("ow.log")
|
||||||
|
|
||||||
---Main hover entry point
|
|
||||||
---@async
|
---@async
|
||||||
---@param expr string Expression to evaluate
|
---@param expr string
|
||||||
---@param session dap.Session DAP session
|
---@param session dap.Session
|
||||||
---@param frame_id number Current frame ID
|
---@param frame_id number
|
||||||
---@param line_nr integer Line number for context
|
---@param line_nr integer
|
||||||
---@param col_nr integer Column number for context
|
---@param col_nr integer
|
||||||
---@param current_file string Current file path
|
---@param current_file string
|
||||||
local function hover_eval(
|
local function hover_eval(
|
||||||
expr,
|
expr,
|
||||||
session,
|
session,
|
||||||
@@ -20,10 +19,8 @@ local function hover_eval(
|
|||||||
current_file
|
current_file
|
||||||
)
|
)
|
||||||
local win = Window.get_instance()
|
local win = Window.get_instance()
|
||||||
-- Close existing hover window
|
|
||||||
win:close()
|
win:close()
|
||||||
|
|
||||||
-- Evaluate expression
|
|
||||||
local eval_request = {
|
local eval_request = {
|
||||||
expression = expr,
|
expression = expr,
|
||||||
frameId = frame_id,
|
frameId = frame_id,
|
||||||
@@ -41,27 +38,21 @@ local function hover_eval(
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create item and tree formatter
|
|
||||||
local item =
|
local item =
|
||||||
Item.new(expr, resp.type, resp.result, resp.variablesReference, 0)
|
Item.new(expr, resp.type, resp.result, resp.variablesReference, 0)
|
||||||
local tree = Tree.new(session)
|
local tree = Tree.new(session)
|
||||||
|
|
||||||
-- Build and render tree
|
|
||||||
tree:build(item)
|
tree:build(item)
|
||||||
local content = tree:render()
|
local content = tree:render()
|
||||||
local lines = content:get_lines()
|
local lines = content:get_lines()
|
||||||
|
|
||||||
-- Store formatter for expansion
|
|
||||||
win.tree = tree
|
win.tree = tree
|
||||||
|
|
||||||
-- Show hover window
|
|
||||||
win:show(lines, content)
|
win:show(lines, content)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Public hover function
|
|
||||||
---@async
|
---@async
|
||||||
local function hover_async()
|
local function hover_async()
|
||||||
-- Check if hover window is already open - focus it instead
|
|
||||||
local win = Window.get_instance()
|
local win = Window.get_instance()
|
||||||
if win.winid and vim.api.nvim_win_is_valid(win.winid) then
|
if win.winid and vim.api.nvim_win_is_valid(win.winid) then
|
||||||
vim.api.nvim_set_current_win(win.winid)
|
vim.api.nvim_set_current_win(win.winid)
|
||||||
@@ -86,11 +77,9 @@ local function hover_async()
|
|||||||
local col_nr = cursor_pos[2] + 1 -- nvim-dap sets columnsStartAt1=true
|
local col_nr = cursor_pos[2] + 1 -- nvim-dap sets columnsStartAt1=true
|
||||||
local current_file = vim.api.nvim_buf_get_name(0)
|
local current_file = vim.api.nvim_buf_get_name(0)
|
||||||
|
|
||||||
-- Get expression under cursor
|
|
||||||
local expr
|
local expr
|
||||||
local mode = vim.api.nvim_get_mode()
|
local mode = vim.api.nvim_get_mode()
|
||||||
if mode.mode == "v" then
|
if mode.mode == "v" then
|
||||||
-- Visual mode selection
|
|
||||||
local start_pos = vim.fn.getpos("v")
|
local start_pos = vim.fn.getpos("v")
|
||||||
local end_pos = vim.fn.getpos(".")
|
local end_pos = vim.fn.getpos(".")
|
||||||
|
|
||||||
@@ -127,7 +116,6 @@ local function hover_async()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get thread and frame information
|
|
||||||
local thread_id
|
local thread_id
|
||||||
do
|
do
|
||||||
local err, resp = session:request("threads", nil)
|
local err, resp = session:request("threads", nil)
|
||||||
@@ -149,11 +137,9 @@ local function hover_async()
|
|||||||
frame_id = resp.stackFrames[1].id
|
frame_id = resp.stackFrames[1].id
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Evaluate and display hover
|
|
||||||
hover_eval(expr, session, frame_id, line_nr, col_nr, current_file)
|
hover_eval(expr, session, frame_id, line_nr, col_nr, current_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Wrapped hover function with error handling
|
|
||||||
local function hover()
|
local function hover()
|
||||||
coroutine.wrap(function()
|
coroutine.wrap(function()
|
||||||
local ok, err = xpcall(hover_async, debug.traceback)
|
local ok, err = xpcall(hover_async, debug.traceback)
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
-- Highlighted text content builder for DAP tree display
|
|
||||||
---@class ow.dap.hover.content.Capture
|
---@class ow.dap.hover.content.Capture
|
||||||
---@field start_col integer
|
---@field start_col integer
|
||||||
---@field end_col integer
|
---@field end_col integer
|
||||||
@@ -7,21 +6,20 @@
|
|||||||
---@field priority integer
|
---@field priority integer
|
||||||
|
|
||||||
---@class ow.dap.hover.Highlight
|
---@class ow.dap.hover.Highlight
|
||||||
---@field group string Highlight group name
|
---@field group string
|
||||||
---@field start_row integer Start line (0-indexed)
|
---@field start_row integer 0-indexed
|
||||||
---@field start_col integer Start column (0-indexed)
|
---@field start_col integer 0-indexed
|
||||||
---@field end_row integer End line (0-indexed)
|
---@field end_row integer 0-indexed
|
||||||
---@field end_col integer End column (0-indexed)
|
---@field end_col integer 0-indexed
|
||||||
|
|
||||||
---@class ow.dap.hover.Content
|
---@class ow.dap.hover.Content
|
||||||
---@field text string The complete text content
|
---@field text string
|
||||||
---@field highlights ow.dap.hover.Highlight[] List of highlights to apply
|
---@field highlights ow.dap.hover.Highlight[]
|
||||||
---@field _current_row integer Current line position (for building)
|
---@field _current_row integer
|
||||||
---@field _current_col integer Current column position (for building)
|
---@field _current_col integer
|
||||||
local Content = {}
|
local Content = {}
|
||||||
Content.__index = Content
|
Content.__index = Content
|
||||||
|
|
||||||
---Create new highlighted content
|
|
||||||
---@return ow.dap.hover.Content
|
---@return ow.dap.hover.Content
|
||||||
function Content.new()
|
function Content.new()
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
@@ -37,9 +35,8 @@ function Content:current_line()
|
|||||||
return self._current_row + 1
|
return self._current_row + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
---Add text with optional highlighting
|
---@param text string May not contain line breaks
|
||||||
---@param text string Text to add. May not contain line breaks.
|
---@param highlight_group? string
|
||||||
---@param highlight_group? string Optional highlight group
|
|
||||||
function Content:add(text, highlight_group)
|
function Content:add(text, highlight_group)
|
||||||
local start_row = self._current_row
|
local start_row = self._current_row
|
||||||
local start_col = self._current_col
|
local start_col = self._current_col
|
||||||
@@ -60,17 +57,14 @@ function Content:add(text, highlight_group)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Add text with tree-sitter syntax highlighting
|
---@param text string May not contain line breaks
|
||||||
---@param text string Text to add. May not contain line breaks.
|
---@param lang string
|
||||||
---@param lang string Language for tree-sitter highlighting
|
|
||||||
function Content:add_with_treesitter(text, lang)
|
function Content:add_with_treesitter(text, lang)
|
||||||
local start_row = self._current_row
|
local start_row = self._current_row
|
||||||
local start_col = self._current_col
|
local start_col = self._current_col
|
||||||
|
|
||||||
-- First, just add the text normally
|
|
||||||
self:add(text)
|
self:add(text)
|
||||||
|
|
||||||
-- Then apply tree-sitter highlights on top
|
|
||||||
local ok, parser = pcall(vim.treesitter.get_string_parser, text, lang)
|
local ok, parser = pcall(vim.treesitter.get_string_parser, text, lang)
|
||||||
if not ok or not parser then
|
if not ok or not parser then
|
||||||
return
|
return
|
||||||
@@ -86,19 +80,16 @@ function Content:add_with_treesitter(text, lang)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add highlights for all captures (overlapping is fine)
|
|
||||||
for id, node in query:iter_captures(tree:root(), text, 0, -1) do
|
for id, node in query:iter_captures(tree:root(), text, 0, -1) do
|
||||||
local capture_name = query.captures[id]
|
local capture_name = query.captures[id]
|
||||||
local start_row_rel, start_col_rel, end_row_rel, end_col_rel =
|
local start_row_rel, start_col_rel, end_row_rel, end_col_rel =
|
||||||
node:range()
|
node:range()
|
||||||
|
|
||||||
-- Convert to absolute positions
|
|
||||||
local abs_start_row = start_row + start_row_rel
|
local abs_start_row = start_row + start_row_rel
|
||||||
local abs_end_row = start_row + end_row_rel
|
local abs_end_row = start_row + end_row_rel
|
||||||
local abs_start_col = start_col + start_col_rel
|
local abs_start_col = start_col + start_col_rel
|
||||||
local abs_end_col = start_col + end_col_rel
|
local abs_end_col = start_col + end_col_rel
|
||||||
|
|
||||||
-- Add the highlight
|
|
||||||
table.insert(self.highlights, {
|
table.insert(self.highlights, {
|
||||||
group = "@" .. capture_name,
|
group = "@" .. capture_name,
|
||||||
start_row = abs_start_row,
|
start_row = abs_start_row,
|
||||||
@@ -109,23 +100,20 @@ function Content:add_with_treesitter(text, lang)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Add a newline and reset column tracking
|
|
||||||
function Content:newline()
|
function Content:newline()
|
||||||
self:add("\n")
|
self:add("\n")
|
||||||
self._current_col = 0
|
self._current_col = 0
|
||||||
self._current_row = self._current_row + 1
|
self._current_row = self._current_row + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the lines as a table
|
|
||||||
---@return string[]
|
---@return string[]
|
||||||
function Content:get_lines()
|
function Content:get_lines()
|
||||||
return vim.split(self.text, "\n", { trimempty = true })
|
return vim.split(self.text, "\n", { trimempty = true })
|
||||||
end
|
end
|
||||||
|
|
||||||
---Apply highlights to a buffer
|
|
||||||
---@param ns_id integer
|
---@param ns_id integer
|
||||||
---@param buf integer Buffer handle
|
---@param buf integer
|
||||||
---@param row_offset integer
|
---@param row_offset integer 0-indexed
|
||||||
function Content:apply_highlights(ns_id, buf, row_offset)
|
function Content:apply_highlights(ns_id, buf, row_offset)
|
||||||
for _, highlight in ipairs(self.highlights) do
|
for _, highlight in ipairs(self.highlights) do
|
||||||
vim.hl.range(
|
vim.hl.range(
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
-- Tree node representation for DAP variables
|
|
||||||
|
|
||||||
---@class ow.dap.hover.Node
|
---@class ow.dap.hover.Node
|
||||||
---@field item ow.dap.Item The DAP item this node represents
|
---@field item ow.dap.Item
|
||||||
---@field parent ow.dap.hover.Node? Parent node
|
---@field parent ow.dap.hover.Node?
|
||||||
---@field children ow.dap.hover.Node[] Child nodes
|
---@field children ow.dap.hover.Node[]
|
||||||
---@field is_expanded boolean Whether this node is expanded
|
---@field is_expanded boolean
|
||||||
---@field is_last_child boolean Whether this is the last child of its parent
|
---@field is_last_child boolean
|
||||||
local Node = {}
|
local Node = {}
|
||||||
Node.__index = Node
|
Node.__index = Node
|
||||||
|
|
||||||
---Create a new tree node
|
|
||||||
---@param item ow.dap.Item
|
---@param item ow.dap.Item
|
||||||
---@param parent ow.dap.hover.Node?
|
---@param parent ow.dap.hover.Node?
|
||||||
---@return ow.dap.hover.Node
|
---@return ow.dap.hover.Node
|
||||||
@@ -25,23 +22,20 @@ function Node.new(item, parent)
|
|||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
|
|
||||||
---Check if this node represents a container (struct/array)
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Node:is_container()
|
function Node:is_container()
|
||||||
return self.item.variablesReference and self.item.variablesReference > 0
|
return self.item.variablesReference and self.item.variablesReference > 0
|
||||||
or false
|
or false
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the tree prefix for this node (├─, └─, │, etc.)
|
|
||||||
---@return string
|
---@return string
|
||||||
function Node:get_tree_prefix()
|
function Node:get_tree_prefix()
|
||||||
if not self.parent then
|
if not self.parent then
|
||||||
return "" -- Root node has no prefix
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local prefix = ""
|
local prefix = ""
|
||||||
|
|
||||||
-- Walk up the tree to build the prefix
|
|
||||||
local node = self.parent
|
local node = self.parent
|
||||||
while node and node.parent do
|
while node and node.parent do
|
||||||
if node.is_last_child then
|
if node.is_last_child then
|
||||||
@@ -52,7 +46,6 @@ function Node:get_tree_prefix()
|
|||||||
node = node.parent
|
node = node.parent
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add the final branch character
|
|
||||||
if self.is_last_child then
|
if self.is_last_child then
|
||||||
prefix = prefix .. "└─ "
|
prefix = prefix .. "└─ "
|
||||||
else
|
else
|
||||||
@@ -99,8 +92,7 @@ function Node:format_c()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Format this node into the provided content
|
---@param session dap.Session
|
||||||
---@param session dap.Session DAP session for making requests
|
|
||||||
---@param content ow.dap.hover.Content
|
---@param content ow.dap.hover.Content
|
||||||
function Node:format_into(session, content)
|
function Node:format_into(session, content)
|
||||||
if self:is_container() then
|
if self:is_container() then
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
-- Tree-based DAP variable formatter with expand/collapse support
|
|
||||||
local Content = require("ow.dap.hover.content")
|
local Content = require("ow.dap.hover.content")
|
||||||
local Node = require("ow.dap.hover.node")
|
local Node = require("ow.dap.hover.node")
|
||||||
|
|
||||||
@@ -8,7 +7,6 @@ local Node = require("ow.dap.hover.node")
|
|||||||
local Tree = {}
|
local Tree = {}
|
||||||
Tree.__index = Tree
|
Tree.__index = Tree
|
||||||
|
|
||||||
---Create a new tree formatter
|
|
||||||
---@param session dap.Session
|
---@param session dap.Session
|
||||||
---@return ow.dap.hover.Tree
|
---@return ow.dap.hover.Tree
|
||||||
function Tree.new(session)
|
function Tree.new(session)
|
||||||
@@ -20,9 +18,8 @@ function Tree.new(session)
|
|||||||
}, Tree)
|
}, Tree)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Build the tree from a DAP item
|
|
||||||
---@async
|
---@async
|
||||||
---@param item ow.dap.Item Root item to build tree from
|
---@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)
|
||||||
|
|
||||||
@@ -32,10 +29,9 @@ function Tree:build(item)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Load children for a node
|
|
||||||
---@async
|
---@async
|
||||||
---@param node ow.dap.hover.Node
|
---@param node ow.dap.hover.Node
|
||||||
---@return boolean success Whether loading succeeded
|
---@return boolean success
|
||||||
function Tree:load_children(node)
|
function Tree:load_children(node)
|
||||||
if not node:is_container() or #node.children > 0 then
|
if not node:is_container() or #node.children > 0 then
|
||||||
return true -- Already loaded or not a container
|
return true -- Already loaded or not a container
|
||||||
@@ -49,7 +45,6 @@ function Tree:load_children(node)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create child nodes
|
|
||||||
for i, var in ipairs(resp.variables) do
|
for i, var in ipairs(resp.variables) do
|
||||||
local child_item = {
|
local child_item = {
|
||||||
name = var.name,
|
name = var.name,
|
||||||
@@ -62,7 +57,6 @@ function Tree:load_children(node)
|
|||||||
local child = Node.new(child_item, node)
|
local child = Node.new(child_item, node)
|
||||||
child.is_last_child = (i == #resp.variables)
|
child.is_last_child = (i == #resp.variables)
|
||||||
|
|
||||||
-- Format array indices properly
|
|
||||||
if child_item.name:match("^%d+$") then
|
if child_item.name:match("^%d+$") then
|
||||||
child_item.name = "[" .. child_item.name .. "]"
|
child_item.name = "[" .. child_item.name .. "]"
|
||||||
end
|
end
|
||||||
@@ -73,7 +67,6 @@ function Tree:load_children(node)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
---Render the tree to highlighted content
|
|
||||||
---@async
|
---@async
|
||||||
---@return ow.dap.hover.Content
|
---@return ow.dap.hover.Content
|
||||||
function Tree:render()
|
function Tree:render()
|
||||||
@@ -87,15 +80,12 @@ function Tree:render()
|
|||||||
return content
|
return content
|
||||||
end
|
end
|
||||||
|
|
||||||
---Render a single node and its expanded children
|
|
||||||
---@async
|
---@async
|
||||||
---@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)
|
||||||
-- Format this node
|
|
||||||
node:format_into(self.session, content)
|
node:format_into(self.session, content)
|
||||||
|
|
||||||
-- Render expanded children
|
|
||||||
if node.is_expanded then
|
if node.is_expanded then
|
||||||
for _, child in ipairs(node.children) do
|
for _, child in ipairs(node.children) do
|
||||||
content:newline()
|
content:newline()
|
||||||
@@ -145,10 +135,9 @@ function Tree:get_node_at_line(target_line)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Toggle expansion state of node at given line
|
|
||||||
---@async
|
---@async
|
||||||
---@param node ow.dap.hover.Node
|
---@param node ow.dap.hover.Node
|
||||||
---@return boolean success Whether toggle succeeded
|
---@return boolean success
|
||||||
function Tree:toggle_node(node)
|
function Tree:toggle_node(node)
|
||||||
if not node.is_expanded then
|
if not node.is_expanded then
|
||||||
local success = self:load_children(node)
|
local success = self:load_children(node)
|
||||||
@@ -157,7 +146,6 @@ function Tree:toggle_node(node)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Toggle state
|
|
||||||
node.is_expanded = not node.is_expanded
|
node.is_expanded = not node.is_expanded
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user