fix(dap.hover): cleanup
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
-- Highlighted text content builder for DAP tree display
|
||||
---@class ow.dap.hover.content.Capture
|
||||
---@field start_col integer
|
||||
---@field end_col integer
|
||||
@@ -7,21 +6,20 @@
|
||||
---@field priority integer
|
||||
|
||||
---@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)
|
||||
---@field group string
|
||||
---@field start_row integer 0-indexed
|
||||
---@field start_col integer 0-indexed
|
||||
---@field end_row integer 0-indexed
|
||||
---@field end_col integer 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)
|
||||
---@field text string
|
||||
---@field highlights ow.dap.hover.Highlight[]
|
||||
---@field _current_row integer
|
||||
---@field _current_col integer
|
||||
local Content = {}
|
||||
Content.__index = Content
|
||||
|
||||
---Create new highlighted content
|
||||
---@return ow.dap.hover.Content
|
||||
function Content.new()
|
||||
return setmetatable({
|
||||
@@ -37,9 +35,8 @@ function Content:current_line()
|
||||
return self._current_row + 1
|
||||
end
|
||||
|
||||
---Add text with optional highlighting
|
||||
---@param text string Text to add. May not contain line breaks.
|
||||
---@param highlight_group? string Optional highlight group
|
||||
---@param text string May not contain line breaks
|
||||
---@param highlight_group? string
|
||||
function Content:add(text, highlight_group)
|
||||
local start_row = self._current_row
|
||||
local start_col = self._current_col
|
||||
@@ -60,17 +57,14 @@ function Content:add(text, highlight_group)
|
||||
end
|
||||
end
|
||||
|
||||
---Add text with tree-sitter syntax highlighting
|
||||
---@param text string Text to add. May not contain line breaks.
|
||||
---@param lang string Language for tree-sitter highlighting
|
||||
---@param text string May not contain line breaks
|
||||
---@param lang string
|
||||
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
|
||||
self:add(text)
|
||||
|
||||
-- Then apply tree-sitter highlights on top
|
||||
local ok, parser = pcall(vim.treesitter.get_string_parser, text, lang)
|
||||
if not ok or not parser then
|
||||
return
|
||||
@@ -86,19 +80,16 @@ function Content:add_with_treesitter(text, lang)
|
||||
return
|
||||
end
|
||||
|
||||
-- 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_rel, start_col_rel, end_row_rel, end_col_rel =
|
||||
node:range()
|
||||
|
||||
-- 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_row = abs_start_row,
|
||||
@@ -109,23 +100,20 @@ function Content:add_with_treesitter(text, lang)
|
||||
end
|
||||
end
|
||||
|
||||
---Add a newline and reset column tracking
|
||||
function Content:newline()
|
||||
self:add("\n")
|
||||
self._current_col = 0
|
||||
self._current_row = self._current_row + 1
|
||||
end
|
||||
|
||||
---Get the lines as a table
|
||||
---@return string[]
|
||||
function Content:get_lines()
|
||||
return vim.split(self.text, "\n", { trimempty = true })
|
||||
end
|
||||
|
||||
---Apply highlights to a buffer
|
||||
---@param ns_id integer
|
||||
---@param buf integer Buffer handle
|
||||
---@param row_offset integer
|
||||
---@param buf integer
|
||||
---@param row_offset integer 0-indexed
|
||||
function Content:apply_highlights(ns_id, buf, row_offset)
|
||||
for _, highlight in ipairs(self.highlights) do
|
||||
vim.hl.range(
|
||||
|
||||
Reference in New Issue
Block a user