kevinhwang91 / nvim-ufo

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

Index out of bounds #235

Open BrunoKrugel opened 1 month ago

BrunoKrugel commented 1 month ago

Neovim version (nvim -v | head -n1)

NVIM v0.10.0

Operating system/version

macOS M1

How to reproduce the issue

cat mini.lua

-- Use Vim packages install the plugin, also work with some plugins manager such as packer.nvim
vim.o.packpath = '~/.local/share/nvim/site'
vim.cmd('packadd promise-async')
vim.cmd('packadd nvim-ufo')

-- Setting
vim.o.foldcolumn = 'auto'
vim.o.foldlevel = 99
vim.o.foldlevelstart = -1
vim.o.foldenable = true

local ufo = require('ufo')
ufo.setup({
  close_fold_kinds_for_ft = {
    default = { "imports" },
  },
  open_fold_hl_timeout = 0,
  preview = {
    win_config = {
      border = "rounded",
      winblend = 0,
    },
  },
  provider_selector = function(_, filetype, buftype)
    -- use nested markdown folding
    if filetype == "markdown" then
      return ""
    end

    -- return ftMap[filetype] or { "treesitter", "indent" }
    -- return { "treesitter", "indent" }
    local function handleFallbackException(bufnr, err, providerName)
      if type(err) == "string" and err:match "UfoFallbackException" then
        return require("ufo").getFolds(bufnr, providerName)
      else
        return require("promise").reject(err)
      end
    end

    -- only use indent until a file is opened
    return (filetype == "" or buftype == "nofile") and "indent"
      or function(bufnr)
        return require("ufo")
          .getFolds(bufnr, "lsp")
          :catch(function(err)
            return handleFallbackException(bufnr, err, "treesitter")
          end)
          :catch(function(err)
            return handleFallbackException(bufnr, err, "indent")
          end)
      end
  end,
})
vim.keymap.set('n', 'zR', ufo.openAllFolds)
vim.keymap.set('n', 'zM', ufo.closeAllFolds)

nvim --clean +'so mini.lua'

  1. Open a go file

image

Expected behavior

The code above should open a Go file with the imports folded.

When UFO works: image

Actual behavior

The code works most of the time, but sometimes when I open a file the folded code has a 0 instead of the default text and I get a lot of index out of bounds error.

Folded code:

0
}

Error

Error in decoration provider ufo.end:
Error executing lua: ....local/share/nvim/lazy/nvim-ufo/lua/ufo/model/buffer.lua:227: index out of bounds
stack traceback:
    [C]: in function 'error'
    ...el/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:143: in function <...el/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:140

image

kevinhwang91 commented 1 month ago

Have you searched your title from the repo issue?

BrunoKrugel commented 1 month ago

Have you searched your title from the repo issue?

Sorry if missed some information, but yes.

output from UFO_LOG=debug:

[24-07-18 11:13:14] [INFO] manager.lua:202 : apply fold rowPairs: {}
[24-07-18 11:13:38] [DEBUG] init.lua:125 : attach bufnr: 634
[24-07-18 11:13:39] [DEBUG] init.lua:125 : attach bufnr: 634
[24-07-18 11:13:39] [INFO] init.lua:86 : providers: { <function 1> }
[24-07-18 11:13:39] [DEBUG] init.lua:53 : requestFoldingRange({ <function 1> }, 634) has elapsed: 0ms
[24-07-18 11:13:39] [INFO] init.lua:93 : selected provider: external
[24-07-18 11:15:05] [DEBUG] init.lua:125 : attach bufnr: 1
[24-07-18 11:15:06] [INFO] init.lua:86 : providers: { <function 1> }
[24-07-18 11:15:07] [DEBUG] init.lua:37 : using nvim as a lsp provider
[24-07-18 11:15:08] [DEBUG] init.lua:53 : requestFoldingRange({ <function 1> }, 1) has elapsed: 2020ms
[24-07-18 11:15:08] [INFO] init.lua:93 : selected provider: external
[24-07-18 11:15:08] [INFO] manager.lua:201 : apply fold ranges: { {
    endLine = 14,
    startLine = 5
  }, {
    endLine = 13,
    startLine = 6
  }, {
    endLine = 12,
    startLine = 7
  }, {
    endLine = 31,
    startLine = 17
  }, {
    endLine = 29,
    startLine = 19
  }, {
    endLine = 28,
    startLine = 21
  }, {
    endLine = 27,
    startLine = 22
  }, {
    endLine = 26,
    startLine = 23
  }, {
    endLine = 35,
    startLine = 34
  } }
[24-07-18 11:15:08] [INFO] manager.lua:202 : apply fold rowPairs: {}
[24-07-18 11:15:14] [DEBUG] init.lua:125 : attach bufnr: 2
[24-07-18 11:15:15] [INFO] init.lua:86 : providers: { "indent" }
[24-07-18 11:15:15] [DEBUG] init.lua:53 : requestFoldingRange({ "indent" }, 2) has elapsed: 3ms
[24-07-18 11:15:15] [INFO] init.lua:93 : selected provider: indent
kevinhwang91 commented 1 month ago

Have you searched your title from the repo issue?

Sorry if missed some information, but yes.

output from UFO_LOG=debug:

[24-07-18 11:13:14] [INFO] manager.lua:202 : apply fold rowPairs: {}
[24-07-18 11:13:38] [DEBUG] init.lua:125 : attach bufnr: 634
[24-07-18 11:13:39] [DEBUG] init.lua:125 : attach bufnr: 634
[24-07-18 11:13:39] [INFO] init.lua:86 : providers: { <function 1> }
[24-07-18 11:13:39] [DEBUG] init.lua:53 : requestFoldingRange({ <function 1> }, 634) has elapsed: 0ms
[24-07-18 11:13:39] [INFO] init.lua:93 : selected provider: external
[24-07-18 11:15:05] [DEBUG] init.lua:125 : attach bufnr: 1
[24-07-18 11:15:06] [INFO] init.lua:86 : providers: { <function 1> }
[24-07-18 11:15:07] [DEBUG] init.lua:37 : using nvim as a lsp provider
[24-07-18 11:15:08] [DEBUG] init.lua:53 : requestFoldingRange({ <function 1> }, 1) has elapsed: 2020ms
[24-07-18 11:15:08] [INFO] init.lua:93 : selected provider: external
[24-07-18 11:15:08] [INFO] manager.lua:201 : apply fold ranges: { {
    endLine = 14,
    startLine = 5
  }, {
    endLine = 13,
    startLine = 6
  }, {
    endLine = 12,
    startLine = 7
  }, {
    endLine = 31,
    startLine = 17
  }, {
    endLine = 29,
    startLine = 19
  }, {
    endLine = 28,
    startLine = 21
  }, {
    endLine = 27,
    startLine = 22
  }, {
    endLine = 26,
    startLine = 23
  }, {
    endLine = 35,
    startLine = 34
  } }
[24-07-18 11:15:08] [INFO] manager.lua:202 : apply fold rowPairs: {}
[24-07-18 11:15:14] [DEBUG] init.lua:125 : attach bufnr: 2
[24-07-18 11:15:15] [INFO] init.lua:86 : providers: { "indent" }
[24-07-18 11:15:15] [DEBUG] init.lua:53 : requestFoldingRange({ "indent" }, 2) has elapsed: 3ms
[24-07-18 11:15:15] [INFO] init.lua:93 : selected provider: indent

Which buffer is index out of bounds from your log? If you load ufo without lazy event, is there an issue?

BrunoKrugel commented 1 month ago

It seems that the issue doesn't happen if I use lazy=false, however, this issue started to happen in the last month because I have been using BufRead for almost a year without issues.

kevinhwang91 commented 1 month ago

Which buffer is index out of bounds from your log? What's the content? Actually, I have no idea to fix.

BrunoKrugel commented 1 month ago

In this example my buffer is the bufnr 2.

https://github.com/user-attachments/assets/b14ea5c7-4fc4-4d88-bc4f-23896162cf0c

Log:

[24-07-18 12:15:09] [DEBUG] init.lua:125 : attach bufnr: 2
[24-07-18 12:15:09] [INFO] init.lua:86 : providers: { <function 1> }
[24-07-18 12:15:11] [DEBUG] init.lua:37 : using nvim as a lsp provider
[24-07-18 12:15:11] [DEBUG] init.lua:53 : requestFoldingRange({ <function 1> }, 2) has elapsed: 1995ms
[24-07-18 12:15:11] [INFO] init.lua:93 : selected provider: external
[24-07-18 12:15:11] [INFO] manager.lua:201 : apply fold ranges: { {
    endLine = 14,
    startLine = 5
  }, {
    endLine = 13,
    startLine = 6
  }, {
    endLine = 12,
    startLine = 7
  }, {
    endLine = 31,
    startLine = 17
  }, {
    endLine = 29,
    startLine = 19
  }, {
    endLine = 28,
    startLine = 21
  }, {
    endLine = 27,
    startLine = 22
  }, {
    endLine = 26,
    startLine = 23
  }, {
    endLine = 35,
    startLine = 34
  } }
[24-07-18 12:15:11] [INFO] manager.lua:202 : apply fold rowPairs: {}
[24-07-18 12:15:13] [DEBUG] init.lua:125 : attach bufnr: 3
[24-07-18 12:15:13] [INFO] init.lua:86 : providers: { "indent" }
[24-07-18 12:15:13] [DEBUG] init.lua:53 : requestFoldingRange({ "indent" }, 3) has elapsed: 0ms
[24-07-18 12:15:13] [INFO] init.lua:93 : selected provider: indent
[24-07-18 12:15:25] [DEBUG] init.lua:125 : attach bufnr: 245
[24-07-18 12:15:25] [INFO] init.lua:86 : providers: { "indent" }
[24-07-18 12:15:25] [DEBUG] init.lua:53 : requestFoldingRange({ "indent" }, 245) has elapsed: 0ms
[24-07-18 12:15:25] [INFO] init.lua:93 : selected provider: indent
kevinhwang91 commented 1 month ago

Leave this issue here, no ability to solve it.