kevinhwang91 / nvim-ufo

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

Auto close all on opening new file #129

Closed Willem-J-an closed 1 year ago

Willem-J-an commented 1 year ago

Feature description

I want to require("ufo").closeAllFolds when I open a file. I tried to use an autocmd like so:

if vim.o.foldcolumn == "0" then
  vim.o.foldcolumn = "1"
  vim.o.foldlevel = 99
  vim.o.foldlevelstart = 99
  vim.o.foldenable = true
end
vim.api.nvim_create_autocmd({ "BufEnter" }, {
  pattern = "*.py",
  callback = function()
    require("ufo").closeAllFolds()
end,
})

But this happens for every BufEnter, not just the initial one. The other autocmd's don't seem to work. I'm probably overlooking something obvious.

Describe the solution you'd like

A way to configure auto close for specific file types upon initial opening of the buffer.

Additional context

Hope you have some guidance on this, thanks a lot!

kevinhwang91 commented 1 year ago

ufo doesn't attach the buffer at once, so buffer is unattached in BufEnter. https://github.com/kevinhwang91/nvim-ufo/blob/9e829d5cfa3de6a2ff561d86399772b0339ae49d/lua/ufo/fold/init.lua#L29-L30 You need to make sure that buffer is attached if you invoke API. https://github.com/kevinhwang91/nvim-ufo/blob/9e829d5cfa3de6a2ff561d86399772b0339ae49d/doc/example.lua#L113-L125