Expand description
§Scorched earth
A minimal port of scorched earth by boxofrox to nvim-rs. Works the same, but foregoes any error handling, removes the customisation of color, and removes some abstractions that aren’t helpfull in an example.
Note that this example uses tokio, while scorched_earth_as uses
async-std.
§Usage
First, build this example via
cargo build --example scorched_earthThen follow the steps described in the README for the examples.
§Description
Some overview over the code:
-
The associated type for our
Handleris out stdout. But tokio’sStdoutdoes not implementfutures::io::AsyncWrite, so it needs to be wrapped in the providedCompattype. -
The handler struct
NeovimHandlerneeds to contain some plugin state, namely two cursor positionsstartandend. It needs to beSendandSync, and we need mutable access, so we wrap it in aArc<Mutex<_>>. -
Implementing the
Handlertrait requires some magic because of the async functions, we we use theasync_traitmacro. -
We use
Stdoutas the type for theWriterbecause neovim acts as our parent, so it reads from our stdout. Note that this is the async version from Tokio. -
We only implement
handle_notifysince we don’t want to serve requests. It gets aNeovimpassed that we can use to send requests to neovim. All requests are async methods, so we need toawaitthem. -
The main function is denoted
#[tokio::main]to use async notation, but it would be perfectly feasible to explicitely create a runtime and use that. -
After creation of the handler, we connect to neovim via one of the
new_*functions. It gives back aNeoviminstance which we could use for requests, and a handle for the io loop. -
The plugin quits by ending the IO task when neovim closes the channel, so we don’t need to do anything special. Any cleanup-logic can happen after the IO task has finished. Note that we’re loosing access to our
Handler, so we might need to implementDropfor it, see the example. -
After the IO task has finished, we’re inspecting the errors to see why it went. A join error simply gets printed, then we inspect potential errors from the io loop itself. First, if we did not see a general reader error, we try to send some last notification to the neovim user. Secondly, we quietly ignore the channel being closed, because this usually means that it was closed by neovim, which isn’t always an error.
Note: A closed channel could still mean an error, so the plugin has the option to react to this.
-
As with the other examples, we implement
Spawnfor ourNeovimHandlermost trivially.