From c1b6bbca88620fc1b9c416f49588987967db18af Mon Sep 17 00:00:00 2001 From: daa84 Date: Fri, 20 Oct 2017 18:06:05 +0300 Subject: [PATCH] Load vim-plug configuration --- src/plug_manager/manager.rs | 13 +++++++++ src/plug_manager/store.rs | 33 ++++++++++++++++++++-- src/plug_manager/ui.rs | 37 +++++++++++++++++++----- src/plug_manager/vim_plug.rs | 55 ++++++++++++++++++++++++++++++++++-- 4 files changed, 126 insertions(+), 12 deletions(-) diff --git a/src/plug_manager/manager.rs b/src/plug_manager/manager.rs index 13158ee..05533f9 100644 --- a/src/plug_manager/manager.rs +++ b/src/plug_manager/manager.rs @@ -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>) { 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() + } + } + } } diff --git a/src/plug_manager/store.rs b/src/plug_manager/store.rs index 8f56edc..2f9c9ec 100644 --- a/src/plug_manager/store.rs +++ b/src/plug_manager/store.rs @@ -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, } +impl Settings { + fn new(plugs: Vec) -> Self { + Settings { plugs } + } +} + impl SettingsLoader for Settings { const SETTINGS_FILE: &'static str = "plugs.toml"; diff --git a/src/plug_manager/ui.rs b/src/plug_manager/ui.rs index c1d939e..56e0297 100644 --- a/src/plug_manager/ui.rs +++ b/src/plug_manager/ui.rs @@ -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(©_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: >k::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(©_btn); + } + fn get_state(&self) -> vim_plug::State { self.manager.vim_plug.get_state() } - - fn get_plugs(&self) { - self.manager.vim_plug.get_plugs(); - } } + diff --git a/src/plug_manager/vim_plug.rs b/src/plug_manager/vim_plug.rs index 9113956..9b74718 100644 --- a/src/plug_manager/vim_plug.rs +++ b/src/plug_manager/vim_plug.rs @@ -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>>, @@ -27,9 +28,47 @@ impl Manager { } } - pub fn get_plugs(&self) { + pub fn get_plugs(&self) -> Result, 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 = 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,