@@ -3,16 +3,19 @@ local icons = require('render-markdown.core.icons')
33local str = require (' render-markdown.core.str' )
44local util = require (' render-markdown.render.util' )
55
6+ --- @class render.md.data.Code
7+ --- @field col integer
8+ --- @field start_row integer
9+ --- @field end_row integer
10+ --- @field code_info ? render.md.NodeInfo
11+ --- @field language_info ? render.md.NodeInfo
12+ --- @field language ? string
13+ --- @field max_width integer
14+ --- @field empty_rows integer[]
15+
616--- @class render.md.render.Code : render.md.Renderer
717--- @field private code render.md.Code
8- --- @field private col integer
9- --- @field private start_row integer
10- --- @field private end_row integer
11- --- @field private code_info ? render.md.NodeInfo
12- --- @field private language_info ? render.md.NodeInfo
13- --- @field private language ? string
14- --- @field private width integer
15- --- @field private empty_rows integer[]
18+ --- @field private data render.md.data.Code
1619local Render = {}
1720Render .__index = Render
1821
@@ -31,44 +34,49 @@ function Render:setup()
3134 if not self .code .enabled or self .code .style == ' none' then
3235 return false
3336 end
34-
3537 -- Do not attempt to render single line code block
36- self .col , self .start_row , self .end_row = self .info .start_col , self .info .start_row , self .info .end_row
37- if self .end_row - self .start_row <= 1 then
38+ if self .info .end_row - self .info .start_row <= 1 then
3839 return false
3940 end
4041
41- self .code_info = self .info :child (' info_string' )
42- self .language_info = self .code_info ~= nil and self .code_info :child (' language' ) or nil
43- self .language = (self .language_info or {}).text
42+ local code_info = self .info :child (' info_string' )
43+ local language_info = code_info ~= nil and code_info :child (' language' ) or nil
4444
4545 -- Account for language padding in first row
4646 local widths = vim .tbl_map (str .width , self .info :lines ())
4747 widths [1 ] = widths [1 ] + self .code .language_pad
48-
49- self .width = self .code .left_pad + vim .fn .max (widths ) + self .code .right_pad
50- self .width = math.max (self .width , self .code .min_width )
51-
52- self .empty_rows = {}
48+ local max_width = self .code .left_pad + vim .fn .max (widths ) + self .code .right_pad
49+ local empty_rows = {}
5350 for row , width in ipairs (widths ) do
5451 if width == 0 then
55- table.insert (self . empty_rows , self .start_row + row - 1 )
52+ table.insert (empty_rows , self . info .start_row + row - 1 )
5653 end
5754 end
5855
56+ self .data = {
57+ col = self .info .start_col ,
58+ start_row = self .info .start_row ,
59+ end_row = self .info .end_row ,
60+ code_info = code_info ,
61+ language_info = language_info ,
62+ language = (language_info or {}).text ,
63+ max_width = math.max (max_width , self .code .min_width ),
64+ empty_rows = empty_rows ,
65+ }
66+
5967 return true
6068end
6169
6270function Render :render ()
63- local disabled_language = vim .tbl_contains (self .code .disable_background , self .language )
71+ local disabled_language = vim .tbl_contains (self .code .disable_background , self .data . language )
6472 local add_background = vim .tbl_contains ({ ' normal' , ' full' }, self .code .style ) and not disabled_language
6573
6674 local icon_added = self :language_hint (add_background )
6775 if add_background then
6876 self :background (icon_added )
6977 end
7078 if icon_added then
71- self .start_row = self .start_row + 1
79+ self .data . start_row = self . data .start_row + 1
7280 end
7381 self :left_pad (add_background )
7482end
@@ -80,7 +88,7 @@ function Render:language_hint(add_background)
8088 if not vim .tbl_contains ({ ' language' , ' full' }, self .code .style ) then
8189 return false
8290 end
83- local info = self .language_info
91+ local info = self .data . language_info
8492 if info == nil then
8593 return false
8694 end
@@ -109,7 +117,7 @@ function Render:language_hint(add_background)
109117 })
110118 elseif self .code .position == ' right' then
111119 local icon_text = icon .. ' ' .. info .text
112- local win_col = self .width - self .code .language_pad
120+ local win_col = self .data . max_width - self .code .language_pad
113121 if self .code .width == ' block' then
114122 win_col = win_col - str .width (icon_text )
115123 end
@@ -125,36 +133,36 @@ end
125133--- @private
126134--- @param icon_added boolean
127135function Render :background (icon_added )
128- local width = self .code .width == ' block' and self .width or self .context :get_width ()
136+ local width = self .code .width == ' block' and self .data . max_width or self .context :get_width ()
129137
130138 if self .code .border == ' thin' then
131- local border_width = width - self .col
132- if not icon_added and self .context :hidden (self .code_info ) and self :delim_hidden (self .start_row ) then
133- self .marks :add (true , self .start_row , self .col , {
139+ local border_width = width - self .data . col
140+ if not icon_added and self .context :hidden (self .data . code_info ) and self :delim_hidden (self . data .start_row ) then
141+ self .marks :add (true , self .data . start_row , self . data .col , {
134142 virt_text = { { self .code .above :rep (border_width ), colors .inverse (self .code .highlight ) } },
135143 virt_text_pos = ' overlay' ,
136144 })
137- self .start_row = self .start_row + 1
145+ self .data . start_row = self . data .start_row + 1
138146 end
139- if self :delim_hidden (self .end_row - 1 ) then
140- self .marks :add (true , self .end_row - 1 , self .col , {
147+ if self :delim_hidden (self .data . end_row - 1 ) then
148+ self .marks :add (true , self .data . end_row - 1 , self . data .col , {
141149 virt_text = { { self .code .below :rep (border_width ), colors .inverse (self .code .highlight ) } },
142150 virt_text_pos = ' overlay' ,
143151 })
144- self .end_row = self .end_row - 1
152+ self .data . end_row = self . data .end_row - 1
145153 end
146154 end
147155
148156 local padding = str .spaces (vim .o .columns * 2 )
149- for row = self .start_row , self .end_row - 1 do
150- self .marks :add (false , row , self .col , {
157+ for row = self .data . start_row , self . data .end_row - 1 do
158+ self .marks :add (false , row , self .data . col , {
151159 end_row = row + 1 ,
152160 hl_group = self .code .highlight ,
153161 hl_eol = true ,
154162 })
155163 if self .code .width == ' block' then
156164 -- Overwrite anything beyond width with Normal
157- self .marks :add (false , row , self .col , {
165+ self .marks :add (false , row , self .data . col , {
158166 priority = 0 ,
159167 virt_text = { { padding , ' Normal' } },
160168 virt_text_win_col = width ,
@@ -173,25 +181,25 @@ end
173181--- @private
174182--- @param add_background boolean
175183function Render :left_pad (add_background )
176- if (self .col == 0 or # self .empty_rows == 0 ) and self .code .left_pad <= 0 then
184+ if (self .data . col == 0 or # self . data .empty_rows == 0 ) and self .code .left_pad <= 0 then
177185 return
178186 end
179187
180188 -- Use low priority to include other marks in padding when code block is at edge
181- local priority = self .col == 0 and 0 or nil
182- local outer_text = { str .spaces (self .col ), ' Normal' }
189+ local priority = self .data . col == 0 and 0 or nil
190+ local outer_text = { str .spaces (self .data . col ), ' Normal' }
183191 local left_text = { str .spaces (self .code .left_pad ), add_background and self .code .highlight or ' Normal' }
184192
185- for row = self .start_row , self .end_row - 1 do
193+ for row = self .data . start_row , self . data .end_row - 1 do
186194 local virt_text = {}
187- if self .col > 0 and vim .tbl_contains (self .empty_rows , row ) then
195+ if self .data . col > 0 and vim .tbl_contains (self . data .empty_rows , row ) then
188196 table.insert (virt_text , outer_text )
189197 end
190198 if self .code .left_pad > 0 then
191199 table.insert (virt_text , left_text )
192200 end
193201 if # virt_text > 0 then
194- self .marks :add (false , row , self .col , {
202+ self .marks :add (false , row , self .data . col , {
195203 priority = priority ,
196204 virt_text = virt_text ,
197205 virt_text_pos = ' inline' ,
0 commit comments