neovim-gtk/src/plug_manager/manager.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

2017-10-15 19:50:59 +00:00
use std::rc::Rc;
2017-10-16 15:34:26 +00:00
use std::cell::{RefCell, RefMut};
2017-10-17 18:34:49 +00:00
use neovim_lib::{Neovim, NeovimApi};
2017-10-15 19:50:59 +00:00
2017-10-17 18:34:49 +00:00
use nvim::{NeovimClient, ErrorReport};
2017-10-15 19:50:59 +00:00
pub struct Manager {
2017-10-17 18:34:49 +00:00
nvim: Option<Rc<RefCell<NeovimClient>>>,
2017-10-15 19:50:59 +00:00
}
impl Manager {
pub fn new() -> Self {
2017-10-17 18:34:49 +00:00
Manager { nvim: None }
2017-10-15 19:50:59 +00:00
}
pub fn initialize(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
2017-10-16 15:34:26 +00:00
self.nvim = Some(nvim);
}
fn nvim(&self) -> Option<RefMut<Neovim>> {
let nvim_client = self.nvim.as_ref().unwrap();
if nvim_client.borrow().is_initialized() {
Some(RefMut::map(nvim_client.borrow_mut(), |n| n.nvim_mut()))
} else {
None
}
2017-10-15 19:50:59 +00:00
}
2017-10-16 15:34:26 +00:00
pub fn get_state(&self) -> State {
if let Some(mut nvim) = self.nvim() {
2017-10-17 18:34:49 +00:00
let loaded_plug = nvim.eval("exists('g:loaded_plug')");
loaded_plug
.ok_and_report(&mut *nvim)
.and_then(|loaded_plug| loaded_plug.as_i64())
.map_or(State::Unknown, |loaded_plug| if loaded_plug > 0 {
State::AlreadyLoaded
} else {
State::Unknown
})
} else {
State::Unknown
2017-10-16 15:34:26 +00:00
}
}
}
pub enum State {
Unknown,
AlreadyLoaded,
2017-10-15 19:50:59 +00:00
}