1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::rc::Rc;

use super::vim_plug;
use super::store::{Store, PlugInfo};

use crate::nvim::NeovimClient;

pub struct Manager {
    pub vim_plug: vim_plug::Manager,
    pub store: Store,
    pub plug_manage_state: PlugManageState,
}

impl Manager {
    pub fn new() -> Self {
        let (plug_manage_state, store) = if Store::is_config_exists() {
            (PlugManageState::NvimGtk, Store::load())
        } else {
            (PlugManageState::Unknown, Default::default())
        };

        Manager {
            vim_plug: vim_plug::Manager::new(),
            plug_manage_state,
            store,
        }
    }

    pub fn generate_config(&self) -> Option<PlugManagerConfigSource> {
        if self.store.is_enabled() {
            Some(PlugManagerConfigSource::new(&self.store))
        } else {
            None
        }
    }

    pub fn init_nvim_client(&mut self, nvim: Rc<NeovimClient>) {
        self.vim_plug.initialize(nvim);
    }

    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 = Default::default();
                }
            }
            PlugManageState::NvimGtk => {
                if Store::is_config_exists() {
                    self.store = Store::load();
                } else {
                    self.store = Default::default();
                }
            }
            PlugManageState::VimPlug => {
                if Store::is_config_exists() {
                    self.store = Store::load();
                    self.plug_manage_state = PlugManageState::NvimGtk;
                } else {
                    self.store = Default::default();
                }
            }
        }
        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;
            }
        }
    }

    pub fn save(&self) {
        self.store.save();
    }

    pub fn clear_removed(&mut self) {
        self.store.clear_removed();
    }

    pub fn add_plug(&mut self, plug: PlugInfo) -> bool {
        self.store.add_plug(plug)
    }

    pub fn move_item(&mut self, idx: usize, offset: i32) {
        self.store.move_item(idx, offset);
    }
}

pub enum PlugManageState {
    NvimGtk,
    VimPlug,
    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() {
            if !plug.removed {
                builder += &format!(
                    "Plug '{}', {{ 'as': '{}' }}\n",
                    plug.get_plug_path(),
                    plug.name
                );
            }
        }

        builder += "call plug#end()\n";

        PlugManagerConfigSource { source: builder }
    }
}