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

Folds Everything after Formatting #160

Closed Pandoks closed 9 months ago

Pandoks commented 9 months ago

Neovim version (nvim -v | head -n1)

0.9.2

Operating system/version

macOS 13.5.2

How to reproduce the issue

I am using lazy.nvim ufo config:

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

return {
    "kevinhwang91/nvim-ufo",
  lazy = false, -- too buggy to lazy load
    dependencies = {
        "kevinhwang91/promise-async",
        "nvim-treesitter/nvim-treesitter",
    },
    opts = {
        fold_virt_text_handler = handler,
        provider_selector = function()
            return { "treesitter", "indent" }
        end,
    },
    keys = {
        {
            "<leader>K",
            "<cmd>lua require('ufo').peekFoldedLinesUnderCursor()<cr>",
            mode = "n",
            desc = "Peek folded portion",
        },
    },
    config = function(_, opts)
        vim.o.foldlevel = 99
        vim.o.foldlevelstart = 99
        vim.o.foldenable = true
        vim.o.fillchars = "eob: ,fold: ,foldopen:,foldsep: ,foldclose:"
        require("ufo").setup(opts)
    end,
}

formatter.nvim:

return {
    "mhartington/formatter.nvim",
    event = "BufWritePre",
    keys = {
        { "<leader>F", "<cmd>Format<cr>", mode = "n", desc = "Format the file" },
    },
    config = function()
        local mason = vim.fn.stdpath("data") .. "/mason/bin/"
        local opts = {
            logging = false,
            filetype = {
                lua = {
                    require("formatter.filetypes.lua").stylua,
                    ignore_exitcode = true,
                },
                sh = {
                    require("formatter.filetypes.sh").shfmt,
                    ignore_exitcode = true,
                },
                c = {
                    require("formatter.filetypes.c").clangformat,
                    ignore_exitcode = true,
                },
                javascript = {
                    require("formatter.filetypes.javascript").prettier,
                    ignore_exitcode = true,
                },
                typescript = {
                    require("formatter.filetypes.typescript").prettier,
                    ignore_exitcode = true,
                },
                svelte = {
                    require("formatter.filetypes.svelte").prettier,
                    ignore_exitcode = true,
                },
                yaml = {
                    require("formatter.filetypes.yaml").prettier,
                    ignore_exitcode = true,
                },
                markdown = {
                    require("formatter.filetypes.markdown").prettier,
                    ignore_exitcode = true,
                },
                ["*"] = {
                    require("formatter.filetypes.any").remove_trailing_whitespace,
                    ignore_exitcode = true,
                },
            },
        }
        require("formatter").setup(opts)

        -- format after save
        vim.api.nvim_create_augroup("FormatAutogroup", { clear = true })
        vim.api.nvim_create_autocmd({ "BufWritePost" }, {
            group = "FormatAutogroup",
            command = "FormatWrite",
        })
    end,
}

Open a file. UFO folding works perfectly. Edit something. Save, which is auto formatted. Fold anything, and it folds the entire buffer. I need to reopen the buffer using :e to get the functionality working again.

Expected behavior

Normal UFO folding: 2023-09-14 00 41 02

Actual behavior

Entire buffer folding: 2023-09-14 00 41 59