Load vim-plug configuration

This commit is contained in:
daa84 2017-10-20 18:06:05 +03:00
parent a286d39b48
commit c1b6bbca88
4 changed files with 126 additions and 12 deletions

View File

@ -2,6 +2,7 @@ use std::rc::Rc;
use std::cell::RefCell;
use super::vim_plug;
use super::store::Store;
use nvim::NeovimClient;
pub struct Manager {
@ -18,4 +19,16 @@ impl Manager {
pub fn initialize(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
self.vim_plug.initialize(nvim);
}
pub fn load_store(&self, vim_plug_state: &vim_plug::State) -> Store {
match *vim_plug_state {
vim_plug::State::AlreadyLoaded => {
let store = Store::load_from_plug(&self.vim_plug);
store
}
vim_plug::State::Unknown => {
Store::load()
}
}
}
}

View File

@ -1,12 +1,33 @@
use toml;
use settings::SettingsLoader;
use super::vim_plug;
pub struct Store {}
pub struct Store {
settings: Settings,
}
impl Store {
pub fn new() -> Self {
Store {}
pub fn load() -> Self {
Store { settings: Settings::load() }
}
pub fn load_from_plug(vim_plug: &vim_plug::Manager) -> Self {
let settings = match vim_plug.get_plugs() {
Err(msg) => {
error!("{}", msg);
Settings::empty()
}
Ok(plugs) => {
let plugs = plugs
.iter()
.map(|vpi| PlugInfo::new(vpi.name.to_owned(), vpi.uri.to_owned()))
.collect();
Settings::new(plugs)
}
};
Store { settings }
}
}
@ -15,6 +36,12 @@ struct Settings {
plugs: Vec<PlugInfo>,
}
impl Settings {
fn new(plugs: Vec<PlugInfo>) -> Self {
Settings { plugs }
}
}
impl SettingsLoader for Settings {
const SETTINGS_FILE: &'static str = "plugs.toml";

View File

@ -3,6 +3,7 @@ use gtk::prelude::*;
use super::manager;
use super::vim_plug;
use super::store::Store;
pub struct Ui<'a> {
manager: &'a manager::Manager,
@ -26,15 +27,22 @@ impl<'a> Ui<'a> {
let content = dlg.get_content_area();
let tabs = gtk::Notebook::new();
match self.get_state() {
let vim_plug_state = self.get_state();
match vim_plug_state {
vim_plug::State::AlreadyLoaded => {
let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
let warn_lbl = gtk::Label::new(
"vim-plug manager already loaded.\n\
NeovimGtk manages plugins using vim-plug as backend.\n\
To allow NeovimGtk manage plugins please disable vim-plug in your configuration",
To allow NeovimGtk manage plugins please disable vim-plug in your configuration.\n\
You can convert vim-plug configuration to NeovimGtk conviguration using button below.\n\
List of current vim-plug plugins can be found in 'Plugins' tab.",
);
get_plugins.add(&warn_lbl);
let copy_btn = gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
get_plugins.add(&copy_btn);
let get_plugins_lbl = gtk::Label::new("Help");
tabs.append_page(&get_plugins, Some(&get_plugins_lbl));
}
@ -45,8 +53,11 @@ impl<'a> Ui<'a> {
}
}
self.get_plugs();
let plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
let store = self.manager.load_store(&vim_plug_state);
self.fill_plugin_list(&plugins, &store);
let plugins_lbl = gtk::Label::new("Plugins");
tabs.append_page(&plugins, Some(&plugins_lbl));
@ -65,11 +76,23 @@ impl<'a> Ui<'a> {
dlg.destroy();
}
fn fill_plugin_list(&self, panel: &gtk::Box, store: &Store) {
let tree = gtk::TreeView::new();
let scroll = gtk::ScrolledWindow::new(None, None);
tree.set_headers_visible(false);
tree.set_can_focus(false);
scroll.set_policy(gtk::PolicyType::Never, gtk::PolicyType::Automatic);
scroll.add(&tree);
panel.add(&scroll);
let copy_btn = gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
panel.add(&copy_btn);
}
fn get_state(&self) -> vim_plug::State {
self.manager.vim_plug.get_state()
}
fn get_plugs(&self) {
self.manager.vim_plug.get_plugs();
}
}

View File

@ -4,6 +4,7 @@ use std::cell::{RefCell, RefMut};
use neovim_lib::{Neovim, NeovimApi};
use nvim::{NeovimClient, ErrorReport};
use value::ValueMapExt;
pub struct Manager {
nvim: Option<Rc<RefCell<NeovimClient>>>,
@ -27,9 +28,47 @@ impl Manager {
}
}
pub fn get_plugs(&self) {
pub fn get_plugs(&self) -> Result<Box<[VimPlugInfo]>, String> {
if let Some(mut nvim) = self.nvim() {
let plugs = nvim.eval("g:plugs");
let g_plugs = nvim.eval("g:plugs").map_err(|e| {
format!("Can't retrive g:plugs map: {}", e)
})?;
let plugs_map = g_plugs
.as_map()
.ok_or("Can't retrive g:plugs map".to_owned())?
.to_attrs_map()?;
let g_plugs_order = nvim.eval("g:plugs_order").map_err(|e| format!("{}", e))?;
let order_arr = g_plugs_order.as_array().ok_or(
"Can't find g:plugs_order array"
.to_owned(),
)?;
let plugs_info: Vec<VimPlugInfo> = order_arr
.iter()
.map(|n| n.as_str())
.filter_map(|name| if let Some(name) = name {
plugs_map
.get(name)
.and_then(|desc| desc.as_map())
.and_then(|desc| desc.to_attrs_map().ok())
.and_then(|desc| {
let uri = desc.get("uri").and_then(|uri| uri.as_str());
if let Some(uri) = uri {
Some(VimPlugInfo::new(name.to_owned(), uri.to_owned()))
} else {
None
}
})
} else {
None
})
.collect();
Ok(plugs_info.into_boxed_slice())
} else {
Err("Nvim not initialized".to_owned())
}
}
@ -50,6 +89,18 @@ impl Manager {
}
}
#[derive(Debug)]
pub struct VimPlugInfo {
pub name: String,
pub uri: String,
}
impl VimPlugInfo {
pub fn new(name: String, uri: String) -> Self {
VimPlugInfo { name, uri }
}
}
pub enum State {
Unknown,
AlreadyLoaded,