kevinhwang91 / nvim-ufo

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

Folds close automatically when leaving insert mode by pressing <esc> #57

Closed mattiasbonte closed 2 years ago

mattiasbonte commented 2 years ago

Neovim version (nvim -v | head -n1)

Nvim 7

Operating system/version

Arch Linux

How to reproduce the issue


-- SETTINGS --

vim.o.foldenable = true vim.o.foldlevel = 99 -- Using ufo provider need a large value, feel free to decrease the value vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]] vim.o.foldcolumn = "0" -- changes the width of the fold column (removes the numbers as well) vim.o.foldlevelstart = -1


-- CONFIG https://github.com/kevinhwang91/nvim-ufo#advanced-configuration --

require("ufo").setup({ open_fold_hl_timeout = 100, fold_virt_text_handler = handler, -- custom text next to fold })


-- KEYMAPS --

vim.keymap.set("n", "zR", require("ufo").openAllFolds, { desc = "Open All Folds" }) vim.keymap.set("n", "zM", require("ufo").closeAllFolds, { desc = "Close All Folds" })

Expected behavior

Folds should maintain their state no matter what action I perform?

Actual behavior

In my regular javascript / typescript files, whenever I leave insert modus, or perform another action like for example delete word in normal mode, the folds automatically collapse.

ufo bug

Any idea what could trigger this? I cleared the ~/.cache/nvim/ufo.log and watched for error but no errors are produced by this action.

kevinhwang91 commented 2 years ago

setlocal foldlevel=99, switch buffer from the window with small foldlevel cause this issue.

mattiasbonte commented 2 years ago

So how do you suggest I can prevent this from happening?

If I manually enter :setlocal foldlevel=99, it does fix it for that buffer..

But it doesn't seem like a sustainable implementation if I have to manually enter that command every time I open a buffer? Can I change this in the configuration? I added this into my ufo.lua config. But it doesn't seem to solve the problem.

-- vim.o.foldlevel = 99 -- FIX: this bugs on javascript files, use the setlocal one below
vim.cmd("setlocal foldlevel=99")

Is an autocommand the solution? For example... but this interferes with a session manager that saves the fold inside a session. Each time we set the fold level locally the folds open up completely on buffer open.

vim.cmd("au BufEnter * silent! setlocal foldlevel=99")

I'm quite new at all this so it would be nice if you could send me in the right direction :). If this has been solved in another ticket i'm sorry, but I couldn't find it.

Thanks a lot!

kevinhwang91 commented 2 years ago

You should find what script changes your foldlevel for javascript buffer.

Is an autocommand the solution? For example... but this interferes with a session manager that saves the fold inside a session. Each time we set the fold level locally the folds open up completely on buffer open.

Yes, BufEnter will make it work, but I suggest you set foldlevelstart = 99

VPavliashvili commented 1 year ago

Adding this comment if someone ends up with similar problem as me. I had same issue caused by diffview.nvim which is setting foldlevel to 0 after opening DiffviewOpen or DiffviewFileHistory. In this case to fix this problem you can add these hooks into diffview.nivm config(also see this(#132) issue from their repository)

require("diffview").setup({
    enhanced_diff_hl = true,
    hooks = {
        diff_buf_read = function(bufnr) 
            vim.opt_local.foldlevel = 99
            vim.opt_local.foldenable = false
        end,
        diff_buf_win_enter = function(bufnr) 
            vim.opt_local.foldlevel = 99
            vim.opt_local.foldenable = false
        end
    }
})