kevinhwang91 / nvim-ufo

Not UFO in the sky, but an ultra fold in Neovim.
BSD 3-Clause "New" or "Revised" License
2.23k stars 44 forks source link

Index out of bounds #47

Closed rhcher closed 2 years ago

rhcher commented 2 years ago

Neovim version (nvim -v | head -n1)

NVIM v0.8.0-dev+608-g778541067-dirty

Operating system/version

wsl2 Arch

How to reproduce the issue

  1. cd neovim && nvim runtime/lua/vim/lsp/codelens.lua
  2. input za to trigger a fold

note thant it must use lua language server to reproduce.

Expected behavior

Get correct folded result

Actual behavior

There is a 0 with some buf info in screen. image

kevinhwang91 commented 2 years ago

can't reproduce. need mini config.

kevinhwang91 commented 2 years ago

should fix now.

rhcher commented 2 years ago

The bug still exists for me, but I can't reproduce it with mini config, so it is related to my config, I will look into it tomorrow.

kevinhwang91 commented 2 years ago
--- a/lua/ufo/model/buffer.lua
+++ b/lua/ufo/model/buffer.lua
@@ -172,6 +172,9 @@ function Buffer:lines(lnum, endLnum)
         self._q = {}
     end
     local hunks, cnt = self:handleChanged()
+    if self._lineCount < lnum then
+        require('ufo.lib.log').error(self._lineCount, lnum, debug.traceback('?????'))
+    end
     assert(self._lineCount >= lnum, 'index out of bounds')
     endLnum = endLnum and endLnum or lnum
     if endLnum < 0 then

tail -f ~/.cache/nvim/ufo.log may help.

rubenmate commented 2 years ago

The bug still exist for me, but I can't reproduce with mini config, so it related my personal config, I will look into tomorrow.

Keep us posted, this issue is happening to me also.

kevinhwang91 commented 2 years ago

The bug still exist for me, but I can't reproduce with mini config, so it related my personal config, I will look into tomorrow.

Keep us posted, this issues is happening to me also.

More info is better.

rubenmate commented 2 years ago
--- a/lua/ufo/model/buffer.lua
+++ b/lua/ufo/model/buffer.lua
@@ -172,6 +172,9 @@ function Buffer:lines(lnum, endLnum)
         self._q = {}
     end
     local hunks, cnt = self:handleChanged()
+    if self._lineCount < lnum then
+        require('ufo.lib.log').error(self._lineCount, lnum, debug.traceback('?????'))
+    end
     assert(self._lineCount >= lnum, 'index out of bounds')
     endLnum = endLnum and endLnum or lnum
     if endLnum < 0 then

tail -f ~/.cache/nvim/ufo.log may help.

The bug still exist for me, but I can't reproduce with mini config, so it related my personal config, I will look into tomorrow.

Keep us posted, this issues is happening to me also.

More info is better.

Sorry, was looking into the log but seems like that file is missing for me. It doesn't happen every time. If I open and close Neovim chances are it will work or not.

Sorry not giving more info, let me look into it with a minimal config.

rhcher commented 2 years ago

accord my test, it because 6c5b905f1dad08b4b2de5be1458a9ddcbb9e4325 , I just reuse vim.api.nvim_buf_get_lines and bug is gone.

kevinhwang91 commented 2 years ago

It's weird. Could you add the above debug snippet and provide the log?

rhcher commented 2 years ago

I can provide a log, but I still can't reproduce with mini config, so I can only provide debug log with my config, is it make sense?

kevinhwang91 commented 2 years ago

Yes, please provide the actual line count for the buffer. self._lineCount may get wrong.

rhcher commented 2 years ago
[22-07-14 13:39:41] [ERROR] buffer.lua:173 : 1 172 ?????
stack traceback:
    ...site/pack/packer/start/nvim-ufo/lua/ufo/model/buffer.lua:173: in function 'lines'
    ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/main.lua:124: in function <...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/main.lua:120>
[22-07-14 13:39:41] [ERROR] buffer.lua:173 : 1 172 ?????
stack traceback:
    ...site/pack/packer/start/nvim-ufo/lua/ufo/model/buffer.lua:173: in function 'lines'
    ...im/site/pack/packer/start/nvim-ufo/lua/ufo/decorator.lua:88: in function 'winCall'
    ...im/site/pack/packer/start/nvim-ufo/lua/ufo/decorator.lua:68: in function <...im/site/pack/packer/start/nvim-ufo/lua/ufo/decorator.lua:58>
[22-07-14 13:39:42] [ERROR] buffer.lua:173 : 1 172 ?????
stack traceback:
    ...site/pack/packer/start/nvim-ufo/lua/ufo/model/buffer.lua:173: in function 'lines'
    ...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/main.lua:124: in function <...re/nvim/site/pack/packer/start/nvim-ufo/lua/ufo/main.lua:120>
[22-07-14 13:39:42] [ERROR] buffer.lua:173 : 1 172 ?????
stack traceback:
    ...site/pack/packer/start/nvim-ufo/lua/ufo/model/buffer.lua:173: in function 'lines'
    ...im/site/pack/packer/start/nvim-ufo/lua/ufo/decorator.lua:88: in function 'winCall'
    ...im/site/pack/packer/start/nvim-ufo/lua/ufo/decorator.lua:68: in function <...im/site/pack/packer/start/nvim-ufo/lua/ufo/decorator.lua:58>

buffer.lua have 200 lines

kevinhwang91 commented 2 years ago

Please update the code, maybe the first buffer attached issue.

lua =require'ufo.bufmanager'.buffers[1].attached should be true after the new commiet.

rubenmate commented 2 years ago

Now it works like a charm! Thanks for your work

meijieru commented 1 year ago

Encounter this issue again, here is the output of :lua =require'ufo.bufmanager'.buffers

{
  [3] = {
    _changedtick = 1,
    _lines = { false },
    attached = true,
    bt = "",
    bufnr = 3,
    ft = "lua",
    <metatable> = <1>{
      __index = <table 1>,
      attach = <function 1>,
      buftype = <function 2>,
      buildMissingHunk = <function 3>,
      changedtick = <function 4>,
      dispose = <function 5>,
      filetype = <function 6>,
      handleLinesChanged = <function 7>,
      lineCount = <function 8>,
      lines = <function 9>,
      new = <function 10>,
      reload = <function 11>,
      sliceLines = <function 12>
    }
  }
}

Seems the bufnr may be any value.

kevinhwang91 commented 1 year ago

Encounter this issue again, here is the output of :lua =require'ufo.bufmanager'.buffers

{
  [3] = {
    _changedtick = 1,
    _lines = { false },
    attached = true,
    bt = "",
    bufnr = 3,
    ft = "lua",
    <metatable> = <1>{
      __index = <table 1>,
      attach = <function 1>,
      buftype = <function 2>,
      buildMissingHunk = <function 3>,
      changedtick = <function 4>,
      dispose = <function 5>,
      filetype = <function 6>,
      handleLinesChanged = <function 7>,
      lineCount = <function 8>,
      lines = <function 9>,
      new = <function 10>,
      reload = <function 11>,
      sliceLines = <function 12>
    }
  }
}

Seems the bufnr may be any value.

buffer content? how to reproduce it?

meijieru commented 1 year ago

Buffer content. Currently no minimal vimrc as it seems conflict with other plugins. Any other debug instructions?

local M = {
  _diagnostics_active = true,
}

local Log = require "core.log"

function M.root_dir(fname)
  return require("lspconfig.util").root_pattern(unpack(myvim.root_markers))(fname) or vim.fn.getcwd()
end

function M.lsp_config(server_name, automatic_servers_installation)
  automatic_servers_installation = automatic_servers_installation or false

  local lsp_installer_servers = require "nvim-lsp-installer.servers"
  local server_available, requested_server = lsp_installer_servers.get_server(server_name)
  if server_available then
    requested_server:on_ready(function()
      local opts = { root_dir = M.root_dir }
      requested_server:setup(opts)
    end)
    if (not requested_server:is_installed()) and automatic_servers_installation then
      requested_server:install()
    end
  else
    Log:info(server_name .. " not available")
  end
end

function M.toggle_diagnostics()
  if M._diagnostics_active then
    vim.diagnostic.disable(0)
    M._diagnostics_active = false
  else
    vim.diagnostic.enable(0)
    M._diagnostics_active = true
  end
end

return M
kevinhwang91 commented 1 year ago

Currently no minimal vimrc as it seems conflict with other plugins.

Find them out and reproduce the issue. Otherwise, even if I want to help you, there is nothing I can do.

meijieru commented 1 year ago

Problem fixed by removing lazy load.

kevinhwang91 commented 1 year ago

Problem fixed by removing lazy load.

Did you load ufo when there are multiple tabpages? And the buffers out of bounds are in non-current tagepage?

meijieru commented 1 year ago

I have only one tabpage. Some context

editor["kevinhwang91/nvim-ufo"] = {
  -- NOTE(meijieru): cause problem.
  -- event = { "BufReadPre" },
  requires = "kevinhwang91/promise-async",
  config = conf.ufo,
  disable = not myvim.plugins.ufo.active,
}
kevinhwang91 commented 1 year ago

Lol, use BufRead instead. BufReadPre is buggy for buffers. Like nvim_buf_line_count always return 1.

meijieru commented 1 year ago

Using BufRead solves the problem. Thx!

catgoose commented 1 year ago

With lazy.nvim, BufRead is not working for me, I had to use event = "VeryLazy"

kareem-abdul commented 9 months ago

Lol, use BufRead instead. BufReadPre is buggy for buffers. Like nvim_buf_line_count always return 1.

this worked for me as well.