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

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

Open
ejrichards opened this issue Mar 27, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@ejrichards
Copy link

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 ejrichards added the bug Something isn't working label Mar 27, 2024
@ejrichards
Copy link
Author

ejrichards commented Mar 28, 2024

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

@ejrichards ejrichards changed the title UFO fails to initialize in buffer when opened via Harpoon 2 UFO fails to initialize in buffer when the buffer is loaded while hidden then opened (Harpoon) Mar 28, 2024
@dhruvindsd-dev
Copy link

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

@ejrichards
Copy link
Author

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants