2016-03-19 11:32:59 +00:00
|
|
|
use neovim_lib::{Neovim, NeovimApi, Session};
|
2016-03-19 10:27:39 +00:00
|
|
|
use std::io::{Result, Error, ErrorKind};
|
|
|
|
use rmp::Value;
|
|
|
|
|
|
|
|
pub struct Nvim {
|
2016-03-23 11:59:18 +00:00
|
|
|
nvim: Neovim,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RedrawEvents {
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Nvim {
|
2016-03-23 11:59:18 +00:00
|
|
|
pub fn start<F: RedrawEvents + Send + 'static> (redraw_cb: F) -> Result<Nvim> {
|
|
|
|
// let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
|
2016-03-19 11:32:59 +00:00
|
|
|
let mut session = try!(Session::new_child());
|
2016-03-19 10:27:39 +00:00
|
|
|
|
2016-03-23 11:59:18 +00:00
|
|
|
session.start_event_loop_cb(move |m, p| Nvim::cb(&redraw_cb, m, p));
|
2016-03-19 10:27:39 +00:00
|
|
|
|
|
|
|
let mut nvim = Neovim::new(session);
|
2016-03-19 11:32:59 +00:00
|
|
|
// fix neovim --embed bug to start embed mode
|
|
|
|
nvim.input("i").unwrap();
|
2016-03-19 10:27:39 +00:00
|
|
|
try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e)));
|
|
|
|
|
2016-03-23 11:59:18 +00:00
|
|
|
Ok(Nvim { nvim: nvim })
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:59:18 +00:00
|
|
|
fn cb<F: RedrawEvents>(readraw_cb: &F, method: &str, params: Vec<Value>) {
|
2016-03-19 10:27:39 +00:00
|
|
|
if method == "redraw" {
|
|
|
|
for ev in params {
|
|
|
|
if let Value::Array(ev_args) = ev {
|
|
|
|
if let Value::String(ref ev_name) = ev_args[0] {
|
|
|
|
println!("Event {}", ev_name);
|
2016-03-23 11:59:18 +00:00
|
|
|
} else {
|
2016-03-19 10:27:39 +00:00
|
|
|
println!("Unsupported event {:?}", ev_args);
|
|
|
|
}
|
2016-03-23 11:59:18 +00:00
|
|
|
} else {
|
2016-03-19 10:27:39 +00:00
|
|
|
println!("Unsupported event type {:?}", ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("Notification {}", method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|