2017-10-15 19:50:59 +00:00
|
|
|
use std::rc::Rc;
|
2017-10-18 14:49:56 +00:00
|
|
|
use std::cell::RefCell;
|
2017-10-16 15:34:26 +00:00
|
|
|
|
2017-10-18 14:49:56 +00:00
|
|
|
use super::vim_plug;
|
2017-10-20 15:06:05 +00:00
|
|
|
use super::store::Store;
|
2017-10-26 14:56:53 +00:00
|
|
|
|
2017-10-18 14:49:56 +00:00
|
|
|
use nvim::NeovimClient;
|
2017-10-15 19:50:59 +00:00
|
|
|
|
|
|
|
pub struct Manager {
|
2017-10-24 15:03:34 +00:00
|
|
|
vim_plug: vim_plug::Manager,
|
|
|
|
pub plug_manage_state: PlugManageState,
|
2017-10-15 19:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Manager {
|
|
|
|
pub fn new() -> Self {
|
2017-10-29 10:55:36 +00:00
|
|
|
Manager {
|
2017-10-18 14:49:56 +00:00
|
|
|
vim_plug: vim_plug::Manager::new(),
|
2017-10-24 15:03:34 +00:00
|
|
|
plug_manage_state: PlugManageState::Unknown,
|
2017-10-16 15:34:26 +00:00
|
|
|
}
|
2017-10-15 19:50:59 +00:00
|
|
|
}
|
2017-10-16 15:34:26 +00:00
|
|
|
|
2017-10-26 14:56:53 +00:00
|
|
|
pub fn load_config(&mut self) -> Option<PlugManagerConfigSource> {
|
2017-10-24 15:03:34 +00:00
|
|
|
if Store::is_config_exists() {
|
2017-10-26 14:56:53 +00:00
|
|
|
let store = Store::load();
|
2017-10-29 10:55:36 +00:00
|
|
|
if store.is_enabled() {
|
|
|
|
let config = PlugManagerConfigSource::new(&store);
|
|
|
|
self.plug_manage_state = PlugManageState::NvimGtk(store);
|
|
|
|
Some(config)
|
|
|
|
} else {
|
|
|
|
self.plug_manage_state = PlugManageState::NvimGtk(store);
|
|
|
|
None
|
|
|
|
}
|
2017-10-24 15:03:34 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_nvim_client(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
|
2017-10-18 14:49:56 +00:00
|
|
|
self.vim_plug.initialize(nvim);
|
2017-10-16 15:34:26 +00:00
|
|
|
}
|
2017-10-20 15:06:05 +00:00
|
|
|
|
2017-10-24 15:03:34 +00:00
|
|
|
pub fn update_state(&mut self) {
|
|
|
|
if self.vim_plug.is_loaded() {
|
|
|
|
if let PlugManageState::Unknown = self.plug_manage_state {
|
2017-10-29 10:55:36 +00:00
|
|
|
self.plug_manage_state =
|
|
|
|
PlugManageState::VimPlug(Store::load_from_plug(&self.vim_plug));
|
2017-10-20 15:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-29 10:55:36 +00:00
|
|
|
|
|
|
|
pub fn store_mut(&mut self) -> Option<&mut Store> {
|
|
|
|
match self.plug_manage_state {
|
|
|
|
PlugManageState::NvimGtk(ref mut store) => Some(store),
|
|
|
|
PlugManageState::VimPlug(ref mut store) => Some(store),
|
|
|
|
PlugManageState::Unknown => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&self) -> Option<&Store> {
|
|
|
|
match self.plug_manage_state {
|
|
|
|
PlugManageState::NvimGtk(ref store) => Some(store),
|
|
|
|
PlugManageState::VimPlug(ref store) => Some(store),
|
|
|
|
PlugManageState::Unknown => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self) {
|
|
|
|
self.store().map(|s| s.save());
|
|
|
|
}
|
2017-10-29 19:16:55 +00:00
|
|
|
|
|
|
|
pub fn clear_removed(&mut self) {
|
|
|
|
self.store_mut().map(|s| s.clear_removed());
|
|
|
|
}
|
2017-10-16 15:34:26 +00:00
|
|
|
}
|
2017-10-24 15:03:34 +00:00
|
|
|
|
|
|
|
pub enum PlugManageState {
|
|
|
|
NvimGtk(Store),
|
2017-10-29 10:55:36 +00:00
|
|
|
VimPlug(Store),
|
2017-10-24 15:03:34 +00:00
|
|
|
Unknown,
|
|
|
|
}
|
2017-10-26 14:56:53 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PlugManagerConfigSource {
|
|
|
|
pub source: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PlugManagerConfigSource {
|
|
|
|
pub fn new(store: &Store) -> Self {
|
2017-10-29 10:55:36 +00:00
|
|
|
let mut builder = "call plug#begin()\n".to_owned();
|
2017-10-26 14:56:53 +00:00
|
|
|
|
|
|
|
for plug in store.get_plugs() {
|
2017-10-29 19:16:55 +00:00
|
|
|
if !plug.removed {
|
|
|
|
builder += &format!("Plug '{}'\n", plug.get_plug_path());
|
|
|
|
}
|
2017-10-26 14:56:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 10:55:36 +00:00
|
|
|
builder += "call plug#end()\n";
|
2017-10-26 14:56:53 +00:00
|
|
|
|
|
|
|
PlugManagerConfigSource { source: builder }
|
|
|
|
}
|
|
|
|
}
|