2022-07-10 23:47:14 +02:00
|
|
|
use rmpv::Value;
|
|
|
|
|
|
|
|
use tokio::io::Stdout;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
2022-07-11 02:08:37 +02:00
|
|
|
use nvim_rs::{compat::tokio::Compat, Handler, Neovim};
|
2022-07-10 23:47:14 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct NeovimHandler {
|
|
|
|
tx: mpsc::Sender<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NeovimHandler {
|
|
|
|
pub async fn new(tx: mpsc::Sender<i32>) -> Result<Self, tonic::transport::Error> {
|
|
|
|
Ok(NeovimHandler { tx })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tonic::async_trait]
|
|
|
|
impl Handler for NeovimHandler {
|
|
|
|
type Writer = Compat<Stdout>;
|
|
|
|
|
|
|
|
async fn handle_request(
|
|
|
|
&self,
|
|
|
|
name: String,
|
|
|
|
_args: Vec<Value>,
|
|
|
|
_neovim: Neovim<Compat<Stdout>>,
|
|
|
|
) -> Result<Value, Value> {
|
|
|
|
match name.as_ref() {
|
|
|
|
"ping" => Ok(Value::from("pong")),
|
|
|
|
"rpc" => {
|
2022-07-11 02:08:37 +02:00
|
|
|
eprintln!("Got 'rpc' from vim");
|
2022-07-10 23:47:14 +02:00
|
|
|
self.tx.send(0).await.unwrap();
|
|
|
|
// let request = tonic::Request::new(SessionRequest {session_id: 0});
|
|
|
|
// let response = self.client.create(request).await.unwrap();
|
|
|
|
Ok(Value::from("sent"))
|
|
|
|
},
|
2022-07-11 02:08:37 +02:00
|
|
|
"buffer" => {
|
|
|
|
let buf = _neovim.create_buf(true, false).await.unwrap();
|
|
|
|
buf.set_lines(0, 1, false, vec!["codeMP".to_string()]).await.unwrap();
|
|
|
|
_neovim.set_current_buf(&buf).await.unwrap();
|
|
|
|
Ok(Value::from("ok"))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
eprintln!("[!] unexpected call");
|
|
|
|
Ok(Value::from(""))
|
|
|
|
},
|
2022-07-10 23:47:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 02:08:37 +02:00
|
|
|
async fn handle_notify(
|
|
|
|
&self,
|
|
|
|
name: String,
|
|
|
|
_args: Vec<Value>,
|
|
|
|
_neovim: Neovim<Compat<Stdout>>,
|
|
|
|
) {
|
|
|
|
match name.as_ref() {
|
|
|
|
"insert" => {},
|
|
|
|
"tick" => eprintln!("tock"),
|
|
|
|
_ => eprintln!("[!] unexpected notify",)
|
2022-07-10 23:47:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|