@@ -154,7 +154,19 @@ module.private.clear_extmark = function(key)
154
154
end
155
155
156
156
module .public = {
157
+ --- @async
157
158
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 )
158
170
--- node range to image handle
159
171
-- module.private.ranges = {}
160
172
--- @type table<string , MathRange>
@@ -196,14 +208,28 @@ module.public = {
196
208
end
197
209
end
198
210
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
207
233
next_images [key ] = { image = img , range = range , snippet = latex_snippet }
208
234
end
209
235
)
@@ -221,7 +247,7 @@ module.public = {
221
247
-- existing images in the same position, if it's a different snippet, clear it, b/c it's
222
248
-- no longer accurate
223
249
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
225
251
module .private .image_api .clear ({ [key ] = existing_img })
226
252
module .private .clear_extmark (key )
227
253
end
@@ -230,6 +256,7 @@ module.public = {
230
256
end ,
231
257
232
258
--- Writes a latex snippet to a file and wraps it with latex headers to it will render nicely
259
+ --- @async
233
260
--- @param snippet string latex snippet (if it ' s math it should include the surrounding $$)
234
261
--- @return string temp file path
235
262
async_create_latex_document = function (snippet )
@@ -253,6 +280,7 @@ module.public = {
253
280
end ,
254
281
255
282
--- Returns a filepath where the rendered image sits
283
+ --- @async
256
284
--- @param snippet string the full latex snippet to convert to an image
257
285
--- @return string | nil
258
286
async_generate_image = function (snippet )
@@ -324,23 +352,25 @@ module.public = {
324
352
table.insert (module .private .cleared_at_cursor , key )
325
353
goto continue
326
354
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
330
359
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
344
374
end
345
375
end
346
376
:: continue::
@@ -361,7 +391,7 @@ local function render_latex()
361
391
end
362
392
363
393
if not render_timer then
364
- render_timer = vim .loop .new_timer ()
394
+ render_timer = vim .uv .new_timer ()
365
395
end
366
396
367
397
render_timer :start (module .config .public .debounce_ms , 0 , function ()
0 commit comments