nvim_gtk/plug_manager/
manager.rs1use std::rc::Rc;
2
3use super::vim_plug;
4use super::store::{Store, PlugInfo};
5
6use crate::nvim::NeovimClient;
7
8pub struct Manager {
9 pub vim_plug: vim_plug::Manager,
10 pub store: Store,
11 pub plug_manage_state: PlugManageState,
12}
13
14impl Manager {
15 pub fn new() -> Self {
16 let (plug_manage_state, store) = if Store::is_config_exists() {
17 (PlugManageState::NvimGtk, Store::load())
18 } else {
19 (PlugManageState::Unknown, Default::default())
20 };
21
22 Manager {
23 vim_plug: vim_plug::Manager::new(),
24 plug_manage_state,
25 store,
26 }
27 }
28
29 pub fn generate_config(&self) -> Option<PlugManagerConfigSource> {
30 if self.store.is_enabled() {
31 Some(PlugManagerConfigSource::new(&self.store))
32 } else {
33 None
34 }
35 }
36
37 pub fn init_nvim_client(&mut self, nvim: Rc<NeovimClient>) {
38 self.vim_plug.initialize(nvim);
39 }
40
41 pub fn reload_store(&mut self) {
42 match self.plug_manage_state {
43 PlugManageState::Unknown => {
44 if self.vim_plug.is_loaded() {
45 self.store = Store::load_from_plug(&self.vim_plug);
46 self.plug_manage_state = PlugManageState::VimPlug;
47 } else {
48 self.store = Default::default();
49 }
50 }
51 PlugManageState::NvimGtk => {
52 if Store::is_config_exists() {
53 self.store = Store::load();
54 } else {
55 self.store = Default::default();
56 }
57 }
58 PlugManageState::VimPlug => {
59 if Store::is_config_exists() {
60 self.store = Store::load();
61 self.plug_manage_state = PlugManageState::NvimGtk;
62 } else {
63 self.store = Default::default();
64 }
65 }
66 }
67 if let PlugManageState::Unknown = self.plug_manage_state {
68 if self.vim_plug.is_loaded() {
69 self.store = Store::load_from_plug(&self.vim_plug);
70 self.plug_manage_state = PlugManageState::VimPlug;
71 }
72 }
73 }
74
75 pub fn save(&self) {
76 self.store.save();
77 }
78
79 pub fn clear_removed(&mut self) {
80 self.store.clear_removed();
81 }
82
83 pub fn add_plug(&mut self, plug: PlugInfo) -> bool {
84 self.store.add_plug(plug)
85 }
86
87 pub fn move_item(&mut self, idx: usize, offset: i32) {
88 self.store.move_item(idx, offset);
89 }
90}
91
92pub enum PlugManageState {
93 NvimGtk,
94 VimPlug,
95 Unknown,
96}
97
98#[derive(Clone)]
99pub struct PlugManagerConfigSource {
100 pub source: String,
101}
102
103impl PlugManagerConfigSource {
104 pub fn new(store: &Store) -> Self {
105 let mut builder = "call plug#begin()\n".to_owned();
106
107 for plug in store.get_plugs() {
108 if !plug.removed {
109 builder += &format!(
110 "Plug '{}', {{ 'as': '{}' }}\n",
111 plug.get_plug_path(),
112 plug.name
113 );
114 }
115 }
116
117 builder += "call plug#end()\n";
118
119 PlugManagerConfigSource { source: builder }
120 }
121}