ibhagwan / fzf-lua

Improved fzf.vim written in lua
MIT License
2.25k stars 147 forks source link

Stack Overflow with a custom previewer as default_opts for a custom command #833

Closed iovis closed 1 year ago

iovis commented 1 year ago

Info

Description

Hey @ibhagwan! I was toying around with a custom previewer for my plugin, muxi.nvim, and I think I'm not registering my previewer correctly.

Unlike the last time, this previewer is a bit more custom (relies on implementation details of my plugin) and it doesn't seem like using an out of the box one is an option. It'll take a path and generate a prettified representation of a lua table associated with it:

Screenshot 2023-08-02 at 12 02 25

The previewer seems to be working great, your docs were very helpful. The one thing that's tripping me up is how to register it as the default previewer.

Basically I had tried the following:

local SessionPreviewer = require("muxi.fzf.sessions.previewer")

---@class MuxiFzfSessionManagerOpts
M.default_opts = {
  prompt = "muxi sessions> ",
  previewer = SessionPreviewer, --> TODO: stack overflow?
  actions = {
    ["ctrl-x"] = { fn = actions.delete_session, reload = true },
  },
  reload_actions = {
    [actions.delete_session] = true,
  },
}

But doing so caused the following backtrace:

E5108: Error executing lua: ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:270: stack overflow
stack traceback:
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:271: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...fig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/utils.lua:275: in function 'tbl_deep_clone'
    ...ig/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/config.lua:137: in function 'normalize_opts'
    ...david/Sites/vim/muxi.nvim/lua/muxi/fzf/sessions/init.lua:32: in function <...david/Sites/vim/muxi.nvim/lua/muxi/fzf/sessions/init.lua:31>

However, if instead of declaring it as default opts I just pass it, like so:

require("muxi.fzf.sessions").cmd({ previewer = SessionPreviewer })

Then everything works great.

What's the proper way of registering a default custom previewer for a command?

Thank you!

ibhagwan commented 1 year ago

But doing so caused the following backtrace:

That’s because most likely it’s not possible to recursively copy the previewer class, can you try like this?


M.default_opts = {
  prompt = "muxi sessions> ",
  previewer = { _ctor = SessionPreviewer },
}

This way only one function is copied (class instantiation) as opposed to the entire class metatable.

iovis commented 1 year ago
---@class MuxiFzfSessionManagerOpts
M.default_opts = {
  prompt = "muxi sessions> ",
  -- previewer = SessionPreviewer, -- TODO: stack overflow?
  previewer = { _ctor = SessionPreviewer },
  actions = {

Same issue unfortunately

ibhagwan commented 1 year ago

What about:

---@class MuxiFzfSessionManagerOpts
M.default_opts = {
  prompt = "muxi sessions> ",
  -- previewer = SessionPreviewer, -- TODO: stack overflow?
  previewer = { _ctor = SessionPreviewer.new },
iovis commented 1 year ago
E5108: Error executing lua: ...david/Sites/vim/muxi.nvim/lua/muxi/fzf/sessions/init.lua:59: ...hare/nvim/lazy/fzf-lua/lua/fzf-lua/previewer/builtin.lua:24: attempt to index local 'self' (a nil value)
stack traceback:
    [C]: in function 'fzf_exec'
    ...david/Sites/vim/muxi.nvim/lua/muxi/fzf/sessions/init.lua:59: in function <...david/Sites/vim/muxi.nvim/lua/muxi/fzf/sessions/init.lua:32>

Being sessions/init.lua:59 [Source]:

  fzf_lua.fzf_exec(contents, opts)
ibhagwan commented 1 year ago

Let me get back to you on that, being away from my computer I was doing some guesswork, I’ll test it myself next time I’m on the computer and let you know.

ibhagwan commented 1 year ago

I remembered I has a workaround to having to copying the class __index, using _ctor, I was close, this way only the function gets copied instead of the entire class object:

M.default_opts = {
  prompt = "muxi sessions> ",
  previewer = { _ctor = function() return SessionPreviewer end },
}
iovis commented 1 year ago

That fixed it, you're the best. Thank you!

hexh250786313 commented 2 months ago

@ibhagwan I am facing the same problem after 61d7f1763828. Thanks the issues query brings me here.

ibhagwan commented 2 months ago

@ibhagwan I am facing the same problem after 61d7f17. Thanks the issues query brings me here.

Can you try with the latest commit https://github.com/ibhagwan/fzf-lua/commit/4e861e4f91314f1f5108791201a45bff6c6abbf5? Although the error is stack overflow this is a different issue that was fixed by #1398.

hexh250786313 commented 2 months ago

@ibhagwan Ok, the latest commit fix it. Thank you!