@@ -4,37 +4,28 @@ local Str = require('render-markdown.lib.str')
4
4
5
5
--- @class render.md.bullet.Data
6
6
--- @field marker render.md.Node
7
- --- @field icons render.md.bullet.Text
8
7
--- @field spaces integer
9
8
--- @field checkbox ? render.md.checkbox.custom.Config
10
9
11
10
--- @class render.md.render.Bullet : render.md.Render
12
- --- @field private info render.md.bullet.Config
13
11
--- @field private data render.md.bullet.Data
14
12
local Render = setmetatable ({}, Base )
15
13
Render .__index = Render
16
14
17
15
--- @return boolean
18
16
function Render :setup ()
19
- self .info = self .config .bullet
20
-
21
17
local marker = self .node :child_at (0 )
22
18
if marker == nil then
23
19
return false
24
20
end
25
-
26
- local ordered_types = { ' list_marker_dot' , ' list_marker_parenthesis' }
27
- local ordered = vim .tbl_contains (ordered_types , marker .type )
28
21
self .data = {
29
22
marker = marker ,
30
- icons = ordered and self .info .ordered_icons or self .info .icons ,
31
23
-- List markers from tree-sitter should have leading spaces removed, however there are edge
32
24
-- cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
33
25
-- As a result we account for leading spaces here, can remove if this gets fixed upstream
34
26
spaces = Str .spaces (' start' , marker .text ),
35
27
checkbox = self .context :get_checkbox (self .node .start_row ),
36
28
}
37
-
38
29
return true
39
30
end
40
31
@@ -47,23 +38,30 @@ function Render:render()
47
38
self :highlight_scope (' check_scope' , scope_highlight )
48
39
end
49
40
else
50
- if self .context :skip (self .info ) then
41
+ local info = self .config .bullet
42
+ if self .context :skip (info ) then
51
43
return
52
44
end
45
+
46
+ local ordered_types = { ' list_marker_dot' , ' list_marker_parenthesis' }
47
+ local ordered = vim .tbl_contains (ordered_types , self .data .marker .type )
48
+ local icons = ordered and info .ordered_icons or info .icons
49
+
53
50
local level , root = self .node :level_in_section (' list' )
54
51
--- @type render.md.bullet.Context
55
52
local ctx = {
56
53
level = level ,
57
54
index = self .node :sibling_count (' list_item' ),
58
55
value = self .data .marker .text ,
59
56
}
60
- local icon = self :get_text (ctx , self .data .icons )
61
- local highlight = self :get_text (ctx , self .info .highlight )
62
- local scope_highlight = self :get_text (ctx , self .info .scope_highlight )
63
- local left_pad = self :get_int (ctx , self .info .left_pad )
64
- local right_pad = self :get_int (ctx , self .info .right_pad )
65
- self :add_icon (icon , highlight )
66
- self :add_padding (left_pad , right_pad , root )
57
+
58
+ local icon = self :get_text (icons , ctx )
59
+ local highlight = self :get_text (info .highlight , ctx )
60
+ local scope_highlight = self :get_text (info .scope_highlight , ctx )
61
+ local left_pad = self :get_int (info .left_pad , ctx )
62
+ local right_pad = self :get_int (info .right_pad , ctx )
63
+ self :icon (icon , highlight )
64
+ self :padding (root , left_pad , right_pad )
67
65
self :highlight_scope (true , scope_highlight )
68
66
end
69
67
end
@@ -74,16 +72,9 @@ function Render:has_checkbox()
74
72
if self .context :skip (self .config .checkbox ) then
75
73
return false
76
74
end
77
- if self .data .checkbox ~= nil then
78
- return true
79
- end
80
- if self .data .marker :sibling (' task_list_marker_unchecked' ) ~= nil then
81
- return true
82
- end
83
- if self .data .marker :sibling (' task_list_marker_checked' ) ~= nil then
84
- return true
85
- end
86
- return false
75
+ return self .data .checkbox ~= nil
76
+ or self .data .marker :sibling (' task_list_marker_unchecked' ) ~= nil
77
+ or self .data .marker :sibling (' task_list_marker_checked' ) ~= nil
87
78
end
88
79
89
80
--- @private
@@ -100,10 +91,10 @@ function Render:highlight_scope(element, highlight)
100
91
end
101
92
102
93
--- @private
103
- --- @param ctx render.md.bullet.Context
104
94
--- @param values render.md.bullet.Text
95
+ --- @param ctx render.md.bullet.Context
105
96
--- @return string ?
106
- function Render :get_text (ctx , values )
97
+ function Render :get_text (values , ctx )
107
98
if type (values ) == ' function' then
108
99
return values (ctx )
109
100
elseif type (values ) == ' string' then
@@ -119,10 +110,10 @@ function Render:get_text(ctx, values)
119
110
end
120
111
121
112
--- @private
122
- --- @param ctx render.md.bullet.Context
123
113
--- @param value render.md.bullet.Int
114
+ --- @param ctx render.md.bullet.Context
124
115
--- @return integer
125
- function Render :get_int (ctx , value )
116
+ function Render :get_int (value , ctx )
126
117
if type (value ) == ' function' then
127
118
return value (ctx )
128
119
else
133
124
--- @private
134
125
--- @param icon string ?
135
126
--- @param highlight string ?
136
- function Render :add_icon (icon , highlight )
127
+ function Render :icon (icon , highlight )
137
128
if icon == nil or highlight == nil then
138
129
return
139
130
end
@@ -147,56 +138,52 @@ function Render:add_icon(icon, highlight)
147
138
end
148
139
149
140
--- @private
141
+ --- @param root ? render.md.Node
150
142
--- @param left_pad integer
151
143
--- @param right_pad integer
152
- --- @param root ? render.md.Node
153
- function Render :add_padding (left_pad , right_pad , root )
144
+ function Render :padding (root , left_pad , right_pad )
154
145
if left_pad <= 0 and right_pad <= 0 then
155
146
return
156
147
end
157
148
local start_row , end_row = self .node .start_row , self :end_row (root )
149
+ local left_line = self :append ({}, left_pad )
150
+ local right_line = self :append ({}, right_pad )
158
151
for row = start_row , end_row - 1 do
159
152
local left = root ~= nil and root .start_col or self .node .start_col
160
153
local right = row == start_row and self .data .marker .end_col - 1 or left
161
- self :side_padding (row , left , left_pad )
162
- self :side_padding (row , right , right_pad )
154
+ if # left_line > 0 then
155
+ self .marks :add (false , row , left , {
156
+ priority = 0 ,
157
+ virt_text = left_line ,
158
+ virt_text_pos = ' inline' ,
159
+ })
160
+ end
161
+ if # right_line > 0 then
162
+ self .marks :add (false , row , right , {
163
+ priority = 0 ,
164
+ virt_text = right_line ,
165
+ virt_text_pos = ' inline' ,
166
+ })
167
+ end
163
168
end
164
169
end
165
170
166
171
--- @private
167
172
--- @param root ? render.md.Node
168
173
--- @return integer
169
174
function Render :end_row (root )
170
- local next_list = self .node :child (' list' )
171
- if next_list ~= nil then
172
- return next_list .start_row
175
+ local sub_list = self .node :child (' list' )
176
+ if sub_list ~= nil then
177
+ return sub_list .start_row
178
+ elseif root == nil then
179
+ return self .node .end_row
173
180
else
181
+ -- on the last item of the root list ignore the last line if empty
174
182
local row = self .node .end_row
175
- -- On the last item of the root list ignore the last line if it is empty
176
- if
177
- root ~= nil
178
- and root .end_row == row
179
- and Str .width (root :line (' last' , 0 )) == 0
180
- then
181
- return row - 1
182
- else
183
- return row
184
- end
185
- end
186
- end
187
-
188
- --- @private
189
- --- @param row integer
190
- --- @param col integer
191
- --- @param amount integer
192
- function Render :side_padding (row , col , amount )
193
- local line = self :append ({}, amount )
194
- if # line > 0 then
195
- self .marks :add (false , row , col , {
196
- priority = 0 ,
197
- virt_text = line ,
198
- virt_text_pos = ' inline' ,
199
- })
183
+ local _ , line = root :line (' last' , 0 )
184
+ local ignore_last = root .end_row == row and Str .width (line ) == 0
185
+ local offset = ignore_last and 1 or 0
186
+ return row - offset
200
187
end
201
188
end
202
189
0 commit comments