@@ -5,39 +5,92 @@ local util = require('tests.util')
5
5
--- @param root TSNode
6
6
--- @param buf integer
7
7
--- @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' )
11
10
for _ , node in query :iter_captures (root , buf ) do
12
11
local start_row , start_col , end_row , _ = node :range ()
13
- --- @type render.md.Mark
14
- local mark = {
12
+ table.insert (marks , {
15
13
conceal = true ,
16
14
start_row = start_row ,
17
15
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
25
63
end
26
64
return marks
27
65
end
28
66
29
67
--- @param row integer
30
- --- @param col integer
68
+ --- @param start_col integer
69
+ --- @param end_col integer
31
70
--- @return render.md.MarkInfo
32
- local function backslash (row , col )
71
+ local function conceal (row , start_col , end_col )
33
72
--- @type render.md.MarkInfo
34
73
return {
35
74
row = { row , row },
36
- col = { col , col + 1 },
75
+ col = { start_col , end_col },
37
76
conceal = ' ' ,
38
77
}
39
78
end
40
79
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
+
41
94
describe (' custom_handler.md' , function ()
42
95
it (' default' , function ()
43
96
util .setup (' tests/data/custom_handler.md' )
@@ -53,41 +106,56 @@ describe('custom_handler.md', function()
53
106
util .marks_are_equal (expected , actual )
54
107
end )
55
108
56
- it (' custom parse override' , function ()
109
+ it (' custom conceal override' , function ()
57
110
util .setup (' tests/data/custom_handler.md' , {
58
111
custom_handlers = {
59
- markdown_inline = {
60
- parse = parse_conceal_escape ,
61
- },
112
+ markdown_inline = { parse = conceal_escape },
62
113
},
63
114
})
64
115
65
116
local expected , row = {}, util .row ()
66
117
vim .list_extend (expected , {
67
118
util .heading (row :get (), 1 ), -- Heading
68
119
{}, -- 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
70
139
})
71
140
72
141
local actual = util .get_actual_marks ()
73
142
util .marks_are_equal (expected , actual )
74
143
end )
75
144
76
- it (' custom parse extend' , function ()
145
+ it (' custom highlight extend' , function ()
77
146
util .setup (' tests/data/custom_handler.md' , {
78
147
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 },
83
150
},
84
151
})
85
152
86
153
local expected , row = {}, util .row ()
87
154
vim .list_extend (expected , {
88
155
util .heading (row :get (), 1 ), -- Heading
89
156
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
91
159
})
92
160
93
161
local actual = util .get_actual_marks ()
0 commit comments