gorbit99 / codewindow.nvim

MIT License
431 stars 16 forks source link

Error: /nvim/lazy/codewindow.nvim/lua/codewindow/highlight.lua:241: Invalid buffer id: 51 #61

Closed nyngwang closed 1 year ago

nyngwang commented 1 year ago
Error executing vim.schedule lua callback: ...e/nvim/lazy/codewindow.nvim/lua/codewindow/highlight.lua:241: Invalid buffer id: 51
stack traceback:
    [C]: in function 'nvim_buf_clear_namespace'
    ...e/nvim/lazy/codewindow.nvim/lua/codewindow/highlight.lua:241: in function 'display_cursor'
    ...local/share/nvim/lazy/codewindow.nvim/lua/codewindow.lua:17: in function ''
    vim/_editor.lua: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

It might be that some API calls can be protected by checking whether or not it's valid using nvim_buf_is_valid.

https://github.com/gorbit99/codewindow.nvim/assets/24765272/a9c34d2e-6644-419c-b014-2c6131fa6feb

These are the two autocmds I used in the DEMO: (with the augroup name removed.)

-- if I remove the first one then the error disappear, but then the minimap will not be closed.
vim.api.nvim_create_autocmd({ 'WinLeave', 'TermEnter' }, {
  callback = vim.schedule_wrap(function ()
    require('codewindow').close_minimap()
  end)
})
vim.api.nvim_create_autocmd({ 'CursorHold' }, {
  callback = vim.schedule_wrap(function ()
    if vim.api.nvim_win_get_config(0).relative == 'editor'
      and vim.api.nvim_buf_is_valid(0)
    then
      pcall(require('codewindow').open_minimap)
    end
  end)
})
gorbit99 commented 1 year ago

I'm curious, what are you trying to achieve with these autocommands?

nyngwang commented 1 year ago

[...], what are you trying to achieve with these autocommands?

Basically active_in_terminals = false + auto_enable = true with a note that the minimap should only be enabled when it on a floating window, but the latter is buggy (it produce a similar error about invalid something, so I decided to do it myself)


TL;DR: I think the two events Term{Enter, Open} should be added when you're closing the minimap given auto_enable = true.

I just resolved it after some trying:

vim.api.nvim_create_autocmd({
  'TermEnter', 'TermOpen', -- for me these two events seem to be the key!
  'BufLeave'
}, {
  group = curfile_augroup,
  callback = function ()
    require('codewindow').close_minimap()
  end
})
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
  group = curfile_augroup,
  callback = vim.schedule_wrap(function ()
    if vim.api.nvim_win_get_config(0).relative == 'editor' then
      require('codewindow').open_minimap()
    end
  end)
})
gorbit99 commented 1 year ago

Try putting in the terminal's filetype into the "exclude_filetypes" table. If you are using floaterm, this would be floaterm, but ymmv. auto_enable iirc works off of that table

nyngwang commented 1 year ago

It doesn't work, and I think that is unrelated to my problem because the error was triggered when I closed the floating window in my demo. (I did try exclude_filetypes with fzf for fzf-lua, but again it didn't solve it.)

gorbit99 commented 1 year ago

Interesting, anyways, if you could solve the problem, that's great. The project is very much in need of a proper rewrite anyways, I'll see what I can do then.