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

cursor get stucked when type zm to fold all in a file which lines over 10k #224

Open ZG-N1 opened 1 month ago

ZG-N1 commented 1 month ago

Neovim version (nvim -v | head -n1)

NVIM v0.9.2

Operating system/version

win11

How to reproduce the issue

cat mini.lua

-- Use Vim packages install the plugin, also work with some plugins manager such as packer.nvim
    use({
        "kevinhwang91/nvim-ufo",
        requires = {
            "kevinhwang91/promise-async",
        },
    })

-- Setting
local vim = vim
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

local 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

-- Option 3: treesitter as a main provider instead
-- Only depend on `nvim-treesitter/queries/filetype/folds.scm`,
-- performance and stability are better than `foldmethod=nvim_treesitter#foldexpr()`
require("ufo").setup({
    open_fold_hl_timeout = 0,
    close_fold_kinds_for_ft = { "imports", "comment" },
    preview = {
        win_config = {
            border = { "", "─", "", "", "", "─", "", "" },
            winhighlight = "Normal:Folded",
            winblend = 0,
        },
        mappings = {
            scrollU = "<C-u>",
            scrollD = "<C-d>",
            jumpTop = "[",
            jumpBot = "]",
        },
    },
    provider_selector = function(bufnr, filetype, buftype)
        return { "treesitter", "ident" }
    end,
    fold_virt_text_handler = handler,
})
--
local bufnr = vim.api.nvim_get_current_buf()
require("ufo").setFoldVirtTextHandler(bufnr, handler)

-- keymaps
vim.keymap.set("n", "zr", require("ufo").openAllFolds)
vim.keymap.set("n", "zm", require("ufo").closeAllFolds)

nvim --clean +'so mini.lua'

  1. create a javascript file that contains lots of codes which is over 10k lines
  2. type zm to close all folds
  3. the problem appear (cursor is get stuck and can't get anywhere for a long time, can't do anything actually)
  4. i have tried increment fold lines step by step, cursor becomes slowly when folded lines over 1k, and got stuck when over 10k ...

Expected behavior

cursor should move easyly and timely as usual

Actual behavior

cursor stuck for a long time (about >1 minute). it is annoy, how can i fix this

kevinhwang91 commented 1 month ago

where's your 10k code?

ZG-N1 commented 1 month ago

where's your 10k code?

javascript.txt it's here. sorry for my carelessness.

kevinhwang91 commented 1 month ago

can't reproduce it, maybe your some plugins are the culprit.

ZG-N1 commented 1 month ago

Thank you for your hint, I have test my all plugins.finnally, i find the culprit plugin. when i disable the plugin called "nvim-lspconfig", the ufo works perfectly. but i don't know how to enable lspconfig correctlly. after all thanks.

kevinhwang91 commented 1 month ago

It may help if you upgraded your neovim.