Expand description
§handler_drop
An example of handling cleanup logic by implementing
Drop for the handler. The plugin attaches to the current
buffer, then sets the first 2 lines, which get sent back to us with a
nvim_buf_lines_event notification. We handle this notification by saving
the lines in the handler. We then let nvim close the channel, and wait for
the IO loop to finish. The handler gets dropped, and so our cleanup logic is
executed.
§Usage
First, build the neovim included as a submodule:
cd neovim
makeSee https://github.com/neovim/neovim/wiki/Building-Neovim for build options. Nothing is really needed to run the example.
After that, run the example via
cargo run --example handler_dropYou can verify it worked by looking at the file handler_drop.txt in the
project directory which should contain 2 lines (“xyz” and “abc”).
§Description
-
The associated type for our
Handleris the stdin of our child. But tokio’sChildStdindoes not implementfutures::io::AsyncWrite, so it needs to be wrapped in the providedCompattype. -
Implementing
Dropis straightforward, except that we cannot do so asynchronously. Since dropping the handler is one of the last things our plugin does, it’s not problem to run even larger code bodies synchronously here. -
The event handling code is not efficient, because we just read the arguments by reference and clone them. It’s easy to take ownership directly by matching on the enum
Valuedirectly, though. -
There’s basically no error handling, other than
unwraping all theResults. -
awaiting the io future handle is probably not necessary, but feels like a nice thing to do. -
As with the other examples, we implement
Spawnfor ourNeovimHandlermost trivially.