@@ -4,37 +4,28 @@ local Str = require('render-markdown.lib.str')
44
55--- @class render.md.bullet.Data
66--- @field marker render.md.Node
7- --- @field icons render.md.bullet.Text
87--- @field spaces integer
98--- @field checkbox ? render.md.checkbox.custom.Config
109
1110--- @class render.md.render.Bullet : render.md.Render
12- --- @field private info render.md.bullet.Config
1311--- @field private data render.md.bullet.Data
1412local Render = setmetatable ({}, Base )
1513Render .__index = Render
1614
1715--- @return boolean
1816function Render :setup ()
19- self .info = self .config .bullet
20-
2117 local marker = self .node :child_at (0 )
2218 if marker == nil then
2319 return false
2420 end
25-
26- local ordered_types = { ' list_marker_dot' , ' list_marker_parenthesis' }
27- local ordered = vim .tbl_contains (ordered_types , marker .type )
2821 self .data = {
2922 marker = marker ,
30- icons = ordered and self .info .ordered_icons or self .info .icons ,
3123 -- List markers from tree-sitter should have leading spaces removed, however there are edge
3224 -- cases in the parser: https://github.com/tree-sitter-grammars/tree-sitter-markdown/issues/127
3325 -- As a result we account for leading spaces here, can remove if this gets fixed upstream
3426 spaces = Str .spaces (' start' , marker .text ),
3527 checkbox = self .context :get_checkbox (self .node .start_row ),
3628 }
37-
3829 return true
3930end
4031
@@ -47,23 +38,30 @@ function Render:render()
4738 self :highlight_scope (' check_scope' , scope_highlight )
4839 end
4940 else
50- if self .context :skip (self .info ) then
41+ local info = self .config .bullet
42+ if self .context :skip (info ) then
5143 return
5244 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+
5350 local level , root = self .node :level_in_section (' list' )
5451 --- @type render.md.bullet.Context
5552 local ctx = {
5653 level = level ,
5754 index = self .node :sibling_count (' list_item' ),
5855 value = self .data .marker .text ,
5956 }
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 )
6765 self :highlight_scope (true , scope_highlight )
6866 end
6967end
@@ -74,16 +72,9 @@ function Render:has_checkbox()
7472 if self .context :skip (self .config .checkbox ) then
7573 return false
7674 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
8778end
8879
8980--- @private
@@ -100,10 +91,10 @@ function Render:highlight_scope(element, highlight)
10091end
10192
10293--- @private
103- --- @param ctx render.md.bullet.Context
10494--- @param values render.md.bullet.Text
95+ --- @param ctx render.md.bullet.Context
10596--- @return string ?
106- function Render :get_text (ctx , values )
97+ function Render :get_text (values , ctx )
10798 if type (values ) == ' function' then
10899 return values (ctx )
109100 elseif type (values ) == ' string' then
@@ -119,10 +110,10 @@ function Render:get_text(ctx, values)
119110end
120111
121112--- @private
122- --- @param ctx render.md.bullet.Context
123113--- @param value render.md.bullet.Int
114+ --- @param ctx render.md.bullet.Context
124115--- @return integer
125- function Render :get_int (ctx , value )
116+ function Render :get_int (value , ctx )
126117 if type (value ) == ' function' then
127118 return value (ctx )
128119 else
133124--- @private
134125--- @param icon string ?
135126--- @param highlight string ?
136- function Render :add_icon (icon , highlight )
127+ function Render :icon (icon , highlight )
137128 if icon == nil or highlight == nil then
138129 return
139130 end
@@ -147,56 +138,52 @@ function Render:add_icon(icon, highlight)
147138end
148139
149140--- @private
141+ --- @param root ? render.md.Node
150142--- @param left_pad integer
151143--- @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 )
154145 if left_pad <= 0 and right_pad <= 0 then
155146 return
156147 end
157148 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 )
158151 for row = start_row , end_row - 1 do
159152 local left = root ~= nil and root .start_col or self .node .start_col
160153 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
163168 end
164169end
165170
166171--- @private
167172--- @param root ? render.md.Node
168173--- @return integer
169174function 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
173180 else
181+ -- on the last item of the root list ignore the last line if empty
174182 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
200187 end
201188end
202189
0 commit comments