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-30 15:25:23 +00:00
|
|
|
use super::store::{Store, PlugInfo};
|
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-11-04 20:04:03 +00:00
|
|
|
pub vim_plug: vim_plug::Manager,
|
2017-10-30 15:25:23 +00:00
|
|
|
pub store: Store,
|
2017-10-24 15:03:34 +00:00
|
|
|
pub plug_manage_state: PlugManageState,
|
2017-10-15 19:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Manager {
|
|
|
|
pub fn new() -> Self {
|
2017-10-30 15:25:23 +00:00
|
|
|
let (plug_manage_state, store) = if Store::is_config_exists() {
|
|
|
|
(PlugManageState::NvimGtk, Store::load())
|
|
|
|
} else {
|
|
|
|
(PlugManageState::Unknown, Store::empty())
|
|
|
|
};
|
|
|
|
|
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-30 15:25:23 +00:00
|
|
|
plug_manage_state,
|
|
|
|
store,
|
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-30 15:25:23 +00:00
|
|
|
pub fn load_config(&self) -> Option<PlugManagerConfigSource> {
|
|
|
|
if self.store.is_enabled() {
|
|
|
|
Some(PlugManagerConfigSource::new(&self.store))
|
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-11-02 14:31:46 +00:00
|
|
|
pub fn reload_store(&mut self) {
|
|
|
|
match self.plug_manage_state {
|
|
|
|
PlugManageState::Unknown => {
|
|
|
|
if self.vim_plug.is_loaded() {
|
|
|
|
self.store = Store::load_from_plug(&self.vim_plug);
|
|
|
|
self.plug_manage_state = PlugManageState::VimPlug;
|
|
|
|
} else {
|
|
|
|
self.store = Store::empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PlugManageState::NvimGtk => {
|
|
|
|
if Store::is_config_exists() {
|
|
|
|
self.store = Store::load();
|
|
|
|
} else {
|
|
|
|
self.store = Store::empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PlugManageState::VimPlug => {
|
|
|
|
if Store::is_config_exists() {
|
|
|
|
self.store = Store::load();
|
|
|
|
self.plug_manage_state = PlugManageState::NvimGtk;
|
|
|
|
} else {
|
|
|
|
self.store = Store::empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 15:25:23 +00:00
|
|
|
if let PlugManageState::Unknown = self.plug_manage_state {
|
|
|
|
if self.vim_plug.is_loaded() {
|
|
|
|
self.store = Store::load_from_plug(&self.vim_plug);
|
|
|
|
self.plug_manage_state = PlugManageState::VimPlug;
|
2017-10-20 15:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-29 10:55:36 +00:00
|
|
|
|
|
|
|
pub fn save(&self) {
|
2017-10-30 15:25:23 +00:00
|
|
|
self.store.save();
|
2017-10-29 10:55:36 +00:00
|
|
|
}
|
2017-10-29 19:16:55 +00:00
|
|
|
|
|
|
|
pub fn clear_removed(&mut self) {
|
2017-10-30 15:25:23 +00:00
|
|
|
self.store.clear_removed();
|
|
|
|
}
|
|
|
|
|
2017-11-01 14:40:54 +00:00
|
|
|
pub fn add_plug(&mut self, plug: PlugInfo) -> bool {
|
|
|
|
self.store.add_plug(plug)
|
2017-10-29 19:16:55 +00:00
|
|
|
}
|
2017-11-02 14:31:46 +00:00
|
|
|
|
|
|
|
pub fn move_item(&mut self, idx: usize, offset: i32) {
|
|
|
|
self.store.move_item(idx, offset);
|
|
|
|
}
|
2017-10-16 15:34:26 +00:00
|
|
|
}
|
2017-10-24 15:03:34 +00:00
|
|
|
|
|
|
|
pub enum PlugManageState {
|
2017-10-30 15:25:23 +00:00
|
|
|
NvimGtk,
|
|
|
|
VimPlug,
|
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 }
|
|
|
|
}
|
|
|
|
}
|