fix(debug.run): schedule only nvim_buf_set_extmark in on_print (#425)

## Description

<!-- Describe the big picture of your changes to communicate to the
maintainers
  why we should accept this pull request. -->

Previously, `get_line` was called from a scheduled function, and would
always return nil, placing the extmark for print output on the wrong
line.

## Screenshots

<!-- Add screenshots of the changes if applicable. -->

Before:

![image](https://github.com/user-attachments/assets/d493f70a-82c6-4a48-82f6-66beed7b9383)

After:

![image](https://github.com/user-attachments/assets/9a0ec53c-fdfb-4786-b954-86284d3eac54)

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Stefan Boca 2025-01-20 04:36:34 -08:00 committed by GitHub
parent f904481439
commit 81572b5f97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -131,9 +131,13 @@ function M.run(opts)
for _, line in ipairs(vim.split(str, "\n", { plain = true })) do for _, line in ipairs(vim.split(str, "\n", { plain = true })) do
table.insert(virt_lines, { { "", "SnacksDebugIndent" }, { line, "SnacksDebugPrint" } }) table.insert(virt_lines, { { "", "SnacksDebugIndent" }, { line, "SnacksDebugPrint" } })
end end
vim.api.nvim_buf_set_extmark(buf, ns, (get_line() or 1) - 1, 0, {
local line = (get_line() or 1) - 1
vim.schedule(function()
vim.api.nvim_buf_set_extmark(buf, ns, line, 0, {
virt_lines = virt_lines, virt_lines = virt_lines,
}) })
end)
end end
-- Load the code -- Load the code
@ -143,7 +147,7 @@ function M.run(opts)
end end
-- Setup the env -- Setup the env
local env = { print = opts.print and vim.schedule_wrap(on_print) or nil } local env = { print = opts.print and on_print or nil }
package.seeall(env) package.seeall(env)
setfenv(chunk, env) setfenv(chunk, env)
xpcall(chunk, function(e) xpcall(chunk, function(e)