Skip to content

Commit de6f057

Browse files
chore(testing): add equals highlight to custom handler test
1 parent d723bfd commit de6f057

File tree

5 files changed

+99
-29
lines changed

5 files changed

+99
-29
lines changed

doc/custom-handlers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Each handler must conform to the following interface:
1919
---@field public opts vim.api.keyset.set_extmark
2020

2121
---@class (exact) render.md.Handler
22-
---@field public parse fun(root: TSNode, buf: integer): render.md.Mark[]
2322
---@field public extends? boolean
23+
---@field public parse fun(root: TSNode, buf: integer): render.md.Mark[]
2424
```
2525

2626
The `parse` function parameters are:

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 September 19
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 20
22

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

lua/render-markdown/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ local M = {}
88
---@field public opts vim.api.keyset.set_extmark
99

1010
---@class (exact) render.md.Handler
11-
---@field public parse fun(root: TSNode, buf: integer): render.md.Mark[]
1211
---@field public extends? boolean
12+
---@field public parse fun(root: TSNode, buf: integer): render.md.Mark[]
1313

1414
---@class (exact) render.md.UserLatex
1515
---@field public enabled? boolean

tests/custom_handler_spec.lua

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,92 @@ local util = require('tests.util')
55
---@param root TSNode
66
---@param buf integer
77
---@return render.md.Mark[]
8-
local function parse_conceal_escape(root, buf)
9-
local marks = {}
10-
local query = vim.treesitter.query.parse('markdown_inline', '(backslash_escape) @escape')
8+
local function conceal_escape(root, buf)
9+
local marks, query = {}, vim.treesitter.query.parse('markdown_inline', '(backslash_escape) @escape')
1110
for _, node in query:iter_captures(root, buf) do
1211
local start_row, start_col, end_row, _ = node:range()
13-
---@type render.md.Mark
14-
local mark = {
12+
table.insert(marks, {
1513
conceal = true,
1614
start_row = start_row,
1715
start_col = start_col,
18-
opts = {
19-
end_row = end_row,
20-
end_col = start_col + 1,
21-
conceal = '',
22-
},
23-
}
24-
table.insert(marks, mark)
16+
opts = { end_row = end_row, end_col = start_col + 1, conceal = '' },
17+
})
18+
end
19+
return marks
20+
end
21+
22+
---@param root TSNode
23+
---@param buf integer
24+
---@return render.md.Mark[]
25+
local function highlight_equal(root, buf)
26+
local marks = {}
27+
28+
---@param row integer
29+
---@param start_col integer
30+
---@param end_col integer
31+
---@param conceal? string
32+
---@param hl_group? string
33+
local function append(row, start_col, end_col, conceal, hl_group)
34+
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 },
39+
})
40+
end
41+
42+
local start_row = root:range()
43+
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
62+
end
2563
end
2664
return marks
2765
end
2866

2967
---@param row integer
30-
---@param col integer
68+
---@param start_col integer
69+
---@param end_col integer
3170
---@return render.md.MarkInfo
32-
local function backslash(row, col)
71+
local function conceal(row, start_col, end_col)
3372
---@type render.md.MarkInfo
3473
return {
3574
row = { row, row },
36-
col = { col, col + 1 },
75+
col = { start_col, end_col },
3776
conceal = '',
3877
}
3978
end
4079

80+
---@param row integer
81+
---@param start_col integer
82+
---@param end_col integer
83+
---@return render.md.MarkInfo
84+
local function highlight(row, start_col, end_col)
85+
---@type render.md.MarkInfo
86+
return {
87+
row = { row, row },
88+
col = { start_col, end_col },
89+
hl_eol = false,
90+
hl_group = 'DiffDelete',
91+
}
92+
end
93+
4194
describe('custom_handler.md', function()
4295
it('default', function()
4396
util.setup('tests/data/custom_handler.md')
@@ -53,41 +106,56 @@ describe('custom_handler.md', function()
53106
util.marks_are_equal(expected, actual)
54107
end)
55108

56-
it('custom parse override', function()
109+
it('custom conceal override', function()
57110
util.setup('tests/data/custom_handler.md', {
58111
custom_handlers = {
59-
markdown_inline = {
60-
parse = parse_conceal_escape,
61-
},
112+
markdown_inline = { parse = conceal_escape },
62113
},
63114
})
64115

65116
local expected, row = {}, util.row()
66117
vim.list_extend(expected, {
67118
util.heading(row:get(), 1), -- Heading
68119
{}, -- No inline code
69-
{ backslash(row:increment(4), 0), backslash(row:get(), 7) }, -- Backslash escapes
120+
{ conceal(row:increment(4), 0, 1), conceal(row:get(), 7, 8) }, -- Backslash escapes
121+
})
122+
123+
local actual = util.get_actual_marks()
124+
util.marks_are_equal(expected, actual)
125+
end)
126+
127+
it('custom conceal extend', function()
128+
util.setup('tests/data/custom_handler.md', {
129+
custom_handlers = {
130+
markdown_inline = { extends = true, parse = conceal_escape },
131+
},
132+
})
133+
134+
local expected, row = {}, util.row()
135+
vim.list_extend(expected, {
136+
util.heading(row:get(), 1), -- Heading
137+
util.inline_code(row:increment(2), 0, 8), -- Inline code
138+
{ conceal(row:increment(2), 0, 1), conceal(row:get(), 7, 8) }, -- Backslash escapes
70139
})
71140

72141
local actual = util.get_actual_marks()
73142
util.marks_are_equal(expected, actual)
74143
end)
75144

76-
it('custom parse extend', function()
145+
it('custom highlight extend', function()
77146
util.setup('tests/data/custom_handler.md', {
78147
custom_handlers = {
79-
markdown_inline = {
80-
parse = parse_conceal_escape,
81-
extends = true,
82-
},
148+
markdown = { extends = true, parse = highlight_equal },
149+
markdown_inline = { extends = true, parse = conceal_escape },
83150
},
84151
})
85152

86153
local expected, row = {}, util.row()
87154
vim.list_extend(expected, {
88155
util.heading(row:get(), 1), -- Heading
89156
util.inline_code(row:increment(2), 0, 8), -- Inline code
90-
{ backslash(row:increment(2), 0), backslash(row:get(), 7) }, -- Backslash escapes
157+
{ conceal(row:increment(2), 0, 1), conceal(row:get(), 7, 8) }, -- Backslash escapes
158+
{ conceal(row:increment(2), 5, 7), highlight(row:get(), 5, 25), conceal(row:get(), 23, 25) }, -- Highlight equals
91159
})
92160

93161
local actual = util.get_actual_marks()

tests/data/custom_handler.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
`Inline` code
44

55
\$1.50 \$3.55
6+
7+
Some ==highlighted text== line

0 commit comments

Comments
 (0)