Skip to content

Commit 8fdfc32

Browse files
refactor: move setup fields into data class
1 parent 9c4af1d commit 8fdfc32

File tree

5 files changed

+176
-169
lines changed

5 files changed

+176
-169
lines changed

lua/render-markdown/core/node_info.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function NodeInfo.new(buf, node)
3535
end
3636

3737
---@param infos render.md.NodeInfo[]
38+
---@return render.md.NodeInfo[]
3839
function NodeInfo.sort_inplace(infos)
3940
table.sort(infos, function(a, b)
4041
if a.start_row ~= b.start_row then
@@ -43,6 +44,7 @@ function NodeInfo.sort_inplace(infos)
4344
return a.start_col < b.start_col
4445
end
4546
end)
47+
return infos
4648
end
4749

4850
---@return boolean

lua/render-markdown/render/code.lua

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ local icons = require('render-markdown.core.icons')
33
local str = require('render-markdown.core.str')
44
local 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
1619
local Render = {}
1720
Render.__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
6068
end
6169

6270
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)
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)
7482
end
@@ -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
127135
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()
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
175183
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
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',

lua/render-markdown/render/heading.lua

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ local list = require('render-markdown.core.list')
33
local str = require('render-markdown.core.str')
44
local util = require('render-markdown.render.util')
55

6+
---@class render.md.data.Heading
7+
---@field level integer
8+
---@field icon? string
9+
---@field sign? string
10+
---@field foreground string
11+
---@field background string
12+
---@field heading_width render.md.heading.Width
13+
614
---@class render.md.render.Heading: render.md.Renderer
715
---@field private heading render.md.Heading
8-
---@field private level integer
9-
---@field private icon? string
10-
---@field private sign? string
11-
---@field private foreground string
12-
---@field private background string
13-
---@field private heading_width render.md.heading.Width
16+
---@field private data render.md.data.Heading
1417
local Render = {}
1518
Render.__index = Render
1619

@@ -30,36 +33,38 @@ function Render:setup()
3033
return false
3134
end
3235

33-
self.level = str.width(self.info.text)
34-
self.icon = list.cycle(self.heading.icons, self.level)
35-
self.sign = list.cycle(self.heading.signs, self.level)
36-
self.foreground = list.clamp(self.heading.foregrounds, self.level)
37-
self.background = list.clamp(self.heading.backgrounds, self.level)
38-
36+
local level = str.width(self.info.text)
3937
local heading_width = self.heading.width
4038
if type(heading_width) == 'table' then
41-
heading_width = list.clamp(heading_width, self.level)
39+
heading_width = list.clamp(heading_width, level)
4240
end
43-
self.heading_width = heading_width
41+
self.data = {
42+
level = level,
43+
icon = list.cycle(self.heading.icons, level),
44+
sign = list.cycle(self.heading.signs, level),
45+
foreground = list.clamp(self.heading.foregrounds, level),
46+
background = list.clamp(self.heading.backgrounds, level),
47+
heading_width = heading_width,
48+
}
4449

4550
return true
4651
end
4752

4853
function Render:render()
4954
local icon_width = self:start_icon()
5055
if self.heading.sign then
51-
util.sign(self.config, self.marks, self.info, self.sign, self.foreground)
56+
util.sign(self.config, self.marks, self.info, self.data.sign, self.data.foreground)
5257
end
5358

5459
self.marks:add(true, self.info.start_row, 0, {
5560
end_row = self.info.end_row + 1,
5661
end_col = 0,
57-
hl_group = self.background,
62+
hl_group = self.data.background,
5863
hl_eol = true,
5964
})
6065

6166
local width = self:width(icon_width)
62-
if self.heading_width == 'block' then
67+
if self.data.heading_width == 'block' then
6368
-- Overwrite anything beyond width with Normal
6469
self.marks:add(true, self.info.start_row, 0, {
6570
priority = 0,
@@ -75,7 +80,7 @@ function Render:render()
7580
if self.heading.left_pad > 0 then
7681
self.marks:add(false, self.info.start_row, 0, {
7782
priority = 0,
78-
virt_text = { { str.spaces(self.heading.left_pad), self.background } },
83+
virt_text = { { str.spaces(self.heading.left_pad), self.data.background } },
7984
virt_text_pos = 'inline',
8085
})
8186
end
@@ -87,26 +92,26 @@ function Render:start_icon()
8792
-- Available width is level + 1 - concealed, where level = number of `#` characters, one
8893
-- is added to account for the space after the last `#` but before the heading title,
8994
-- and concealed text is subtracted since that space is not usable
90-
local width = self.level + 1 - self.context:concealed(self.info)
91-
if self.icon == nil then
95+
local width = self.data.level + 1 - self.context:concealed(self.info)
96+
if self.data.icon == nil then
9297
return width
9398
end
9499

95-
local padding = width - str.width(self.icon)
100+
local padding = width - str.width(self.data.icon)
96101
if self.heading.position == 'inline' or padding < 0 then
97102
self.marks:add(true, self.info.start_row, self.info.start_col, {
98103
end_row = self.info.end_row,
99104
end_col = self.info.end_col,
100-
virt_text = { { self.icon, { self.foreground, self.background } } },
105+
virt_text = { { self.data.icon, { self.data.foreground, self.data.background } } },
101106
virt_text_pos = 'inline',
102107
conceal = '',
103108
})
104-
return str.width(self.icon)
109+
return str.width(self.data.icon)
105110
else
106111
self.marks:add(true, self.info.start_row, self.info.start_col, {
107112
end_row = self.info.end_row,
108113
end_col = self.info.end_col,
109-
virt_text = { { str.pad(padding, self.icon), { self.foreground, self.background } } },
114+
virt_text = { { str.pad(padding, self.data.icon), { self.data.foreground, self.data.background } } },
110115
virt_text_pos = 'overlay',
111116
})
112117
return width
@@ -117,7 +122,7 @@ end
117122
---@param icon_width integer
118123
---@return integer
119124
function Render:width(icon_width)
120-
if self.heading_width == 'block' then
125+
if self.data.heading_width == 'block' then
121126
local width = self.heading.left_pad + icon_width + self.heading.right_pad
122127
local content = self.info:sibling('inline')
123128
if content ~= nil then
@@ -132,12 +137,12 @@ end
132137
---@private
133138
---@param width integer
134139
function Render:border(width)
135-
local background = colors.inverse(self.background)
136-
local prefix = self.heading.border_prefix and self.level or 0
140+
local background = colors.inverse(self.data.background)
141+
local prefix = self.heading.border_prefix and self.data.level or 0
137142

138143
local line_above = {
139144
{ self.heading.above:rep(self.heading.left_pad), background },
140-
{ self.heading.above:rep(prefix), self.foreground },
145+
{ self.heading.above:rep(prefix), self.data.foreground },
141146
{ self.heading.above:rep(width - self.heading.left_pad - prefix), background },
142147
}
143148
if str.width(self.info:line('above')) == 0 and self.info.start_row - 1 ~= self.context.last_heading then
@@ -154,7 +159,7 @@ function Render:border(width)
154159

155160
local line_below = {
156161
{ self.heading.below:rep(self.heading.left_pad), background },
157-
{ self.heading.below:rep(prefix), self.foreground },
162+
{ self.heading.below:rep(prefix), self.data.foreground },
158163
{ self.heading.below:rep(width - self.heading.left_pad - prefix), background },
159164
}
160165
if str.width(self.info:line('below')) == 0 then

lua/render-markdown/render/quote.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local state = require('render-markdown.state')
44

55
---@class render.md.render.Quote: render.md.Renderer
66
---@field private quote render.md.Quote
7+
---@field private highlight string
78
local Render = {}
89
Render.__index = Render
910

@@ -23,6 +24,9 @@ function Render:setup()
2324
return false
2425
end
2526

27+
local callout = component.callout(self.config, self.info.text, 'contains')
28+
self.highlight = callout ~= nil and callout.highlight or self.quote.highlight
29+
2630
return true
2731
end
2832

@@ -39,12 +43,10 @@ end
3943
---@private
4044
---@param info render.md.NodeInfo
4145
function Render:quote_marker(info)
42-
local callout = component.callout(self.config, self.info.text, 'contains')
43-
local highlight = callout ~= nil and callout.highlight or self.quote.highlight
4446
self.marks:add(true, info.start_row, info.start_col, {
4547
end_row = info.end_row,
4648
end_col = info.end_col,
47-
virt_text = { { info.text:gsub('>', self.quote.icon), highlight } },
49+
virt_text = { { info.text:gsub('>', self.quote.icon), self.highlight } },
4850
virt_text_pos = 'overlay',
4951
virt_text_repeat_linebreak = self.quote.repeat_linebreak or nil,
5052
})

0 commit comments

Comments
 (0)