|
| 1 | +---@class render.md.DebugMark |
| 2 | +---@field conceal boolean |
| 3 | +---@field opts vim.api.keyset.set_extmark |
| 4 | +---@field row { [1]: integer, [2]: integer } |
| 5 | +---@field col { [1]: integer, [2]: integer } |
| 6 | +local DebugMark = {} |
| 7 | +DebugMark.__index = DebugMark |
| 8 | + |
| 9 | +---@param mark render.md.Mark |
| 10 | +---@return render.md.DebugMark |
| 11 | +function DebugMark.new(mark) |
| 12 | + local self = setmetatable({}, DebugMark) |
| 13 | + self.conceal, self.opts = mark.conceal, mark.opts |
| 14 | + self.row = { mark.start_row, mark.opts.end_row or mark.start_row } |
| 15 | + self.col = { mark.start_col, mark.opts.end_col or mark.start_col } |
| 16 | + return self |
| 17 | +end |
| 18 | + |
| 19 | +---@return integer[] |
| 20 | +function DebugMark:priorities() |
| 21 | + local row_offset = 0 |
| 22 | + if self.opts.virt_lines ~= nil then |
| 23 | + row_offset = self.opts.virt_lines_above and -0.5 or 0.5 |
| 24 | + end |
| 25 | + local col = self.opts.virt_text_win_col or 0 |
| 26 | + local result = { self.row[1] + row_offset, self.row[2] + row_offset } |
| 27 | + return vim.list_extend(result, { math.max(self.col[1], col), math.max(self.col[2], col) }) |
| 28 | +end |
| 29 | + |
| 30 | +---@return string |
| 31 | +function DebugMark:__tostring() |
| 32 | + ---@param text string |
| 33 | + ---@return string |
| 34 | + local function serialize_text(text) |
| 35 | + local chars = vim.fn.str2list(text) |
| 36 | + if #chars <= 1 then |
| 37 | + return string.format('"%s"', text) |
| 38 | + end |
| 39 | + local first = chars[1] |
| 40 | + for _, char in ipairs(chars) do |
| 41 | + if first ~= char then |
| 42 | + return string.format('"%s"', text) |
| 43 | + end |
| 44 | + end |
| 45 | + return string.format('rep(%s, %d)', vim.fn.nr2char(first), #chars) |
| 46 | + end |
| 47 | + |
| 48 | + ---@param highlight number|string|string[] |
| 49 | + ---@return string |
| 50 | + local function serialize_highlight(highlight) |
| 51 | + if type(highlight) == 'table' then |
| 52 | + highlight = table.concat(highlight, '+') |
| 53 | + end |
| 54 | + local result, _ = highlight:gsub('RenderMarkdown_?', '') |
| 55 | + result, _ = result:gsub('Inverse', 'I') |
| 56 | + return string.format('(%s)', result) |
| 57 | + end |
| 58 | + |
| 59 | + ---@param line { [1]?: string, [2]?: number|string|string[] }[] |
| 60 | + ---@return string[]? |
| 61 | + local function virt_line(line) |
| 62 | + local result = {} |
| 63 | + for _, part in ipairs(line) do |
| 64 | + local serialized, text, highlight = {}, part[1], part[2] |
| 65 | + if text ~= nil then |
| 66 | + table.insert(serialized, serialize_text(text)) |
| 67 | + end |
| 68 | + if highlight ~= nil then |
| 69 | + table.insert(serialized, serialize_highlight(highlight)) |
| 70 | + end |
| 71 | + if #serialized > 0 then |
| 72 | + table.insert(result, table.concat(serialized, '::')) |
| 73 | + end |
| 74 | + end |
| 75 | + return #result > 0 and result or nil |
| 76 | + end |
| 77 | + |
| 78 | + ---@param vals { [1]: integer, [2]: integer } |
| 79 | + ---@return string|integer |
| 80 | + local function collapse(vals) |
| 81 | + return vals[1] == vals[2] and vals[1] or string.format('%d -> %d', vals[1], vals[2]) |
| 82 | + end |
| 83 | + |
| 84 | + local lines = { |
| 85 | + string.rep('=', vim.o.columns - 10), |
| 86 | + string.format('row: %s', collapse(self.row)), |
| 87 | + string.format('column: %s', collapse(self.col)), |
| 88 | + string.format('hide: %s', self.conceal), |
| 89 | + } |
| 90 | + |
| 91 | + ---@param name string |
| 92 | + ---@param value any |
| 93 | + local function append(name, value) |
| 94 | + if type(value) == 'table' then |
| 95 | + value = virt_line(value) |
| 96 | + end |
| 97 | + if value ~= nil then |
| 98 | + if type(value) == 'table' then |
| 99 | + value = table.concat(value, ' + ') |
| 100 | + end |
| 101 | + if type(value) == 'string' and #value == 0 then |
| 102 | + value = vim.inspect(value) |
| 103 | + end |
| 104 | + table.insert(lines, string.format(' %s: %s', name, value)) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + append('conceal', self.opts.conceal) |
| 109 | + append('sign', { { self.opts.sign_text, self.opts.sign_hl_group } }) |
| 110 | + append('virt_text', self.opts.virt_text) |
| 111 | + append('virt_text_pos', self.opts.virt_text_pos) |
| 112 | + append('virt_text_win_col', self.opts.virt_text_win_col) |
| 113 | + append('virt_text_repeat_linebreak', self.opts.virt_text_repeat_linebreak) |
| 114 | + append('virt_line', (self.opts.virt_lines or {})[1]) |
| 115 | + append('virt_line_above', self.opts.virt_lines_above) |
| 116 | + append('hl_group', { { nil, self.opts.hl_group } }) |
| 117 | + append('hl_eol', self.opts.hl_eol) |
| 118 | + append('hl_mode', self.opts.hl_mode) |
| 119 | + return table.concat(lines, '\n') |
| 120 | +end |
| 121 | + |
| 122 | +---@param a render.md.DebugMark |
| 123 | +---@param b render.md.DebugMark |
| 124 | +---@return boolean |
| 125 | +function DebugMark.__lt(a, b) |
| 126 | + local as, bs = a:priorities(), b:priorities() |
| 127 | + for i = 1, math.max(#as, #bs) do |
| 128 | + if as[i] ~= bs[i] then |
| 129 | + return as[i] < bs[i] |
| 130 | + end |
| 131 | + end |
| 132 | + return false |
| 133 | +end |
| 134 | + |
| 135 | +---@class render.md.DebugMarks |
| 136 | +local M = {} |
| 137 | + |
| 138 | +---@param row integer |
| 139 | +---@param marks render.md.Mark[] |
| 140 | +function M.debug(row, marks) |
| 141 | + print('Decorations on row: ' .. row) |
| 142 | + if #marks == 0 then |
| 143 | + print('No decorations found') |
| 144 | + end |
| 145 | + local debug_marks = vim.tbl_map(DebugMark.new, marks) |
| 146 | + table.sort(debug_marks) |
| 147 | + for _, mark in ipairs(debug_marks) do |
| 148 | + print(mark) |
| 149 | + end |
| 150 | +end |
| 151 | + |
| 152 | +return M |
0 commit comments