Skip to content

Commit d228a3c

Browse files
perf: store components in context to avoid duplicate queries
1 parent 74b77c7 commit d228a3c

File tree

8 files changed

+58
-65
lines changed

8 files changed

+58
-65
lines changed

Diff for: benches/medium_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local util = require('benches.util')
55
describe('medium.md', function()
66
it('default', function()
77
local base_marks = 46
8-
util.less_than(util.setup('temp/medium.md'), 30)
8+
util.less_than(util.setup('temp/medium.md'), 35)
99
util.num_marks(base_marks)
1010

1111
util.less_than(util.move_down(3), 0.5)

Diff for: benches/medium_table_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local util = require('benches.util')
55
describe('medium-table.md', function()
66
it('default', function()
77
local base_marks = 180
8-
util.less_than(util.setup('temp/medium-table.md'), 105)
8+
util.less_than(util.setup('temp/medium-table.md'), 110)
99
util.num_marks(base_marks)
1010

1111
util.less_than(util.move_down(1), 0.5)

Diff for: lua/render-markdown/core/component.lua

-52
This file was deleted.

Diff for: lua/render-markdown/core/context.lua

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local util = require('render-markdown.core.util')
88
---@field private win integer
99
---@field private top integer
1010
---@field private bottom integer
11+
---@field private components table<integer, render.md.CustomComponent>
1112
---@field private conceal? table<integer, [integer, integer][]>
1213
---@field private links table<integer, [integer, integer, integer][]>
1314
---@field last_heading integer
@@ -25,6 +26,7 @@ function Context.new(buf, win, offset)
2526
local top = util.view(win).topline - 1
2627
self.top = math.max(top - offset, 0)
2728
self.bottom = self:compute_bottom(top, offset)
29+
self.components = {}
2830
self.conceal = nil
2931
self.links = {}
3032
self.last_heading = -1
@@ -48,6 +50,18 @@ function Context:compute_bottom(top, offset)
4850
return bottom
4951
end
5052

53+
---@param info render.md.NodeInfo
54+
---@return render.md.CustomComponent?
55+
function Context:get_component(info)
56+
return self.components[info.start_row]
57+
end
58+
59+
---@param info render.md.NodeInfo
60+
---@param component render.md.CustomComponent
61+
function Context:add_component(info, component)
62+
self.components[info.start_row] = component
63+
end
64+
5165
---@param info? render.md.NodeInfo
5266
---@return integer
5367
function Context:width(info)

Diff for: lua/render-markdown/handler/markdown.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local Context = require('render-markdown.core.context')
2-
local component = require('render-markdown.core.component')
32
local list = require('render-markdown.core.list')
43
local logger = require('render-markdown.core.logger')
54
local state = require('render-markdown.state')
@@ -106,14 +105,16 @@ function Handler:list_marker(info)
106105
if not self.config.checkbox.enabled then
107106
return false
108107
end
108+
if self.context:get_component(info) ~= nil then
109+
return true
110+
end
109111
if info:sibling('task_list_marker_unchecked') ~= nil then
110112
return true
111113
end
112114
if info:sibling('task_list_marker_checked') ~= nil then
113115
return true
114116
end
115-
local paragraph = info:sibling('paragraph')
116-
return paragraph ~= nil and component.checkbox(self.config, paragraph.text, 'starts') ~= nil
117+
return false
117118
end
118119
if sibling_checkbox() then
119120
-- Hide the list marker for checkboxes rather than replacing with a bullet point

Diff for: lua/render-markdown/handler/markdown_inline.lua

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local Context = require('render-markdown.core.context')
2-
local component = require('render-markdown.core.component')
32
local list = require('render-markdown.core.list')
43
local logger = require('render-markdown.core.logger')
54
local state = require('render-markdown.state')
@@ -56,13 +55,13 @@ end
5655
---@private
5756
---@param info render.md.NodeInfo
5857
function Handler:shortcut(info)
59-
local callout = component.callout(self.config, info.text, 'exact')
58+
local callout = self:get_callout(info)
6059
if callout ~= nil then
6160
self:callout(info, callout)
6261
return
6362
end
6463

65-
local checkbox = component.checkbox(self.config, info.text, 'exact')
64+
local checkbox = self:get_checkbox(info)
6665
if checkbox ~= nil then
6766
self:checkbox(info, checkbox)
6867
return
@@ -75,6 +74,32 @@ function Handler:shortcut(info)
7574
end
7675
end
7776

77+
---@private
78+
---@param info render.md.NodeInfo
79+
---@return render.md.CustomComponent?
80+
function Handler:get_callout(info)
81+
local text = info.text:lower()
82+
for _, callout in pairs(self.config.callout) do
83+
if text == callout.raw:lower() then
84+
return callout
85+
end
86+
end
87+
return nil
88+
end
89+
90+
---@private
91+
---@param info render.md.NodeInfo
92+
---@return render.md.CustomComponent?
93+
function Handler:get_checkbox(info)
94+
local text = info.text
95+
for _, checkbox in pairs(self.config.checkbox.custom) do
96+
if text == checkbox.raw then
97+
return checkbox
98+
end
99+
end
100+
return nil
101+
end
102+
78103
---@private
79104
---@param info render.md.NodeInfo
80105
---@param callout render.md.CustomComponent
@@ -99,13 +124,16 @@ function Handler:callout(info, callout)
99124
end
100125

101126
local text, conceal = custom_title()
102-
self.marks:add(true, info.start_row, info.start_col, {
127+
local added = self.marks:add(true, info.start_row, info.start_col, {
103128
end_row = info.end_row,
104129
end_col = info.end_col,
105130
virt_text = { { text, callout.highlight } },
106131
virt_text_pos = 'overlay',
107132
conceal = conceal,
108133
})
134+
if added then
135+
self.context:add_component(info, callout)
136+
end
109137
end
110138

111139
---@private
@@ -117,13 +145,16 @@ function Handler:checkbox(info, checkbox)
117145
end
118146
local inline = self.config.checkbox.position == 'inline'
119147
local icon, highlight = checkbox.rendered, checkbox.highlight
120-
self.marks:add(true, info.start_row, info.start_col, {
148+
local added = self.marks:add(true, info.start_row, info.start_col, {
121149
end_row = info.end_row,
122150
end_col = info.end_col,
123151
virt_text = { { inline and icon or str.pad_to(info.text, icon), highlight } },
124152
virt_text_pos = 'inline',
125153
conceal = '',
126154
})
155+
if added then
156+
self.context:add_component(info, checkbox)
157+
end
127158
end
128159

129160
---@private

Diff for: lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.3.1'
8+
M.version = '6.3.2'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

Diff for: lua/render-markdown/render/quote.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local Base = require('render-markdown.render.base')
2-
local component = require('render-markdown.core.component')
32
local logger = require('render-markdown.core.logger')
43
local state = require('render-markdown.state')
54

@@ -25,7 +24,7 @@ function Render:setup()
2524
return false
2625
end
2726

28-
local callout = component.callout(self.config, self.info.text, 'contains')
27+
local callout = self.context:get_component(self.info)
2928
self.highlight = callout ~= nil and callout.highlight or self.quote.highlight
3029

3130
return true

0 commit comments

Comments
 (0)