neovimhaskell / nvim-hs

Neovim API for Haskell plugins as well as the plugin provider
Other
267 stars 18 forks source link

Listening to buffer events with `nvim_buf_attach` #104

Closed Lysxia closed 1 year ago

Lysxia commented 1 year ago

I'd like to keep track of what text gets inserted and deleted in real time. It seems the correct function for that is nvim_buf_attach. However, it takes a callback to be applied to the events, which seems only possible to provide in lua. Is there another way to do that?

saep commented 1 year ago

I have to research that myself.

It's been 7 years since I last seriously worked on the messaging side, so I'll have a look at it at the weekend.

I'll just dump two spontaneous ideas about how to solve this.

The "right" way

Activates buffer-update events on a channel, or as Lua callbacks.

The documentation mentions that these events are sent on a channel. I assume that this can mean the channel nvim-hs is using to communicate with nvim. I'm hopeful that it is possible to subscribe to all these notifications. But I don't remember how to hook into that mechanism or whether its possible at the moment to use. I think that doing this is useful and an API (module) should be added to nvim-hs that makes this possible and ideally easy to use.

The lua way

You can probably expose functions from nvim-hs and add lua callbacks that call these functions. This might be fine for a quick workaround.

Lysxia commented 1 year ago

Thanks! I'm currently trying out the lua way with the help of isovector, and with a little bit of lua that I can hide in a corner it seems to work well enough.

saep commented 1 year ago

I've created a first draft for subscribing to events in #105

My spontaneous idea for the api is, that there is a subscribe function with a callback for a specifc event and an unsubscribe function.

subscribe :: Text -> ([Object] -> Neovim env ()) -> Neovim env Subscription

unsubscribe :: Subscription -> Neovim env ()

You can see the usage in test-suite/Neovim/NvimBufAttachSpec.hs where I write the event in a custom object inside an MVar.

I still have to clean up the code and add more tests.

Feel free to suggest improvements to this low level API.

A more user friendly API specifically for nvim_buf_attach can be built on top of this later. This could provide typed event objects for the ones mentioned in :help nvim_buf_attach() instead of the current [Object].

Lysxia commented 1 year ago

Thank you so much! I'll be trying this soon.

saep commented 1 year ago

I've created a release with these functions added: https://hackage.haskell.org/package/nvim-hs-2.3.1.0