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

Close all folds without unwanted autofolding/unfolding triggered by insert mode exit. #85

Closed foil-hat-guy closed 1 year ago

foil-hat-guy commented 1 year ago

Dear kevinhwang91 and nvim-ufo users,

In the issue list there are several issues about unwanted behavior of zR and zM commands: unwanted automatic folding/unfolding triggered by insert mode exit. See issues #7, #49, #57 and #62. I have the same problem and none of solutions at metioned issues works for me. So I made my own one. I made another implementation of zM command "closeAllFolds" on top of zC command witch is not causing unwanted behaviour.

New implementation of "closeAllFolds" command.

Press F9 key and the following script will walk the buffer top to bottom and invoke zC command at each line:

local function FoldEverything() local row,col = unpack(vim.api.nvim_win_get_cursor(0)) vim.cmd("normal gg") for i=1,vim.api.nvim_buf_line_count(0) do vim.cmd("silent! normal "..tostring(i).."GzC") end vim.cmd("normal "..tostring(row).."G") end

vim.keymap.set('n', '', FoldEverything, {noremap=true, silent=true})

Result

All parts of code are folded. Exiting from insert mode not triggering any automatic folding/unfolding, everything stays the same as before insert mode entrance.

I hope it will be useful for people with the same issue.

System setup

Operating system: Linux Debian 11. Neovim version: v0.7.2 nvim-ufo version(commit): ad4ecb6 LSP server: gopls LSP client: nvim LSP (option 2 from minimal config) nvim-ufo config:

vim.o.foldcolumn = '1' vim.o.foldlevel = 99 vim.o.foldlevelstart = -1 vim.o.foldenable = true

kevinhwang91 commented 1 year ago

Do you mean require('ufo').closeAllFolds() doesn't work for you ? Maybe some issues with your config, please use a minimal config. foldlevel and foldlevelstart are both set to a large value like 99 may help.

hinell commented 1 year ago

I made a demo of this issue. Should I open a new one? There are different discussions elsewhere on that.

https://user-images.githubusercontent.com/8136158/216767149-e59ee6f7-ba17-4d00-9af7-4ede6c24d251.mp4

Reproduce

  1. Setup config below
  2. zx
  3. zM (z shift+m)
  4. Move into a folded text

Environment

NVIM v0.9.0-dev-d3355ad
Build type: MinSizeRel
LuaJIT 2.1.0-beta3

~/.config/nvim/lua/options.lua

...
local opt = vim.opt
        opt.foldopen:append("insert") 
        opt.foldmethod="expr"
        opt.foldlevel=1
        opt.foldlevelstart=99 -- 0 to close all folds upon opening file
        opt.foldminlines=4
        opt.foldcolumn="auto"
        opt.foldenable=true

Related discussions elsewhere

kevinhwang91 commented 1 year ago

set foldlevel? say what, maybe it was changed by another script.

hinell commented 1 year ago

foldlevel is default for me. In my setup the only plugin that affects folding currently is the nvim-treesitter. I've disbled nvim-ufo because of this quirk.

lmburns commented 1 year ago

This happens to me as well. It has something to do with the zM command specifically. Because if I go to foldlevel 0 by using zm instead, the BufWritePost and InsertLeave autocommands do not cause all folds to be closed.

Whenever I use zM, (the builtin zM, not ufo.closeAllFolds), once I leave insert mode or save the buffer, the corresponding autocommand from mentioned above trigger all folds to be closed.

If I use the zm command and then run zx or zX, the autocommand will trigger all folds to be closed as well.

D00mch commented 1 year ago

Thank you! I just fixed it by remapping zM to ufo.closeAllFolds.