RRethy / vim-illuminate

illuminate.vim - (Neo)Vim plugin for automatically highlighting other uses of the word under the cursor using either LSP, Tree-sitter, or regex matching.
2.14k stars 47 forks source link

Invalid buffer id `nvim_buf_clear_namespace` #100

Closed aspeddro closed 2 years ago

aspeddro commented 2 years ago

Describe the bug

I have a <c-w> mapping for deleting a buffer. When I press multiple times, closing multiple buffers the following error is thrown

Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1508: Invalid buffer id: 178
stack traceback:
        [C]: in function 'nvim_buf_clear_namespace'
        /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1508: in function 'buf_clear_references'
        ...site/pack/packer/start/vim-illuminate/lua/illuminate.lua:65: in function 'fn'
        vim.lua:415: in function 'cb'
        vim.lua:285: in function <vim.lua:285>

To Reproduce

Expected behavior

Screenshots image

Additional context

M.close = function()
  local bufnr = vim.api.nvim_get_current_buf()
  local winr = vim.api.nvim_get_current_win()

  local buffers = vim.tbl_filter(function(buf)
    return vim.fn.buflisted(buf) == 1
  end, vim.api.nvim_list_bufs())

  local windows = vim.tbl_filter(function(win)
    return vim.api.nvim_win_get_buf(win) == bufnr
  end, vim.api.nvim_list_wins())

  local is_split = #vim.tbl_filter(function(buf)
    return vim.fn.bufwinid(buf) ~= -1
  end, buffers) > 1

  if #buffers > 1 then
    for i, v in ipairs(buffers) do
      if v == bufnr then
        local next_buffer = buffers[i % #buffers + 1]
        for _, win in ipairs(windows) do
          vim.api.nvim_win_set_buf(win, next_buffer)
        end
      end
    end
  end

  if vim.fn.buflisted(bufnr) == 1 then
    vim.api.nvim_buf_delete(bufnr, { force = true })
  end

  if is_split then
    vim.api.nvim_win_close(winr, true)
  end
end