Closed jghauser closed 2 years ago
Reproduced. It's a tech limitation for ufo. ufo is based on extmarks
to update the folds.
Unfortunately, mkview
/loadview
can't save/load any extmarks to/from script.
Must write pre/post script as a workaround.
Edit: A bulb flashed in my brain. I'm trying to fix this issue
Works perfectly, thanks for the super quick fix @kevinhwang91!
EDIT: sorry, i messed up the minimal config. let me quickly fix it! Fixed!
Hey @kevinhwang91, I'm still encountering this problem when trying to open files with Telescope's find_files.
Here's my minimal config to reproduce. I've added Telescope and some settings to get loadview
to work with it. Note that there is a longstanding issue in Telescope where folds only work with fixes. In that issue there is a different method of getting it to work (with autocmd
), and I've tried that as well with the same result.
local create_autocmd = vim.api.nvim_create_autocmd
local create_augroup = vim.api.nvim_create_augroup
vim.o.packpath = "~/.local/share/nvim/site"
vim.cmd("packadd promise-async")
vim.cmd("packadd nvim-ufo")
vim.cmd("packadd telescope.nvim")
-- Setting
vim.o.foldcolumn = "1"
vim.o.foldlevel = 99
vim.o.foldlevelstart = -1
vim.o.foldenable = true
local ufo = require("ufo")
ufo.setup()
vim.keymap.set("n", "zR", ufo.openAllFolds)
vim.keymap.set("n", "zM", ufo.closeAllFolds)
local saveAndLoadViews = create_augroup("saveAndLoadViews", { clear = true })
create_autocmd("BufLeave", {
pattern = "*.*",
command = "mkview",
group = saveAndLoadViews,
desc = "Save view on leaving buffer",
})
create_autocmd("BufEnter", {
pattern = "*.*",
command = "silent! loadview",
group = saveAndLoadViews,
desc = "Load view on entering buffer",
})
-- fix to get folds to work with telescope (see https://github.com/nvim-telescope/telescope.nvim/issues/559)
local action_set = require("telescope.actions.set")
require("telescope").setup({
pickers = {
find_files = {
hidden = true,
attach_mappings = function(prompt_bufnr)
action_set.select:enhance({
post = function()
vim.cmd(":normal! zx")
vim.cmd(":silent! loadview")
end,
})
return true
end,
},
},
})
The steps to reproduce are exactly the same as above, except now instead of point 5, the file is opened with Telescope find_files
.
Let me know if you think this is a different issue, and I will open a new one.
It's telescope issue.
Let me clarify maybe. When I use telescope, then with the above fix, folds are restored and everything works as expected. However, when I combine telescope with nvim-ufo, then only the folds that are visible are restored, which is the same problem as originally discussed in this issue. I thought maybe that's because ufo's conversion of the folds from the view
file into extmarks (which is what your fix does if I understand it correctly) doesn't get triggered. Is there maybe some function I can call in action_set.select:enhance
instead of loadview
that triggers ufo fetching the folds correctly?
Just like with the telescope issue, reloading the file with :e
fixes the folds. I guess I can use that for now, although it's very clunky. I totally agree that at the root this is telescope's issue; I guess I'm hoping for a workaround to get the workaround to work :sweat: :rofl: I don't have much hope on this getting properly fixed in telescope any time soon... But yeah, I do understand if you don't want spend any time on this!
In any case, thanks for the great plugin! :)
UFO_LOG=trace nvim
Log affter enter file:
You should add
loadview
after second BufEnter
require('ufo.lib.log').trace('debug yourself')
If I have time I will do some research.
Thanks for this useful response. I'll do some digging myself though I have to admit I'm still a noob in most of these things haha
@kevinhwang91: I asked in the telescope issue thread, and someone posted a working workaround. :)
Should work now without any hack. please handle your comment in telescope issue thread.
Wow, thanks a lot for working on this when it wasn't even ufo's bug to begin with!
Neovim version (nvim -v | head -n1)
NVIM v0.7.2
Operating system/version
ArchLinux latest
How to reproduce the issue
cat mini.lua
nvim --clean -u mini.lua some-file.ext
(NOTE: the bug does not seem to happen when opening a file with
:e
from within neovim)Expected behavior
I use some
autocmd
s to save and restore views and in particular folds (see abovemini.lua
). I would like all folds to be restored when opening a file again.Actual behavior
Only visible folds (that aren't off screen) are restored. All folds that are above or below what's currently visible on screen are fully opened.
Thanks for the great plugin! This has been something I've been missing for a while. I'm especially happy that it also gets rid of some annoying bugs in neovim's treesitter fold handling!