Skip to content

feat: allows showing output as virtual text #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ lang_conf["markdown.pandoc"] = { "```", "```" }

M.lang_conf = lang_conf

M.virtual_text = false
M.require_confirmation = true
M.allowed_file_types = {}
M.exec_timeout = -1
Expand Down
48 changes: 46 additions & 2 deletions lua/mdeval.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ local function remove_previous_output(linenr)
fn.setpos(".", saved_pos)
end

-- Deletes existing lines in virtual output
-- @param bufnr Buffer number
-- @param ns_id Namespace id
local function remove_virtual_lines(bufnr, ns_id, line_start, line_end)
local existing_marks = vim.api.nvim_buf_get_extmarks(
bufnr,
ns_id,
{line_start, 0},
{line_end, 0},
{}
)
for _, v in pairs(existing_marks) do
vim.api.nvim_buf_del_extmark(bufnr, ns_id, v[1])
end
end

-- Writes output of the excuted command after the linenr line.
-- @param out Table containing lines from the output.
local function write_output(linenr, out)
Expand All @@ -278,10 +294,29 @@ local function write_output(linenr, out)
out_table[#out_table + 1] = ""
end

-- TODO: uncomment this block after we get the option to work
-- -- Do not use virtual text
-- if (M.opts.virtual_text ~= nil) then
-- for _, s in pairs(out_table) do
-- fn.append(linenr, s)
-- linenr = linenr + 1
-- end
-- return
-- end

-- Use virtual text
local bufnr = vim.api.nvim_get_current_buf()
local ns_id = vim.api.nvim_create_namespace("mdeval")
remove_virtual_lines(bufnr, ns_id, linenr, linenr+1)
local virtual_lines = {}
for _, s in pairs(out_table) do
fn.append(linenr, s)
linenr = linenr + 1
local vline = {{ s, "MdEvalLine" }}
table.insert(virtual_lines, vline)
end
vim.api.nvim_buf_set_extmark(bufnr, ns_id, linenr, 0, {
virt_lines = virtual_lines,
virt_lines_above = true,
})
end

-- Parses start line to get code of the language.
Expand Down Expand Up @@ -427,7 +462,16 @@ function M:eval_clean_results()
print("Not inside a code block.")
return
end

remove_previous_output(linenr_until + 1)

-- clean virtual text (block)
local bufnr = vim.api.nvim_get_current_buf()
local ns_id = vim.api.nvim_create_namespace("mdeval")
remove_virtual_lines(bufnr, ns_id, linenr_until, linenr_until+1)

-- INFO: if it were to delete all virtual text, it would be like:
-- delete_existing_lines(bufnr, ns_id, 0, vim.api.nvim_buf_line_count(0))
end

M.opts = defaults
Expand Down