neovim-gtk/src/plug_manager/manager.rs

119 lines
3.1 KiB
Rust
Raw Normal View History

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;
use super::store::{Store, PlugInfo};
2017-10-18 14:49:56 +00:00
use nvim::NeovimClient;
2017-10-15 19:50:59 +00:00
pub struct Manager {
pub vim_plug: vim_plug::Manager,
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 {
let (plug_manage_state, store) = if Store::is_config_exists() {
(PlugManageState::NvimGtk, Store::load())
} else {
(PlugManageState::Unknown, Store::empty())
};
Manager {
2017-10-18 14:49:56 +00:00
vim_plug: vim_plug::Manager::new(),
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-11-05 19:31:38 +00:00
pub fn generate_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
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();
}
}
}
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
}
}
}
pub fn save(&self) {
self.store.save();
}
2017-10-29 19:16:55 +00:00
pub fn clear_removed(&mut self) {
self.store.clear_removed();
}
pub fn add_plug(&mut self, plug: PlugInfo) -> bool {
self.store.add_plug(plug)
2017-10-29 19:16:55 +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 {
NvimGtk,
VimPlug,
2017-10-24 15:03:34 +00:00
Unknown,
}
#[derive(Clone)]
pub struct PlugManagerConfigSource {
pub source: String,
}
impl PlugManagerConfigSource {
pub fn new(store: &Store) -> Self {
let mut builder = "call plug#begin()\n".to_owned();
for plug in store.get_plugs() {
2017-10-29 19:16:55 +00:00
if !plug.removed {
2017-11-05 17:52:29 +00:00
builder += &format!("Plug '{}', {{ 'as': '{}' }}\n", plug.get_plug_path(), plug.name);
2017-10-29 19:16:55 +00:00
}
}
builder += "call plug#end()\n";
PlugManagerConfigSource { source: builder }
}
}