kevinhwang91 / nvim-ufo

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

auto-unfolding when executing :w in some language like golang and rust #82

Closed OliverChao closed 1 year ago

OliverChao commented 1 year ago

Neovim version (nvim -v | head -n1)

NVIM v0.7.2

Operating system/version

Ubuntu 22.04

How to reproduce the issue

Some folds will auto-unfold when I save buffers. This occurs in golang and rust files, but the txt files are always fine. I don't know where I break up the ufo configurations, and HOW can I fix it? Thanks..... I am using the default ufo configurations.

https://user-images.githubusercontent.com/39361033/191886798-71be1b74-f7f6-494a-81b0-aa8b349e902e.mp4

Expected behavior

keep foldings when I save buffers.

Actual behavior

auto unfolding when I run :w command.

kevinhwang91 commented 1 year ago

Check out your foldlevel.

OliverChao commented 1 year ago

Check out your foldlevel.

I set foldlevel=99; Here is my ufo configurations:

vim.o.foldcolumn = '1'
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.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
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

-- global handler
require('ufo').setup({
    fold_virt_text_handler = handler,
    provider_selector = function(bufnr, filetype, buftype)
        return {'treesitter', 'indent'}
    end
})

-- buffer scope handler
-- will override global handler if it is existed
local bufnr = vim.api.nvim_get_current_buf()
require('ufo').setFoldVirtTextHandler(bufnr, handler)

And in rust, the foldlevel is also 99.

https://user-images.githubusercontent.com/39361033/191915040-bf201b7d-283f-426e-8ed5-595d930a7307.mp4

kevinhwang91 commented 1 year ago

No idea why this happens. Please use a mini config.

ysl2 commented 1 year ago

If you use lazy.nvim to manage your plugins, you might want to put these variables into init keyword:

{ 'kevinhwang91/nvim-ufo', dependencies = 'kevinhwang91/promise-async',
    init = 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
    end }

And you should check if other plugins conflict with the setting above.

catgoose commented 1 year ago

I have to use lazy = false in addition to the above

rohit-kumar-j commented 1 year ago

Even with the above suggestions, there is no change in this behaviour.