kevinhwang91 / nvim-ufo

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

automatically folds after deleting newlines between folds #94

Closed ipsod closed 1 year ago

ipsod commented 1 year ago

Neovim version (nvim -v | head -n1)

NVIM v0.9.0-dev

Operating system/version

linux mint 21

How to reproduce the issue

create text file with multiple levels of folds:

test
    row
        sub
        sub
    row
        sub
        sub

fold to hide sub, but show row (set foldlevel=1, I think)

insert or delete a blank newline between the two row folds. Both inserting and deleting cause it.

editor will now automatically (and undesirably) fold all the way, and hide rows

Expected behavior

fold level should not change because of modification of text

Actual behavior

The text you're editing gets folded because you edited the text. Especially troublesome if it folds, then you end up deleting N folded lines instead of the single unfolded line you meant to.

kevinhwang91 commented 1 year ago

Using a provider of ufo, must set a large value for foldlevel, this is the limitation of foldmethod=manual. A small value may close fold automatically if the fold ranges updated.

https://github.com/kevinhwang91/nvim-ufo/issues/7 https://github.com/kevinhwang91/nvim-ufo/issues/57 https://github.com/kevinhwang91/nvim-ufo/issues/89

ipsod commented 1 year ago

Ah, I see. Sorry about that.

Is the config here supposed to restore normal functionality to zm, zM, zr, and zR? https://github.com/kevinhwang91/nvim-ufo#customize-configuration

I copied it directly, and zm and zr seem to have the same behavior as zM and zR.

kevinhwang91 commented 1 year ago
  1. number + zm.
  2. zr is the same as zM if the kind is excluded in close_fold_kinds.
ipsod commented 1 year ago

Is there a way to just increment/deincrement mock-indent-level, to simulate standard behavior?

kevinhwang91 commented 1 year ago

No, foldlevel should keep a large value. Must maintain your_foldlevel by yourself. Need to write a script.

vim.api.nvim_create_autocmd({'WinNew', 'VimEnter'}, {
    pattern = '*',
    callback = function()
        vim.w.my_foldlevel = 20
    end
})

vim.keymap.set('n', 'zr', function()
    vim.w.my_foldlevel = vim.w.my_foldlevel - 1
    require('ufo').closeFoldsWith(vim.w.my_foldlevel)
end)

vim.keymap.set('n', 'zm', function()
    vim.w.my_foldlevel = vim.w.my_foldlevel + 1
    require('ufo').closeFoldsWith(vim.w.my_foldlevel)
end)
ipsod commented 1 year ago

This seems to be working well. Thanks for the help!

    vim.api.nvim_create_autocmd({'WinNew', 'VimEnter'}, {
        pattern = '*',
        callback = function()
            vim.w.my_foldlevel = 1
        end
    })

    vim.keymap.set('n', 'zM', function()
        vim.w.my_foldlevel = 0
        require('ufo').closeFoldsWith(vim.w.my_foldlevel)
    end)

    vim.keymap.set('n', 'zR', function()
        vim.w.my_foldlevel = 5
        require('ufo').closeFoldsWith(vim.w.my_foldlevel)
    end)

    vim.keymap.set('n', 'zr', function()
        vim.w.my_foldlevel = vim.w.my_foldlevel + 1
        require('ufo').closeFoldsWith(vim.w.my_foldlevel)
    end)

    vim.keymap.set('n', 'zm', function()
        vim.w.my_foldlevel = math.max(vim.w.my_foldlevel - 1, 0)

        require('ufo').closeFoldsWith(vim.w.my_foldlevel)
    end)