Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
foil-hat-guy opened this issue Oct 8, 2022 · 6 comments

Comments

@foil-hat-guy
Copy link

foil-hat-guy commented Oct 8, 2022

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

@foil-hat-guy foil-hat-guy added the enhancement New feature or request label Oct 8, 2022
@foil-hat-guy foil-hat-guy changed the title Close all folds without unwanted autofolding triggered by insert mode exit. Close all folds without unwanted autofolding/unfolding triggered by insert mode exit. Oct 8, 2022
@kevinhwang91
Copy link
Owner

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.

@kevinhwang91 kevinhwang91 removed the enhancement New feature or request label Oct 8, 2022
@hinell
Copy link

hinell commented Feb 4, 2023

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

Autofolding-when-escaping-from-insert-mode-2023-02-04_14.37.52.mp4
  • When folded, moving into a folded chunk of text and insert-ing and then hitting the <ESC> (escape) closes folds back.
  • This behavior consistent across different filetypes (lua, sh, cmake, c/c++).
  • This behavior affected by the foldopen option.
  • I'm sure this is not an upstream issue, cause it works fine with indent-based folding for me in nvim --clean mode

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
Copy link
Owner

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

@hinell
Copy link

hinell commented Feb 5, 2023

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
Copy link

lmburns commented Mar 15, 2023

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
Copy link

D00mch commented Mar 20, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants