|
25 | 25 | local function highlight_equal(root, buf)
|
26 | 26 | local marks = {}
|
27 | 27 |
|
28 |
| - ---@param row integer |
29 |
| - ---@param start_col integer |
30 |
| - ---@param end_col integer |
| 28 | + ---@param row { [1]: integer, [2]: integer } |
| 29 | + ---@param col { [1]: integer, [2]: integer } |
31 | 30 | ---@param conceal? string
|
32 | 31 | ---@param hl_group? string
|
33 |
| - local function append(row, start_col, end_col, conceal, hl_group) |
| 32 | + local function append(row, col, conceal, hl_group) |
34 | 33 | table.insert(marks, {
|
35 |
| - conceal = true, |
36 |
| - start_row = row, |
37 |
| - start_col = start_col, |
38 |
| - opts = { end_row = row, end_col = end_col, conceal = conceal, hl_group = hl_group }, |
| 34 | + conceal = row[1] == row[2], |
| 35 | + start_row = row[1], |
| 36 | + start_col = col[1], |
| 37 | + opts = { end_row = row[2], end_col = col[2], conceal = conceal, hl_group = hl_group }, |
39 | 38 | })
|
40 | 39 | end
|
41 | 40 |
|
42 |
| - local start_row = root:range() |
43 | 41 | local text = vim.treesitter.get_node_text(root, buf)
|
44 |
| - for i, line in ipairs(vim.split(text, '\n', { plain = true })) do |
45 |
| - local row = start_row + i - 1 |
46 |
| - ---@type integer|nil |
47 |
| - local position = 1 |
48 |
| - while position ~= nil do |
49 |
| - local start_col, end_col = line:find('(=)=[^=]+=(=)', position) |
50 |
| - if start_col ~= nil and end_col ~= nil then |
51 |
| - -- Translate 1 based index to 0 based index, update position |
52 |
| - start_col, position = start_col - 1, end_col + 1 |
53 |
| - -- Hide first 2 equal signs |
54 |
| - append(row, start_col, start_col + 2, '', nil) |
55 |
| - -- Highlight contents |
56 |
| - append(row, start_col, end_col, nil, 'DiffDelete') |
57 |
| - -- Hide last 2 equal signs |
58 |
| - append(row, end_col - 2, end_col, '', nil) |
59 |
| - else |
60 |
| - position = nil |
61 |
| - end |
| 42 | + local top_row = root:range() |
| 43 | + |
| 44 | + ---@param index integer |
| 45 | + ---@return integer, integer |
| 46 | + local function row_col(index) |
| 47 | + local lines = vim.split(text:sub(1, index), '\n', { plain = true }) |
| 48 | + return top_row + #lines - 1, #lines[#lines] |
| 49 | + end |
| 50 | + |
| 51 | + ---@type integer|nil |
| 52 | + local index = 1 |
| 53 | + while index ~= nil do |
| 54 | + local start_index, end_index = text:find('(=)=[^=]+=(=)', index) |
| 55 | + if start_index ~= nil and end_index ~= nil then |
| 56 | + local start_row, start_col = row_col(start_index - 1) |
| 57 | + local end_row, end_col = row_col(end_index) |
| 58 | + -- Hide first 2 equal signs |
| 59 | + append({ start_row, start_row }, { start_col, start_col + 2 }, '', nil) |
| 60 | + -- Highlight contents |
| 61 | + append({ start_row, end_row }, { start_col, end_col }, nil, 'DiffDelete') |
| 62 | + -- Hide last 2 equal signs |
| 63 | + append({ end_row, end_row }, { end_col - 2, end_col }, '', nil) |
| 64 | + index = end_index + 1 |
| 65 | + else |
| 66 | + index = nil |
62 | 67 | end
|
63 | 68 | end
|
| 69 | + |
64 | 70 | return marks
|
65 | 71 | end
|
66 | 72 |
|
67 |
| ----@param row integer |
68 |
| ----@param start_col integer |
69 |
| ----@param end_col integer |
70 |
| ----@return render.md.MarkInfo |
71 |
| -local function highlight(row, start_col, end_col) |
| 73 | +---@param row { [1]: integer, [2]: integer } |
| 74 | +---@param col { [1]: integer, [2]: integer } |
| 75 | +---@return render.md.MarkInfo[] |
| 76 | +local function highlight_equals(row, col) |
72 | 77 | ---@type render.md.MarkInfo
|
73 |
| - return { |
74 |
| - row = { row, row }, |
75 |
| - col = { start_col, end_col }, |
| 78 | + local highlight = { |
| 79 | + row = row, |
| 80 | + col = col, |
76 | 81 | hl_eol = false,
|
77 | 82 | hl_group = 'DiffDelete',
|
78 | 83 | }
|
| 84 | + return { util.conceal(row[1], col[1], col[1] + 2), highlight, util.conceal(row[2], col[2] - 2, col[2]) } |
79 | 85 | end
|
80 | 86 |
|
81 | 87 | describe('custom_handler.md', function()
|
@@ -142,7 +148,8 @@ describe('custom_handler.md', function()
|
142 | 148 | util.heading(row:get(), 1), -- Heading
|
143 | 149 | util.inline_code(row:increment(2), 0, 8), -- Inline code
|
144 | 150 | { util.conceal(row:increment(2), 0, 1), util.conceal(row:get(), 7, 8) }, -- Backslash escapes
|
145 |
| - { util.conceal(row:increment(2), 5, 7), highlight(row:get(), 5, 25), util.conceal(row:get(), 23, 25) }, -- Highlight equals |
| 151 | + highlight_equals({ row:increment(2), row:get() }, { 5, 25 }), -- Highlight equals 1 |
| 152 | + highlight_equals({ row:increment(), row:increment() }, { 7, 7 }), -- Highlight equals 2 |
146 | 153 | })
|
147 | 154 |
|
148 | 155 | local actual = util.get_actual_marks()
|
|
0 commit comments