kevinhwang91 / nvim-ufo

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

Index out of bounds when closing some folds #174

Closed KneeCapStealer closed 8 months ago

KneeCapStealer commented 8 months ago

Neovim version (nvim -v | head -n1)

NVIM v0.9.4 Build type: RelWithDebInfo LuaJIT 2.1.1696883897

Operating system/version

Windows 11 Home 10.0.22621

How to reproduce the issue

cat plugins/ufo.lua

return {
    'kevinhwang91/nvim-ufo',
    main = 'ufo',
    name = 'ufo',
    dependencies = {
        'kevinhwang91/promise-async',
    },
    config = function()
        vim.o.foldcolumn = '1' -- '0' is not bad
        vim.o.foldlevel = 99 -- Using ufo provider need a large value, feel free to decrease the value
        vim.o.foldlevelstart = 99
        vim.o.foldenable = true
        vim.opt.fillchars:append(
            [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
        )

        require('ufo').setup({
            close_fold_kinds = { 'imports', 'comment' },
            preview = {
                mappings = {
                    scrollU = '<C-u>',
                    scrollD = '<C-d>',
                    jumpTop = '[',
                    jumpBot = ']',
                },
            },
            provider_selector = function(bufnr, filetype, buftype)
                return { 'lsp', 'indent' }
            end,
            fold_virt_text_handler = function(
                virtText,
                lnum,
                endLnum,
                width,
                truncate
            )
                local newVirtText = {}
                local suffix = (' 󰁂 %d '):format(endLnum - lnum)
                local sufWidth = vim.fn.strdisplaywidth(suffix)
                local targetWidth = width - sufWidth
                local curWidth = 0
                for _, chunk in ipairs(virtText) do
                    local chunkText = chunk[1]
                    local chunkWidth = vim.fn.strdisplaywidth(chunkText)
                    if targetWidth > curWidth + chunkWidth then
                        table.insert(newVirtText, chunk)
                    else
                        chunkText = truncate(chunkText, targetWidth - curWidth)
                        local hlGroup = chunk[2]
                        table.insert(newVirtText, { chunkText, hlGroup })
                        chunkWidth = vim.fn.strdisplaywidth(chunkText)
                        -- str width returned from truncate() may less than 2nd argument, need padding
                        if curWidth + chunkWidth < targetWidth then
                            suffix = suffix
                                .. (' '):rep(
                                    targetWidth - curWidth - chunkWidth
                                )
                        end
                        break
                    end
                    curWidth = curWidth + chunkWidth
                end
                table.insert(newVirtText, { suffix, 'MoreMsg' })
                return newVirtText
            end,
        })
    end,
}

Other plugins:

Reproduce

  1. Open any file
  2. Press za to close a fold (The error doesn't come if you do it on a lua return {})
  3. Instead of showing correct fold text a 0 is displayed at fold
  4. Move away from the fold, change mode, or unfold again
  5. Assert in ufo/models/buffer.lua line 217 appear

Expected behavior

Works:

It accually works when i use it on a return { } in lua. And this is how i expect it to work in all other cases as well: (Extra lines at the top to show errors in later photos)

Image

image

Actual behavior

Instead of displaying proper fold text it shows a 0 instead and an error is displayed. Besides this the folding works fine.

Images:

Close fold with za and move cursor:

image

Close fold and open fold with za

image

KneeCapStealer commented 8 months ago

I accually have a GitHub Repo with my nvim config if that is useful

arminveres commented 8 months ago

Same here, has been like this for a few weeks now, on nightly and on 0.9.4.

kevinhwang91 commented 8 months ago

Provide a mini config and reproduce the issue, otherwise debug yourself. Maybe the log file can give you some information.

  1. Execute tail -f ~/.cache/nvim/ufo.log in a shell;
  2. Run UFO_LOG=debug nvim;
arminveres commented 7 months ago

@kevinhwang91 Well, I will try to provide a minimal setup once I found some time, but basically I followed you setup guide for treesitter. The log says nothing, except periodically printing for each error message that appears inside neovim [23-11-11 15:40:21] [DEBUG] decorator.lua:77 : folded lnums: { 5 } didOpen: false, and the error in neovim being:

Error in decoration provider ufo.end:
Error executing lua: ....local/share/nvim/lazy/nvim-ufo/lua/ufo/model/buffer.lua:217: index out of bounds
stack traceback:
    [C]: in function 'assert'
    ....local/share/nvim/lazy/nvim-ufo/lua/ufo/model/buffer.lua:217: in function 'lines'
    ...es/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:97: in function 'winCall'
    ...es/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:74: in function <...es/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:65>
kevinhwang91 commented 7 months ago

@kevinhwang91 Well, I will try to provide a minimal setup once I found some time, but basically I followed you setup guide for treesitter. The log says nothing, except periodically printing for each error message that appears inside neovim [23-11-11 15:40:21] [DEBUG] decorator.lua:77 : folded lnums: { 5 } didOpen: false, and the error in neovim being:

Error in decoration provider ufo.end:
Error executing lua: ....local/share/nvim/lazy/nvim-ufo/lua/ufo/model/buffer.lua:217: index out of bounds
stack traceback:
  [C]: in function 'assert'
  ....local/share/nvim/lazy/nvim-ufo/lua/ufo/model/buffer.lua:217: in function 'lines'
  ...es/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:97: in function 'winCall'
  ...es/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:74: in function <...es/.local/share/nvim/lazy/nvim-ufo/lua/ufo/decorator.lua:65>

Look like ufo doesn't attach your buffer, UFO_LOG=trace nvim may help, ufo will attach buffer in BufEnter event.

arminveres commented 7 months ago

Look like ufo doesn't attach your buffer, UFO_LOG=trace nvim may help, ufo will attach buffer in BufEnter event.

Ah dang it, you are right, I was not letting UFO attach at the right event with Lazy.nvim. Thanks!

KneeCapStealer commented 7 months ago

Ah dang it, you are right, I was not letting UFO attach at the right event with Lazy.nvim. Thanks!

I didn't have a lot of time (and kinda forgot) to check up on this issue. Thank you for writing your fix, this worked for me as well!