echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.74k stars 179 forks source link

Adding events that triggered once when open or close are called #817

Closed bassamsdata closed 4 months ago

bassamsdata commented 4 months ago

Contributing guidelines

Module(s)

mini.files

Description

Hello, Would it be possible to include an event trigger for when minifiles.open() is called, and another for when minifiles.close() happens? Currently, many events trigger multiple times throughout a minifiles session, which works well for actions within minifiles windows/buffers. However, it's not ideal for triggering actions solely on minifiles open or close.

Here's why I'm suggesting this: I've implemented git integration with caching and need to clear the cache efficiently when the minifiles session ends. Without these specific events, it's almost impossible to achieve this efficiently. something like this:

local function clearCache()
  gitStatusCache = {}
end

vim.api.nvim_create_autocmd("User", {
  pattern = "MiniFilesClose", -- here is an example
  callback = function(args)
      clearCache()
    end
  end,
})

I understand the value of your time, and I don't want to take up too much of it. It seems like a straightforward addition, considering there's already a mechanism for generating triggers in the source code.

Thanks for considering this!

echasnovski commented 4 months ago

Thanks for the suggestion!

My previous answer to this was for user to override the open() and close() functions to perform actions they need. But this use case is both denies this advice and is quite useful.

I myself was thinking recently about Git status icons and it seems to be a relatively concise and performant way to do so. And it indeed needs those events. So I think I'll consider adding that to 'mini.extra' in the recent-ish future, while resolving this issue hopefully sooner.

bassamsdata commented 4 months ago

thank you, appreciated!

echasnovski commented 4 months ago

This should now be resolved on latest main with MiniFilesExplorerOpen and MiniFilesExplorerClose events.

Would you mind following up on whether they work for your git status caching use case? Thanks.

bassamsdata commented 4 months ago

Would you mind following up on whether they work for your git status caching use case?

Thanks for adding these events. Autocommands that trigger open (git status) and close(clear cache) are working wonderfully (you can check out my integration here).

Currently, everything's smooth, and I've also got a mapping that uses vim.cmd("@"), which now functions perfectly without triggering multiple requests for git status.

Thank you!