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

UFO fails to initialize in buffer when the buffer is loaded while hidden then opened (Harpoon) #210

Open ejrichards opened 3 months ago

ejrichards commented 3 months ago

Neovim version (nvim -v | head -n1)

NVIM v0.9.5

Operating system/version

Ubuntu 22.04.4 (WSL) and Windows 11

How to reproduce the issue

Minimal config:

vim.g.mapleader = " "

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require('lazy').setup({
    {
        "kevinhwang91/nvim-ufo",
        dependencies = { "kevinhwang91/promise-async" },
        config = function()
            vim.o.foldcolumn = '1'
            vim.o.foldlevel = 99 -- Using ufo provider need a large value, feel free to decrease the value
            vim.o.foldlevelstart = 99
            vim.o.foldenable = true

            local ufo = require('ufo')
            ufo.setup({
                provider_selector = function(bufnr, filetype, buftype)
                    return {'treesitter', 'indent'}
                end
            })
        end
    },
    {
        "ThePrimeagen/harpoon",
        dependencies = { "nvim-lua/plenary.nvim" },
        branch = "harpoon2",
        config = function()
            local harpoon = require("harpoon"):setup({
                settings = {
                    save_on_toggle = true
                }
            });

            vim.keymap.set("n", "<leader>a", function() harpoon:list():append() end, { desc = 'Harpoon Append' })
            vim.keymap.set("n", "<leader>j", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end, { desc = 'Harpoon List' })
            vim.keymap.set("n", "<C-j>", function() harpoon:list():select(1) end)
            vim.keymap.set("n", "<C-k>", function() harpoon:list():select(2) end)
            vim.keymap.set("n", "<C-l>", function() harpoon:list():select(3) end)
            vim.keymap.set("n", "<C-;>", function() harpoon:list():select(4) end)
        end
    }
})
  1. Run nvim and open any file, eg. nvim -u init.lua init.lua
  2. <Space>a to append the file to Harpoon list
  3. :bw
  4. <C-j> to open the file via Harpoon
  5. :UfoInspect

Output:

E5108: Error executing lua .../lazy/nvim-ufo/lua/ufo/main.lua:123: attempt to index field 'providers' (a nil value)
stack traceback:
        .../lazy/nvim-ufo/lua/ufo/main.lua:123: in function 'inspectBuf'
        .../lazy/nvim-ufo/lua/ufo.lua:65: in function 'inspect'
[string ":lua"]:1: in main chunk
  1. :e to reload
  2. Folds are created normally and no error for :UfoInspect

Expected behavior

UFO should initialize properly when a buffer is opened via Harpoon

Actual behavior

No folds are created and :UfoInspect reports an error

ejrichards commented 3 months ago

I dug into the Harpoon code a bit and was able to make a smaller minimal config based on how it is loading buffers:

init.lua

vim.api.nvim_create_user_command('LoadTest', function ()
  local filename = 'init.lua'
  local bufnr = vim.fn.bufnr(filename, true)
  vim.fn.bufload(bufnr)
  vim.api.nvim_set_option_value("buflisted", true, { buf = bufnr })
  vim.api.nvim_set_current_buf(bufnr)
end, {})

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require('lazy').setup({
  {
    "kevinhwang91/nvim-ufo",
    dependencies = { "kevinhwang91/promise-async" },
    config = function()
      vim.o.foldcolumn = '1'
      vim.o.foldlevel = 99 -- Using ufo provider need a large value, feel free to decrease the value
      vim.o.foldlevelstart = 99
      vim.o.foldenable = true

      local ufo = require('ufo')
      ufo.setup({
        provider_selector = function(bufnr, filetype, buftype)
          return {'treesitter', 'indent'}
        end
      })
    end
  },
})
  1. nvim -u init.lua
  2. :LoadTest
  3. :UfoInspect - Same output as above

Relevant function in harpoon: Link

dhruvindsd-dev commented 2 months ago

facing this exact same issue, were you able to find a solution for this?

ejrichards commented 2 months ago

A workaround for harpoon specifically, you can force Ufo to reload on every navigation:

require("harpoon"):extend(require("harpoon.extensions").builtins.command_on_nav('UfoEnableFold'))
dhruvindsd-dev commented 2 months ago

thanks for the solution! this was becoming irritating as I usually first zm when opening a large file.