@@ -3,6 +3,8 @@ local List = require('render-markdown.lib.list')
33local Str = require (' render-markdown.lib.str' )
44
55--- @class render.md.data.ListMarker
6+ --- @field marker render.md.Node
7+ --- @field ordered boolean
68--- @field spaces integer
79--- @field checkbox ? render.md.CustomCheckbox
810
@@ -16,55 +18,63 @@ Render.__index = Render
1618function Render :setup ()
1719 self .bullet = self .config .bullet
1820
21+ local marker = self .node :child_at (0 )
22+ if marker == nil then
23+ return false
24+ end
25+
1926 self .data = {
27+ marker = marker ,
28+ ordered = vim .tbl_contains ({ ' list_marker_dot' , ' list_marker_parenthesis' }, marker .type ),
2029 -- List markers from tree-sitter should have leading spaces removed, however there are edge
2130 -- cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
2231 -- As a result we account for leading spaces here, can remove if this gets fixed upstream
23- spaces = Str .spaces (' start' , self . node .text ),
32+ spaces = Str .spaces (' start' , marker .text ),
2433 checkbox = self .context :get_checkbox (self .node ),
2534 }
2635
2736 return true
2837end
2938
3039function Render :render ()
31- if self :sibling_checkbox () then
40+ if self :has_checkbox () then
3241 -- Hide the list marker for checkboxes rather than replacing with a bullet point
3342 self :hide_marker ()
3443 self :highlight_scope ()
3544 else
3645 if not self .bullet .enabled then
3746 return
3847 end
39- local level , root_list = self .node :level_in_section (' list' )
48+ local level , root = self .node :level_in_section (' list' )
4049 self :icon (level )
41- self :padding (root_list )
50+ self :padding (root )
4251 end
4352end
4453
4554--- @private
4655--- @return boolean
47- function Render :sibling_checkbox ()
56+ function Render :has_checkbox ()
4857 if not self .config .checkbox .enabled then
4958 return false
5059 end
5160 if self .data .checkbox ~= nil then
5261 return true
5362 end
54- if self .node :sibling (' task_list_marker_unchecked' ) ~= nil then
63+ if self .data . marker :sibling (' task_list_marker_unchecked' ) ~= nil then
5564 return true
5665 end
57- if self .node :sibling (' task_list_marker_checked' ) ~= nil then
66+ if self .data . marker :sibling (' task_list_marker_checked' ) ~= nil then
5867 return true
5968 end
6069 return false
6170end
6271
6372--- @private
6473function Render :hide_marker ()
65- self .marks :add (' check_icon' , self .node .start_row , self .node .start_col + self .data .spaces , {
66- end_row = self .node .end_row ,
67- end_col = self .node .end_col ,
74+ local node = self .data .marker
75+ self .marks :add (' check_icon' , node .start_row , node .start_col + self .data .spaces , {
76+ end_row = node .end_row ,
77+ end_col = node .end_col ,
6878 conceal = ' ' ,
6979 })
7080end
@@ -74,54 +84,48 @@ function Render:highlight_scope()
7484 if self .data .checkbox == nil then
7585 return
7686 end
77- self :checkbox_scope (self .data .checkbox .scope_highlight )
87+ self :checkbox_scope (self .node : child ( ' paragraph ' ), self . data .checkbox .scope_highlight )
7888end
7989
8090--- @private
8191--- @param level integer
8292function Render :icon (level )
83- local icon = List .cycle (self .bullet .icons , level )
93+ local icons = self .data .ordered and self .bullet .ordered_icons or self .bullet .icons
94+ local icon = List .cycle (icons , level )
8495 if type (icon ) == ' table' then
85- local item = self .node :parent (' list_item' )
86- if item == nil then
87- return
88- end
89- icon = List .clamp (icon , item :sibling_count (' list_item' ))
96+ icon = List .clamp (icon , self .node :sibling_count (' list_item' ))
9097 end
9198 if icon == nil then
9299 return
93100 end
101+ local node = self .data .marker
94102 local text = Str .pad (self .data .spaces ) .. icon
95103 local position , conceal = ' overlay' , nil
96- if Str .width (text ) > Str .width (self . node .text ) then
104+ if Str .width (text ) > Str .width (node .text ) then
97105 position , conceal = ' inline' , ' '
98106 end
99- self .marks :add (' bullet' , self . node .start_row , self . node .start_col , {
100- end_row = self . node .end_row ,
101- end_col = self . node .end_col ,
107+ self .marks :add (' bullet' , node .start_row , node .start_col , {
108+ end_row = node .end_row ,
109+ end_col = node .end_col ,
102110 virt_text = { { text , self .bullet .highlight } },
103111 virt_text_pos = position ,
104112 conceal = conceal ,
105113 })
106114end
107115
108116--- @private
109- --- @param root_list ? render.md.Node
110- function Render :padding (root_list )
117+ --- @param root ? render.md.Node
118+ function Render :padding (root )
111119 if self .bullet .left_pad <= 0 and self .bullet .right_pad <= 0 then
112120 return
113121 end
114- local list_item = self .node :parent (' list_item' )
115- if list_item == nil then
116- return
117- end
118- local left_col = root_list ~= nil and root_list .start_col or list_item .start_col
122+ local left_col = root ~= nil and root .start_col or self .node .start_col
119123
120- local next_list = list_item :child (' list' )
121- local end_row = next_list ~= nil and next_list .start_row or list_item .end_row
124+ local next_list = self . node :child (' list' )
125+ local end_row = next_list ~= nil and next_list .start_row or self . node .end_row
122126
123- for row = list_item .start_row , end_row - 1 do
124- local right_col = row == list_item . start_row and self .node .end_col - 1 or left_col
127+ for row = self . node .start_row , end_row - 1 do
128+ local right_col = row == self . node . start_row and self .data . marker .end_col - 1 or left_col
125129 self :padding_mark (row , left_col , self .bullet .left_pad )
126130 self :padding_mark (row , right_col , self .bullet .right_pad )
127131 end
0 commit comments