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.23k stars 53 forks source link

Occasional error on creating a split window #185

Closed andis-sprinkis closed 1 year ago

andis-sprinkis commented 1 year ago

Describe the bug I occasionally get this same error when creating a populated split window by seemingly any command.

        Error detected while processing CursorMoved Autocommands for "<buffer=3>":
        E5108: Error executing lua /usr/share/nvim/runtime/lua/vim/lsp/util.lua:138: index out of range
        stack traceback:
                [C]: in function 'str_utfindex'
                /usr/share/nvim/runtime/lua/vim/lsp/util.lua:138: in function '_str_utfindex_enc'
                /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1951: in function 'make_position_param'
                /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1968: in function 'make_position_params'
                /usr/share/nvim/runtime/lua/vim/lsp/buf.lua:577: in function 'document_highlight'
                ....local/share/nvim/lazy/vim-illuminate/lua/illuminate.lua:151: in function 'on_cursor_moved'
                [string ":lua"]:1: in main chunk
        Press ENTER or type command to continue

To Reproduce Steps to reproduce the behavior (include minimal init.vim or .vimrc):

  1. Set up nvim-lspconfig and nvim-treesitter, also vim-fugitive and fzf-lua
  2. While having any code files open (Shell, Javascript, Python) create a populated split either by
    • calling lua vim.diagnostic.setloclist() (assigned to <leader>q)
    • by selecting some search results in fzf-lua overlay dialogue, and pressing Enter, thereby passing them to loclist split
    • calling vim-fugitive :Gstatus command (this one is not a loclist)

Note Omitting a minimal init.vim/init.lua/.vimrc will likely result in the issue being closed without explanation.

Without investigating vim-illuminate code and knowing the issue, I do not know what would be the minimum config required to reproduce this, as it only happens on occasion (3-5 times a day).

This is my full config at the moment of writing this

Output from :IlluminateDebug

buf_should_illuminate 2 false
config {
  case_insensitive_regex = false,
  delay = 100,
  filetype_overrides = {},
  filetypes_allowlist = {},
  filetypes_denylist = { "dirbuf", "dirvish", "fugitive" },
  min_count_to_highlight = 1,
  modes_allowlist = {},
  modes_denylist = {},
  providers = { "lsp", "treesitter", "regex" },
  providers_regex_syntax_allowlist = {},
  providers_regex_syntax_denylist = {},
  under_cursor = true
}
started true
provider table: 0x7f55c43d0b68 lsp
`termguicolors` true

Expected behavior A vertical split window to be created and populated by vim diagnostics loclist content or by respective plugin (vim-fugitive, fzf-lua) content.

Instead of:

This error sowing up in the status area

        Error detected while processing CursorMoved Autocommands for "<buffer=3>":
        E5108: Error executing lua /usr/share/nvim/runtime/lua/vim/lsp/util.lua:138: index out of range
        stack traceback:
                [C]: in function 'str_utfindex'
                /usr/share/nvim/runtime/lua/vim/lsp/util.lua:138: in function '_str_utfindex_enc'
                /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1951: in function 'make_position_param'
                /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1968: in function 'make_position_params'
                /usr/share/nvim/runtime/lua/vim/lsp/buf.lua:577: in function 'document_highlight'
                ....local/share/nvim/lazy/vim-illuminate/lua/illuminate.lua:151: in function 'on_cursor_moved'
                [string ":lua"]:1: in main chunk
        Press ENTER or type command to continue

And due to error the respective split window gets created, but populated only by the previously focused open file window buffer, rather than the LSP diagnostics loclist content or plugin content. EDIT: that was incorrect, the split window content does get populated with the LSP debug / plugin content, even with the error occurring.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1697887905

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info
andis-sprinkis commented 1 year ago

Looking into this further now.

I found a way to explicitly trigger it on my setup.

I create and open a file

#!/usr/bin/env sh
\\\

echo "a"

I make sure that the last line in the text is at over half the vertical height of the window.

I add and remove slash on line 2. Then I press Esc and call :split.

Then the error occurs.

1699709749 1699709759

It seems to occur only when the newly creates split at the bottom obfuscates one of the lines that would be processed by the highlighting, in this case echo "a", where echo would be highlighted.

I'll disable the plugins and options customization to see the minimal setup I can replicate this on.

andis-sprinkis commented 1 year ago

Minimal config. has arrived

vim.opt.splitkeep = 'screen' -- bug replication: could not replicate the bug without setting this exact value
vim.opt.splitbelow = true -- bug replication: to obfuscate the last part of the open file via :split

vim.g.loaded_netrwPlugin = 0
vim.opt.backup = false
vim.opt.swapfile = false
vim.opt.writebackup = false

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"

if not vim.loop.fs_stat(lazypath) then
  if vim.fn.confirm("Download and initialize the configured plugins?", "&Yes\n&No", 2) == 2 then return end
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath })
end

vim.opt.rtp:prepend(lazypath)

require("lazy").setup(
  {
    {
      'williamboman/mason.nvim',
      config = function()
        local lsp_servers = {
          'bashls', --  bug replication: issue does not occur if no language server is initialized
        }

        local on_attach = function(client, bufnr)
          require('illuminate').on_attach(client)
        end -- bug replication: LEAVING IN THIS V1 METHOD CAUSES THE ERR. ON BUF. SPLIT W/ THIS CFG.

        local function make_config()
          return {
            on_attach = on_attach,
          }
        end

        require("mason").setup()

        require("mason-lspconfig").setup({ ensure_installed = lsp_servers })

        local lspconfig = require("lspconfig")

        require("mason-lspconfig").setup_handlers({
          function(server_name)
            lspconfig[server_name].setup(make_config())
          end,
        })

      end,
      dependencies = {
        'neovim/nvim-lspconfig',
        'williamboman/mason-lspconfig.nvim',
        'RRethy/vim-illuminate',
      }
    }
  },
  {
    performance = {
      rtp = {
        disabled_plugins = {
          "gzip",
          "matchparen",
          "netrwPlugin",
          "rplugin",
          "spellfile",
          "tarPlugin",
          "tohtml",
          "tutor",
          "zipPlugin",
        },
      },
    }
  }
)
andis-sprinkis commented 1 year ago

Not calling the V1 method require('illuminate').on_attach(client) mitigates, and, I guess, fixes this issue on the user side.

RRethy commented 1 year ago

Hey, require('illuminate').on_attach(client) isn't supported anymore so this isn't going to get a direct fix. Since your most recent comment says it's fixed I'm going to close the issue. Behaviour should remain the same (and in many cases better) with the newer setup instead of the old on_attach.