refactor(dap): simplify hover tree rendering architecture

- Add row/column tracking to Content for multi-line highlight support
- Switch from Node:format() returning Content to Node:format_into(content)
- Remove complex highlight offset calculations and manual text merging
- Use content:current_line() instead of tracking line_number in nodes
- Fix pointer child formatting to use item.name directly
- Clean up unused imports and simplify render_node method
This commit is contained in:
2025-09-26 19:29:29 +02:00
parent cff07f8317
commit acf6fffb2f
4 changed files with 57 additions and 91 deletions
+36 -42
View File
@@ -1,5 +1,4 @@
-- Highlighted text content builder for DAP tree display
local log = require("ow.log")
---@class ow.dap.hover.content.Capture
---@field start_col integer
---@field end_col integer
@@ -9,12 +8,15 @@ local log = require("ow.log")
---@class ow.dap.hover.Highlight
---@field group string Highlight group name
---@field start_row integer Start line (0-indexed)
---@field start_col integer Start column (0-indexed)
---@field end_row integer End line (0-indexed)
---@field end_col integer End column (0-indexed)
---@class ow.dap.hover.Content
---@field text string The complete text content
---@field highlights ow.dap.hover.Highlight[] List of highlights to apply
---@field _current_row integer Current line position (for building)
---@field _current_col integer Current column position (for building)
local Content = {}
Content.__index = Content
@@ -25,15 +27,23 @@ function Content.new()
return setmetatable({
text = "",
highlights = {},
_current_row = 0,
_current_col = 0,
}, Content)
end
---@return integer
function Content:current_line()
return self._current_row + 1
end
---Add text with optional highlighting
---@param text string Text to add
---@param text string Text to add. May not contain line breaks.
---@param highlight_group? string Optional highlight group
function Content:add(text, highlight_group)
local start_row = self._current_row
local start_col = self._current_col
local end_row = self._current_row
local end_col = start_col + #text
self.text = self.text .. text
@@ -42,16 +52,19 @@ function Content:add(text, highlight_group)
if highlight_group then
table.insert(self.highlights, {
group = highlight_group,
start_row = start_row,
start_col = start_col,
end_row = end_row,
end_col = end_col,
})
end
end
---Add text with tree-sitter syntax highlighting
---@param text string The text to highlight
---@param lang string Language for tree-sitter
---@param text string Text to add. May not contain line breaks.
---@param lang string Language for tree-sitter highlighting
function Content:add_with_treesitter(text, lang)
local start_row = self._current_row
local start_col = self._current_col
-- First, just add the text normally
@@ -76,22 +89,23 @@ function Content:add_with_treesitter(text, lang)
-- Add highlights for all captures (overlapping is fine)
for id, node in query:iter_captures(tree:root(), text, 0, -1) do
local capture_name = query.captures[id]
local start_row, start_col_rel, end_row, end_col_rel = node:range()
local start_row_rel, start_col_rel, end_row_rel, end_col_rel =
node:range()
-- TODO: keep track of text as lines instead, so we can handle multiline
-- highlights
if start_row == end_row then -- Single line only
-- Convert to absolute column positions
local abs_start_col = start_col + start_col_rel
local abs_end_col = start_col + end_col_rel
-- Convert to absolute positions
local abs_start_row = start_row + start_row_rel
local abs_end_row = start_row + end_row_rel
local abs_start_col = start_col + start_col_rel
local abs_end_col = start_col + end_col_rel
-- Add the highlight
table.insert(self.highlights, {
group = "@" .. capture_name,
start_col = abs_start_col,
end_col = abs_end_col,
})
end
-- Add the highlight
table.insert(self.highlights, {
group = "@" .. capture_name,
start_row = abs_start_row,
start_col = abs_start_col,
end_row = abs_end_row,
end_col = abs_end_col,
})
end
end
@@ -99,6 +113,7 @@ end
function Content:newline()
self:add("\n")
self._current_col = 0
self._current_row = self._current_row + 1
end
---Get the lines as a table
@@ -110,35 +125,14 @@ end
---Apply highlights to a buffer
---@param ns_id integer
---@param buf integer Buffer handle
---@param line_offset? integer Line offset to apply highlights at (default 0)
function Content:apply_highlights(ns_id, buf, line_offset)
line_offset = line_offset or 0
local lines = self:get_lines()
local current_line = 0
local line_start_col = 0
function Content:apply_highlights(ns_id, buf)
for _, highlight in ipairs(self.highlights) do
-- Find which line this highlight belongs to
while
current_line < #lines - 1
and highlight.start_col
>= line_start_col + #lines[current_line + 1] + 1
do
line_start_col = line_start_col + #lines[current_line + 1] + 1 -- +1 for newline
current_line = current_line + 1
end
-- Calculate column positions relative to line start
local start_col = highlight.start_col - line_start_col
local end_col = highlight.end_col - line_start_col
-- Apply highlight
vim.hl.range(
buf,
ns_id,
highlight.group,
{ line_offset + current_line, start_col },
{ line_offset + current_line, end_col }
{ highlight.start_row, highlight.start_col },
{ highlight.end_row, highlight.end_col }
)
end
end