Skip to content

Commit 9f96517

Browse files
committed
chore: @ async + use vim.uv
1 parent 6222a8b commit 9f96517

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

lua/neorg/modules/core/integrations/image/module.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,24 @@ module.public = {
4343
---@param images any[]
4444
render = function(images)
4545
for _, limage in pairs(images) do
46-
limage.image:clear()
47-
limage.image:render()
46+
if limage.image then
47+
limage.image:clear()
48+
limage.image:render()
49+
end
4850
end
4951
end,
5052
clear = function(images)
5153
for _, limage in pairs(images) do
52-
limage.image:clear()
54+
if limage.image then
55+
limage.image:clear()
56+
end
5357
end
5458
end,
5559
clear_at_cursor = function(images, row)
5660
local cleared = {}
5761
for id, limage in pairs(images) do
5862
local image = limage.image
59-
if image.geometry.y == row then
63+
if image and image.geometry.y == row then
6064
image:clear()
6165
table.insert(cleared, id)
6266
end

lua/neorg/modules/core/latex/renderer/module.lua

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,19 @@ module.private.clear_extmark = function(key)
154154
end
155155

156156
module.public = {
157+
---@async
157158
async_latex_renderer = function()
159+
--- current location key -> old key
160+
---@type table<string, string>
161+
local latest_image_locations = {}
162+
for key, limage in pairs(module.private.latex_images) do
163+
local extm =
164+
vim.api.nvim_buf_get_extmark_by_id(0, module.private.extmark_ns, module.private.extmark_ids[key], {})
165+
local pos_key = module.private.get_key(extm)
166+
latest_image_locations[pos_key] = key
167+
end
168+
169+
P(latest_image_locations)
158170
---node range to image handle
159171
-- module.private.ranges = {}
160172
---@type table<string, MathRange>
@@ -196,14 +208,28 @@ module.public = {
196208
end
197209
end
198210

199-
local img = module.private.image_api.new_image(
200-
nio.api.nvim_get_current_buf(),
201-
png_location,
202-
module.required["core.integrations.treesitter"].get_node_range(node),
203-
nio.api.nvim_get_current_win(),
204-
module.config.public.scale,
205-
not module.config.public.conceal
206-
)
211+
local img = nil
212+
local next_key = latest_image_locations[key]
213+
if latest_image_locations[key] then
214+
local old_limage = module.private.latex_images[next_key]
215+
print("in here")
216+
img = old_limage.image
217+
module.private.latex_images[next_key].image = nil
218+
module.private.extmark_ids[next_key] = module.private.extmark_ids[key]
219+
else
220+
img = module.private.image_api.new_image(
221+
nio.api.nvim_get_current_buf(),
222+
png_location,
223+
module.required["core.integrations.treesitter"].get_node_range(node),
224+
nio.api.nvim_get_current_win(),
225+
module.config.public.scale,
226+
not module.config.public.conceal
227+
)
228+
end
229+
if not img then
230+
print("nil image")
231+
print(key)
232+
end
207233
next_images[key] = { image = img, range = range, snippet = latex_snippet }
208234
end
209235
)
@@ -221,7 +247,7 @@ module.public = {
221247
-- existing images in the same position, if it's a different snippet, clear it, b/c it's
222248
-- no longer accurate
223249
local existing_img = module.private.latex_images[key]
224-
if existing_img and existing_img.snippet ~= limage.snippet then
250+
if existing_img and existing_img.image and existing_img.snippet ~= limage.snippet then
225251
module.private.image_api.clear({ [key] = existing_img })
226252
module.private.clear_extmark(key)
227253
end
@@ -230,6 +256,7 @@ module.public = {
230256
end,
231257

232258
---Writes a latex snippet to a file and wraps it with latex headers to it will render nicely
259+
---@async
233260
---@param snippet string latex snippet (if it's math it should include the surrounding $$)
234261
---@return string temp file path
235262
async_create_latex_document = function(snippet)
@@ -253,6 +280,7 @@ module.public = {
253280
end,
254281

255282
---Returns a filepath where the rendered image sits
283+
---@async
256284
---@param snippet string the full latex snippet to convert to an image
257285
---@return string | nil
258286
async_generate_image = function(snippet)
@@ -324,23 +352,25 @@ module.public = {
324352
table.insert(module.private.cleared_at_cursor, key)
325353
goto continue
326354
end
327-
if not image.is_rendered then
328-
module.private.image_api.render({ limage })
329-
end
355+
if image then
356+
if not image.is_rendered then
357+
module.private.image_api.render({ limage })
358+
end
330359

331-
if conceal_on then
332-
if not module.private.extmark_ids[key] then
333-
module.private.clear_extmark(key)
334-
local id = vim.api.nvim_buf_set_extmark(0, module.private.extmark_ns, range[1], range[2], {
335-
end_col = range[4],
336-
conceal = "",
337-
virt_text = { { (" "):rep(image.rendered_geometry.width or image.geometry.width) } },
338-
virt_text_pos = "inline",
339-
strict = false, -- this might be a problem... I'm not sure, it could also be fine.
340-
invalidate = true,
341-
undo_restore = false,
342-
})
343-
module.private.extmark_ids[key] = id
360+
if conceal_on then
361+
if not module.private.extmark_ids[key] then
362+
module.private.clear_extmark(key)
363+
local id = vim.api.nvim_buf_set_extmark(0, module.private.extmark_ns, range[1], range[2], {
364+
end_col = range[4],
365+
conceal = "",
366+
virt_text = { { (" "):rep(image.rendered_geometry.width or image.geometry.width) } },
367+
virt_text_pos = "inline",
368+
strict = false, -- this might be a problem... I'm not sure, it could also be fine.
369+
invalidate = true,
370+
undo_restore = false,
371+
})
372+
module.private.extmark_ids[key] = id
373+
end
344374
end
345375
end
346376
::continue::
@@ -361,7 +391,7 @@ local function render_latex()
361391
end
362392

363393
if not render_timer then
364-
render_timer = vim.loop.new_timer()
394+
render_timer = vim.uv.new_timer()
365395
end
366396

367397
render_timer:start(module.config.public.debounce_ms, 0, function()

0 commit comments

Comments
 (0)