brenoprata10 / nvim-highlight-colors

Highlight colors for neovim
723 stars 33 forks source link

Calling `HighlightColors On` again in an autocmd caused an error `Vim:Error .../lua/nvim-highlight-colors/init.lua:85: Invalid window id: -1` #58

Closed nyngwang closed 9 months ago

nyngwang commented 9 months ago

I think this is easy to fix by ensuring that it is always safe to call HighlightColors related commands again even if it's already enabled.

   Error  18:59:36 msg_show.lua_error E5108: Error executing lua: vim/_editor.lua:0: nvim_exec2()..ColorScheme Autocommands for "miasma": Vim(append):Error executing lua callback: vim/_editor.lua:0: nvim_exec2()..ColorScheme Autocommands for "miasma"..script nvim_exec2() called at ColorScheme Autocommands for "miasma":0..User Autocommands for "PostColorScheme": Vim(append):Error executing lua callback: vim/_editor.lua:0: nvim_exec2()..ColorScheme Autocommands for "miasma"..script nvim_exec2() called at ColorScheme Autocommands for "miasma":0..User Autocommands for "PostColorScheme"..script nvim_exec2() called at User Autocommands for "PostColorScheme":0:

Vim:Error executing Lua callback: ...nvim-highlight-colors/lua/nvim-highlight-colors/init.lua:85: Invalid window id: -1

stack traceback:
    [C]: in function 'get_visible_rows_by_buffer_id'
    ...nvim-highlight-colors/lua/nvim-highlight-colors/init.lua:85: in function 'turn_on'
    ...nvim-highlight-colors/lua/nvim-highlight-colors/init.lua:141: in function <...nvim-highlight-colors/lua/nvim-highlight-colors/init.lua:138>
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    /Users/ningwang/.config/nvim/lua/plugins/colors.lua:85: in function </Users/ningwang/.config/nvim/lua/plugins/colors.lua:85>
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    /Users/ningwang/.config/nvim/lua/plugins/colors.lua:182: in function 

The error is caused by one of my autocmd to enable your plugin once I have changed colorscheme, which saves me from manually enabling it again. This used to work fine before the recent updates:

vim.api.nvim_create_autocmd({ 'User' }, {
  group = curfile_augroup,
  pattern = 'PostColorScheme',
  callback = function () vim.cmd('HighlightColors On') end
})
brenoprata10 commented 9 months ago

Thanks for reporting! Should be fixed now. Cuold you give it a try? Feel free to reopen the issue if it's still happening 😄

nyngwang commented 9 months ago

@brenoprata10 Lol, I just completed my PR and fixed it too. Can confirm that it works :) Thank you for the swift fix!

brenoprata10 commented 9 months ago

I just found another bug case with similar cause, just pushed a fix, please update your plugin again :p

nyngwang commented 9 months ago

Yeah, it's always tricky to solve those edge cases where -1,nil or something that is passed into those neovim APIs.

update: can confirm that the new one works.