Skip to content

Commit 25eb261

Browse files
chore: update custom handler unit test to support multi line
1 parent 5137b5e commit 25eb261

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

Diff for: doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 October 20
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 October 23
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

Diff for: tests/custom_handler_spec.lua

+43-36
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,63 @@ end
2525
local function highlight_equal(root, buf)
2626
local marks = {}
2727

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 }
3130
---@param conceal? string
3231
---@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)
3433
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 },
3938
})
4039
end
4140

42-
local start_row = root:range()
4341
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
6267
end
6368
end
69+
6470
return marks
6571
end
6672

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)
7277
---@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,
7681
hl_eol = false,
7782
hl_group = 'DiffDelete',
7883
}
84+
return { util.conceal(row[1], col[1], col[1] + 2), highlight, util.conceal(row[2], col[2] - 2, col[2]) }
7985
end
8086

8187
describe('custom_handler.md', function()
@@ -142,7 +148,8 @@ describe('custom_handler.md', function()
142148
util.heading(row:get(), 1), -- Heading
143149
util.inline_code(row:increment(2), 0, 8), -- Inline code
144150
{ 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
146153
})
147154

148155
local actual = util.get_actual_marks()

Diff for: tests/data/custom_handler.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
\$1.50 \$3.55
66

77
Some ==highlighted text== line
8+
Across ==multiple
9+
lines== of text

0 commit comments

Comments
 (0)