mei28 / luminate.nvim

🌟 Highlight yanked, pasted, and undone/redone text in Neovim with a splash of color.
MIT License
37 stars 2 forks source link

`defer_clear_highlights`: invalid buffer id in `nvim_buf_clear_namespace` #4

Closed lttb closed 1 month ago

lttb commented 1 month ago

thanks for this plugin, looks really nice!

but I occasionally see this error:

Error executing vim.schedule lua callback: ...ocal/share/nvim/lazy/luminate.nvim/lua/luminate/init.lua:144: Invalid buffer id: 9
stack traceback:
    [C]: in function 'nvim_buf_clear_namespace'
    ...ocal/share/nvim/lazy/luminate.nvim/lua/luminate/init.lua:144: in function ''
    vim/_editor.lua: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

it seems that it happens when I open the file with telescope.

video with an example of the issue:

https://github.com/mei28/luminate.nvim/assets/11135392/b8e51772-7514-4d93-a887-87b9480496cc

I'm happy to open a PR if I'll be able to resolve this issue.

thanks!

mei28 commented 1 month ago

Thank you for reporting this issue. I will investigate to see if I can reproduce the problem.

JayLCypher commented 1 month ago

+1 I notice the same thing happening many times. Seems to be related to temporary buffers that close on file open, such as the immediate empty new buffer on entry and alpha.nvim type dashboard buffers, additionally the file explorer buffers.

Might be a simple life-time deal, the defer stores the buffer ID but if you close it faster than illuminate can defer, it errors out as a closed buffer. Might need to check if the buffer is still open in the clear_highlights callback.

Created a small change to defer_clear_highlights

function M.defer_clear_highlights(bufnr, namespace)
  vim.defer_fn(function()
    if not api.nvim_buf_is_valid(bufnr) then return end -- Check if buffer is still valid.
    api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
  end, config_module.config.duration)
end