|
40 | 40 | ---@field virt_lines_above? boolean
|
41 | 41 | ---@field sign_text? string
|
42 | 42 | ---@field sign_hl_group? string
|
| 43 | +local MarkInfo = {} |
| 44 | +MarkInfo.__index = MarkInfo |
| 45 | + |
| 46 | +---@param row integer |
| 47 | +---@param col integer |
| 48 | +---@param details vim.api.keyset.extmark_details |
| 49 | +---@return render.md.MarkInfo |
| 50 | +function MarkInfo.new(row, col, details) |
| 51 | + local self = setmetatable({}, MarkInfo) |
| 52 | + self.row = { row, details.end_row } |
| 53 | + self.col = { col, details.end_col } |
| 54 | + self.hl_eol = details.hl_eol |
| 55 | + self.hl_group = details.hl_group |
| 56 | + ---@diagnostic disable-next-line: assign-type-mismatch |
| 57 | + self.conceal = details.conceal |
| 58 | + self.virt_text = details.virt_text |
| 59 | + self.virt_text_pos = details.virt_text_pos |
| 60 | + self.virt_text_win_col = details.virt_text_win_col |
| 61 | + self.virt_lines = details.virt_lines |
| 62 | + self.virt_lines_above = details.virt_lines_above |
| 63 | + self.sign_text = details.sign_text |
| 64 | + self.sign_hl_group = details.sign_hl_group |
| 65 | + return self |
| 66 | +end |
| 67 | + |
| 68 | +---@param a render.md.MarkInfo |
| 69 | +---@param b render.md.MarkInfo |
| 70 | +---@return boolean |
| 71 | +function MarkInfo.__lt(a, b) |
| 72 | + ---@param getter fun(mark: render.md.MarkInfo): number[] |
| 73 | + ---@return boolean? |
| 74 | + local function compare_ints(getter) |
| 75 | + local as, bs = getter(a), getter(b) |
| 76 | + for i = 1, math.max(#as, #bs) do |
| 77 | + local av, bv = as[math.min(i, #as)], bs[math.min(i, #bs)] |
| 78 | + if av ~= bv then |
| 79 | + return av < bv |
| 80 | + end |
| 81 | + end |
| 82 | + return nil |
| 83 | + end |
| 84 | + |
| 85 | + local row_comp = compare_ints(function(mark) |
| 86 | + if mark.virt_lines == nil then |
| 87 | + return mark.row |
| 88 | + end |
| 89 | + local offset = mark.virt_lines_above and -0.5 or 0.5 |
| 90 | + return vim.iter(mark.row) |
| 91 | + :map(function(row) |
| 92 | + return row + offset |
| 93 | + end) |
| 94 | + :totable() |
| 95 | + end) |
| 96 | + if row_comp ~= nil then |
| 97 | + return row_comp |
| 98 | + end |
| 99 | + |
| 100 | + local col_comp = compare_ints(function(mark) |
| 101 | + if mark.virt_text_win_col == nil then |
| 102 | + return mark.col |
| 103 | + else |
| 104 | + return { mark.virt_text_win_col } |
| 105 | + end |
| 106 | + end) |
| 107 | + if col_comp ~= nil then |
| 108 | + return col_comp |
| 109 | + end |
| 110 | + |
| 111 | + -- Higher priority for inline text |
| 112 | + local a_inline, b_inline = a.virt_text_pos == 'inline', b.virt_text_pos == 'inline' |
| 113 | + if a_inline ~= b_inline then |
| 114 | + return a_inline |
| 115 | + end |
| 116 | + |
| 117 | + -- Lower priority for signs |
| 118 | + local a_sign, b_sign = a.sign_text ~= nil, b.sign_text ~= nil |
| 119 | + if a_sign ~= b_sign then |
| 120 | + return not a_sign |
| 121 | + end |
| 122 | + |
| 123 | + return false |
| 124 | +end |
43 | 125 |
|
44 | 126 | ---@class render.md.test.Util
|
45 | 127 | local M = {}
|
@@ -330,81 +412,9 @@ function M.get_actual_marks()
|
330 | 412 | local marks = vim.api.nvim_buf_get_extmarks(0, ui.namespace, 0, -1, { details = true })
|
331 | 413 | for _, mark in ipairs(marks) do
|
332 | 414 | local _, row, col, details = unpack(mark)
|
333 |
| - ---@type render.md.MarkInfo |
334 |
| - local mark_info = { |
335 |
| - row = { row, details.end_row }, |
336 |
| - col = { col, details.end_col }, |
337 |
| - hl_eol = details.hl_eol, |
338 |
| - hl_group = details.hl_group, |
339 |
| - conceal = details.conceal, |
340 |
| - virt_text = details.virt_text, |
341 |
| - virt_text_pos = details.virt_text_pos, |
342 |
| - virt_text_win_col = details.virt_text_win_col, |
343 |
| - virt_lines = details.virt_lines, |
344 |
| - virt_lines_above = details.virt_lines_above, |
345 |
| - sign_text = details.sign_text, |
346 |
| - sign_hl_group = details.sign_hl_group, |
347 |
| - } |
348 |
| - table.insert(actual, mark_info) |
349 |
| - end |
350 |
| - |
351 |
| - ---@param a render.md.MarkInfo |
352 |
| - ---@param b render.md.MarkInfo |
353 |
| - ---@param getter fun(mark: render.md.MarkInfo): number[] |
354 |
| - ---@return boolean? |
355 |
| - local function compare_ints(a, b, getter) |
356 |
| - local as, bs = getter(a), getter(b) |
357 |
| - for i = 1, math.max(#as, #bs) do |
358 |
| - local av, bv = as[math.min(i, #as)], bs[math.min(i, #bs)] |
359 |
| - if av ~= bv then |
360 |
| - return av < bv |
361 |
| - end |
362 |
| - end |
363 |
| - return nil |
| 415 | + table.insert(actual, MarkInfo.new(row, col, details)) |
364 | 416 | end
|
365 |
| - |
366 |
| - table.sort(actual, function(a, b) |
367 |
| - local row_comp = compare_ints(a, b, function(mark) |
368 |
| - if mark.virt_lines == nil then |
369 |
| - return mark.row |
370 |
| - end |
371 |
| - local offset = mark.virt_lines_above and -0.5 or 0.5 |
372 |
| - return vim.iter(mark.row) |
373 |
| - :map(function(row) |
374 |
| - return row + offset |
375 |
| - end) |
376 |
| - :totable() |
377 |
| - end) |
378 |
| - if row_comp ~= nil then |
379 |
| - return row_comp |
380 |
| - end |
381 |
| - |
382 |
| - local col_comp = compare_ints(a, b, function(mark) |
383 |
| - if mark.virt_text_win_col == nil then |
384 |
| - return mark.col |
385 |
| - else |
386 |
| - return { mark.virt_text_win_col } |
387 |
| - end |
388 |
| - end) |
389 |
| - if col_comp ~= nil then |
390 |
| - return col_comp |
391 |
| - end |
392 |
| - |
393 |
| - -- Higher priority for inline text |
394 |
| - local a_inline, b_inline = a.virt_text_pos == 'inline', b.virt_text_pos == 'inline' |
395 |
| - if a_inline ~= b_inline then |
396 |
| - return a_inline |
397 |
| - end |
398 |
| - |
399 |
| - -- Lower priority for signs |
400 |
| - local a_sign, b_sign = a.sign_text ~= nil, b.sign_text ~= nil |
401 |
| - if a_sign ~= b_sign then |
402 |
| - return not a_sign |
403 |
| - end |
404 |
| - |
405 |
| - return false |
406 |
| - end) |
407 |
| - |
| 417 | + table.sort(actual) |
408 | 418 | return actual
|
409 | 419 | end
|
410 | 420 |
|
|
0 commit comments