nvimtools / hydra.nvim

Create custom submodes and menus
MIT License
145 stars 8 forks source link

[Feature Request] HydraEnter/Exit autocommands #1

Closed benlubas closed 10 months ago

benlubas commented 10 months ago

porting https://github.com/nvim-island/hydra.nvim/issues/8

Firing User autocommands after a hydra gets activated/deactivated would be cool. My use case is using it to immediately refresh my status line which I use to show an active hydra:

image

Currently I have to wait for something else to refresh it (which isn't a big deal, it's just not ideal).

As far as implementation details, there should probably be 4 of these:

Each one could also include some data in the data field (see :h nvim_exec_autocmds()), though I'm not sure what type of data would be useful (I wouldn't need any for my use case)

miversen33 commented 10 months ago

This is a copy from issue #8 from the previous fork.

Last comments were about us considering going with an event system based in lua vs using Vim autocommands. I do still think this is the right path to go though in either case we don't know enough about "how" and "when" hydra does stuff so an event system might be a reach right now. One of us (probably me) will need to dig into the internals of hydra to get an idea of how hard it would be to do this one way vs another.

benlubas commented 10 months ago

Here's the argument for autocommands:

There's first class support for them in the lua API with: nvim_create_autocmd(), nvim_create_augroup(), and nvim_exec_autocmds() and others. The last of which has additional functionality on top of vanilla vim script that allows attaching arbitrary data to the fired event.

Main advantages:

miversen33 commented 10 months ago

All of those are fantastic points

A couple things here

My biggest reason for wanting to go with a lua event system as opposed to using autocommands is that it is much easier to debug code when it is not diving in and out of the vim stack. I acknowledge that debugging ease alone should not make a decision though.

Do you know when the various nvim_autocmd functions were added to the API? My other concern is introducing reliance on nightly apis. I understand that alot of users use the nightlys because they want the new APIs as they come, but I am not personally open to the idea of forcing an existing userbase to update to a nightly version of neovim just for a new feature we want.

I believe the nvim_autocmd functions are on stable so this last argument is likely moot, but it should be considered for any new features we wish to add. Hydra has a pretty big userbase and we want to stay as compliant as possible with them (especially as it seems that the original is effectively dead).

All of that said, this is a feature I am interested in but not enough to make it. So at the end of the day, if you wish to do it with Autocommands, you absolutely can :)

If we do go that route (or if we go the callback route), we need to document what the data package is that we are attaching/returning.

miversen33 commented 10 months ago

Digging into the code a bit, there is already a quasi event system in place

Hydra supports adding custom on_enter and on_exit callbacks to hydra. So in theory this should be a minimal change regardless of which route we go, though it does raise the question of if this is needed or not.

Additional details, there is a util.add_hook_before function, however there is no add_hook_after function. We could probably add that, though I do wonder how much that would impact the overall philosophy behind hydra (remember that you can chain heads and therefore you may not want to call something after each head is completed)

benlubas commented 10 months ago

when the various nvim_autocmd functions were added to the API?

Started showing up in like 0.5 or something. I use them right now running 9.4.

I agree using nightly features should be configurable, off by default, and clearly labeled.

on_enter and on_exit

I don't know how I missed that 🤦

This is good enough for me. It would be more convenient to have global enter and exit just so I can set it up only once, but that's not the end of the world.

miversen33 commented 10 months ago

It would be more convenient to have global enter and exit just so I can set it up only once

I don't know that hydra has a sort of "global" configuration, but there is nothing saying you cant make that a thing and just add that into the on_enter and on_exit functions for each hydra ;)

benlubas commented 10 months ago

I'm going to close this for now, might revisit it in the future.