@@ -3,16 +3,19 @@ local icons = require('render-markdown.core.icons')
3
3
local str = require (' render-markdown.core.str' )
4
4
local util = require (' render-markdown.render.util' )
5
5
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
+
6
16
--- @class render.md.render.Code : render.md.Renderer
7
17
--- @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
16
19
local Render = {}
17
20
Render .__index = Render
18
21
@@ -31,44 +34,49 @@ function Render:setup()
31
34
if not self .code .enabled or self .code .style == ' none' then
32
35
return false
33
36
end
34
-
35
37
-- 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
38
39
return false
39
40
end
40
41
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
44
44
45
45
-- Account for language padding in first row
46
46
local widths = vim .tbl_map (str .width , self .info :lines ())
47
47
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 = {}
53
50
for row , width in ipairs (widths ) do
54
51
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 )
56
53
end
57
54
end
58
55
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
+
59
67
return true
60
68
end
61
69
62
70
function 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 )
64
72
local add_background = vim .tbl_contains ({ ' normal' , ' full' }, self .code .style ) and not disabled_language
65
73
66
74
local icon_added = self :language_hint (add_background )
67
75
if add_background then
68
76
self :background (icon_added )
69
77
end
70
78
if icon_added then
71
- self .start_row = self .start_row + 1
79
+ self .data . start_row = self . data .start_row + 1
72
80
end
73
81
self :left_pad (add_background )
74
82
end
@@ -80,7 +88,7 @@ function Render:language_hint(add_background)
80
88
if not vim .tbl_contains ({ ' language' , ' full' }, self .code .style ) then
81
89
return false
82
90
end
83
- local info = self .language_info
91
+ local info = self .data . language_info
84
92
if info == nil then
85
93
return false
86
94
end
@@ -109,7 +117,7 @@ function Render:language_hint(add_background)
109
117
})
110
118
elseif self .code .position == ' right' then
111
119
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
113
121
if self .code .width == ' block' then
114
122
win_col = win_col - str .width (icon_text )
115
123
end
@@ -125,36 +133,36 @@ end
125
133
--- @private
126
134
--- @param icon_added boolean
127
135
function 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 ()
129
137
130
138
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 , {
134
142
virt_text = { { self .code .above :rep (border_width ), colors .inverse (self .code .highlight ) } },
135
143
virt_text_pos = ' overlay' ,
136
144
})
137
- self .start_row = self .start_row + 1
145
+ self .data . start_row = self . data .start_row + 1
138
146
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 , {
141
149
virt_text = { { self .code .below :rep (border_width ), colors .inverse (self .code .highlight ) } },
142
150
virt_text_pos = ' overlay' ,
143
151
})
144
- self .end_row = self .end_row - 1
152
+ self .data . end_row = self . data .end_row - 1
145
153
end
146
154
end
147
155
148
156
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 , {
151
159
end_row = row + 1 ,
152
160
hl_group = self .code .highlight ,
153
161
hl_eol = true ,
154
162
})
155
163
if self .code .width == ' block' then
156
164
-- Overwrite anything beyond width with Normal
157
- self .marks :add (false , row , self .col , {
165
+ self .marks :add (false , row , self .data . col , {
158
166
priority = 0 ,
159
167
virt_text = { { padding , ' Normal' } },
160
168
virt_text_win_col = width ,
@@ -173,25 +181,25 @@ end
173
181
--- @private
174
182
--- @param add_background boolean
175
183
function 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
177
185
return
178
186
end
179
187
180
188
-- 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' }
183
191
local left_text = { str .spaces (self .code .left_pad ), add_background and self .code .highlight or ' Normal' }
184
192
185
- for row = self .start_row , self .end_row - 1 do
193
+ for row = self .data . start_row , self . data .end_row - 1 do
186
194
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
188
196
table.insert (virt_text , outer_text )
189
197
end
190
198
if self .code .left_pad > 0 then
191
199
table.insert (virt_text , left_text )
192
200
end
193
201
if # virt_text > 0 then
194
- self .marks :add (false , row , self .col , {
202
+ self .marks :add (false , row , self .data . col , {
195
203
priority = priority ,
196
204
virt_text = virt_text ,
197
205
virt_text_pos = ' inline' ,
0 commit comments