@@ -252,6 +252,22 @@ local function remove_previous_output(linenr)
252
252
fn .setpos (" ." , saved_pos )
253
253
end
254
254
255
+ -- Deletes existing lines in virtual output
256
+ -- @param bufnr Buffer number
257
+ -- @param ns_id Namespace id
258
+ local function remove_virtual_lines (bufnr , ns_id , line_start , line_end )
259
+ local existing_marks = vim .api .nvim_buf_get_extmarks (
260
+ bufnr ,
261
+ ns_id ,
262
+ {line_start , 0 },
263
+ {line_end , 0 },
264
+ {}
265
+ )
266
+ for _ , v in pairs (existing_marks ) do
267
+ vim .api .nvim_buf_del_extmark (bufnr , ns_id , v [1 ])
268
+ end
269
+ end
270
+
255
271
-- Writes output of the excuted command after the linenr line.
256
272
-- @param out Table containing lines from the output.
257
273
local function write_output (linenr , out )
@@ -278,10 +294,28 @@ local function write_output(linenr, out)
278
294
out_table [# out_table + 1 ] = " "
279
295
end
280
296
297
+ -- Do not use virtual text
298
+ if (M .opts .virt_text ~= nil ) then
299
+ for _ , s in pairs (out_table ) do
300
+ fn .append (linenr , s )
301
+ linenr = linenr + 1
302
+ end
303
+ return
304
+ end
305
+
306
+ -- Use virtual text
307
+ local bufnr = vim .api .nvim_get_current_buf ()
308
+ local ns_id = vim .api .nvim_create_namespace (" mdeval" )
309
+ remove_virtual_lines (bufnr , ns_id , linenr , linenr + 1 )
310
+ local virtual_lines = {}
281
311
for _ , s in pairs (out_table ) do
282
- fn . append ( linenr , s )
283
- linenr = linenr + 1
312
+ local vline = {{ s , " MdEvalLine " }}
313
+ table.insert ( virtual_lines , vline )
284
314
end
315
+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , linenr , 0 , {
316
+ virt_lines = virtual_lines ,
317
+ virt_lines_above = true ,
318
+ })
285
319
end
286
320
287
321
-- Parses start line to get code of the language.
@@ -427,7 +461,16 @@ function M:eval_clean_results()
427
461
print (" Not inside a code block." )
428
462
return
429
463
end
464
+
430
465
remove_previous_output (linenr_until + 1 )
466
+
467
+ -- clean virt text (block)
468
+ local bufnr = vim .api .nvim_get_current_buf ()
469
+ local ns_id = vim .api .nvim_create_namespace (" mdeval" )
470
+ remove_virtual_lines (bufnr , ns_id , linenr_until , linenr_until + 1 )
471
+
472
+ -- INFO: if it were to delete all virt text, it would be like:
473
+ -- delete_existing_lines(bufnr, ns_id, 0, vim.api.nvim_buf_line_count(0))
431
474
end
432
475
433
476
M .opts = defaults
0 commit comments