rebelot/heirline.nvim

`update` with multiple autocmd events

Closed this issue · 6 comments

There are a lot of times when it is useful to update a component on multiple autocmd events. This can be seen with the diagnostics for example doing update = { "DiagnosticChanged", "BufEnter" }, This works great if you are setting up a single autocmd with a single callback and pattern. This breaks down for example if you want to do the same with git difference component for example. where I want it to be updated on { "User", pattern = "GitSignsUpdate" } and { "BufEnter" }. If you do { "User", "BufEnter", pattern = "GitSignsUpdate" } then you are saying the pattern applies to both autocmd events. Do you think there could be a way to do this where you can create multiple autocommands to update the item?

I've just had something similar in the discussion

This is how one could define independent autocommand hooks:

local Updater = {
  init = function(self)
    if not rawget(self, "once") then
      local clear_cache = function()
        self._win_cache = nil
      end
      vim.api.nvim_create_autocomd("Event1", {
        callback = clear_cache
        })
      vim.api.nvim_create_autocomd("Event2", {
        callback = clear_cache
        })
      self.once = true
    end
  end
}

The above component and any of its children would be updated on Event1 or Event2. The window-local cache of the component is invalidate by the line self._win_cache = nil

I could add this example to the cookbook or allow the update field to be a list of events, but this would probably result in a breaking change

Awesome! Totally understand the avoidance of breaking changes and greatly greatly appreciate the desire to avoid them! I was able to make a generalized init function for the AstroNvim status API that resolves my issues and lets me migrate the rest of our statusline components to being updated on demand rather than on intervals (outside of file information component which I haven't quite cracked yet)

sorry for the duplicate issue, I'll make sure to check the discussions before opening anything else!

Oh not a problem at all! I was just pointing out a coincidence ;)

(outside of file information component which I haven't quite cracked yet)

Maybe I can help, what would you like to achieve? I'd imagine a filetype/bufname/fileflags component that updates on some events?

Iron-E commented

Maybe I can help, what would you like to achieve? I'd imagine a filetype/bufname/fileflags component that updates on some events?

I know I'm not OP, but I just tried this and ended up here:

update = {'BufEnter', {'User', pattern = 'GitSignsUpdate'}},

It's part of a Git component that I'd like to refresh when the git status changes, or when entering a new buffer.

For now I've worked around this by doing something like this:

local command = 'doautocmd User BufEnterOrGitSignsUpdate'
vim.api.nvim_create_autocmd('User', {command = command, pattern = 'GitSignsUpdate'})
vim.api.nvim_create_autocmd('BufEnter', {command = command})

And then set update to be on BufEnterOrGitSignsUpdate