kevinhwang91 / nvim-ufo

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

`loadview` only restores visible folds #45

Closed jghauser closed 2 years ago

jghauser commented 2 years ago

Neovim version (nvim -v | head -n1)

NVIM v0.7.2

Operating system/version

ArchLinux latest

How to reproduce the issue

cat mini.lua

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")

-- 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",
})

nvim --clean -u mini.lua some-file.ext

  1. open a file which contains a number of fold
  2. close some folds
  3. move the cursor so that some closed folds are off screen/invisible
  4. close neovim
  5. open file again
  6. scroll to look at the (previously) closed folds off screen ...

(NOTE: the bug does not seem to happen when opening a file with :e from within neovim)

Expected behavior

I use some autocmds to save and restore views and in particular folds (see above mini.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!

kevinhwang91 commented 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

jghauser commented 2 years ago

Works perfectly, thanks for the super quick fix @kevinhwang91!

jghauser commented 2 years ago

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.

kevinhwang91 commented 2 years ago

It's telescope issue.

jghauser commented 2 years ago

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! :)

kevinhwang91 commented 2 years ago

UFO_LOG=trace nvim Log affter enter file: image You should add loadview after second BufEnter require('ufo.lib.log').trace('debug yourself')

kevinhwang91 commented 2 years ago

If I have time I will do some research.

jghauser commented 2 years ago

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

jghauser commented 2 years ago

@kevinhwang91: I asked in the telescope issue thread, and someone posted a working workaround. :)

kevinhwang91 commented 2 years ago

Should work now without any hack. please handle your comment in telescope issue thread.

jghauser commented 2 years ago

Wow, thanks a lot for working on this when it wasn't even ufo's bug to begin with!