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};
|
2016-03-25 09:51:28 +00:00
|
|
|
use std::cell::UnsafeCell;
|
|
|
|
use std::thread;
|
|
|
|
use std::sync::Arc;
|
2016-03-19 10:27:39 +00:00
|
|
|
use rmp::Value;
|
2016-03-23 15:22:28 +00:00
|
|
|
use ui::Ui;
|
2016-03-25 09:51:28 +00:00
|
|
|
use gtk;
|
|
|
|
|
|
|
|
pub struct MainLoopMutex<T: Sized> {
|
|
|
|
data: UnsafeCell<T>,
|
|
|
|
main_thread_name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: Sized + Send> Sync for MainLoopMutex<T> {}
|
|
|
|
|
|
|
|
impl<T> MainLoopMutex<T> {
|
|
|
|
pub fn new(t: T) -> MainLoopMutex<T> {
|
|
|
|
MainLoopMutex {
|
|
|
|
data: UnsafeCell::new(t),
|
|
|
|
main_thread_name: thread::current().name().map(|v| v.to_owned()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: return some sort of ref guard here
|
|
|
|
pub fn get(&self) -> &mut T {
|
|
|
|
if thread::current().name().map(|v| v.to_owned()) != self.main_thread_name {
|
|
|
|
panic!("Can access value only from main thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { &mut *self.data.get() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn safe_call<F, I>(mutex: Arc<MainLoopMutex<I>>, cb: F)
|
|
|
|
where I: 'static,
|
|
|
|
F: Fn(&MainLoopMutex<I>) + 'static
|
|
|
|
{
|
|
|
|
gtk::idle_add(move || {
|
|
|
|
cb(&*mutex);
|
|
|
|
gtk::Continue(false)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 10:27:39 +00:00
|
|
|
|
|
|
|
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 15:22:28 +00:00
|
|
|
pub fn start(mut ui: Ui) -> Result<Nvim> {
|
2016-03-23 11:59:18 +00:00
|
|
|
// let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
|
2016-03-24 09:21:27 +00:00
|
|
|
let mut session = if cfg!(target_os = "windows") {
|
|
|
|
Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap()
|
|
|
|
} else {
|
|
|
|
Session::new_child().unwrap()
|
|
|
|
};
|
2016-03-19 10:27:39 +00:00
|
|
|
let mut nvim = Neovim::new(session);
|
2016-03-23 15:22:28 +00:00
|
|
|
|
|
|
|
nvim.session.start_event_loop_cb(move |m, p| Nvim::cb(&mut ui, m, p));
|
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 15:22:28 +00:00
|
|
|
fn cb(ui: &mut Ui, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|