From b0477c36f60e9843fd5042326d70081eefb07ba2 Mon Sep 17 00:00:00 2001 From: daa Date: Tue, 17 Oct 2017 21:34:49 +0300 Subject: [PATCH] Check vim-plug loaded --- src/nvim.rs | 11 +++++++++-- src/plug_manager/manager.rs | 24 +++++++++++++++--------- src/plug_manager/ui.rs | 33 +++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/nvim.rs b/src/nvim.rs index d9287bc..4290f69 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -505,17 +505,24 @@ fn call( Ok(repaint_mode) } -pub trait ErrorReport { +pub trait ErrorReport { fn report_err(&self, nvim: &mut NeovimApi); + + fn ok_and_report(&self, nvim: &mut NeovimApi) -> Option<&T>; } -impl ErrorReport for result::Result { +impl ErrorReport for result::Result { fn report_err(&self, _: &mut NeovimApi) { if let Err(ref err) = *self { println!("{}", err); //nvim.report_error(&err_msg).expect("Error report error :)"); } } + + fn ok_and_report(&self, nvim: &mut NeovimApi) -> Option<&T> { + self.report_err(nvim); + self.as_ref().ok() + } } #[derive(Clone, Debug)] diff --git a/src/plug_manager/manager.rs b/src/plug_manager/manager.rs index fe9a93c..24d295e 100644 --- a/src/plug_manager/manager.rs +++ b/src/plug_manager/manager.rs @@ -1,19 +1,17 @@ use std::rc::Rc; use std::cell::{RefCell, RefMut}; -use neovim_lib::{Neovim, NeovimApi, Value}; +use neovim_lib::{Neovim, NeovimApi}; -use nvim::NeovimClient; +use nvim::{NeovimClient, ErrorReport}; pub struct Manager { - nvim: Option>>, + nvim: Option>>, } impl Manager { pub fn new() -> Self { - Manager { - nvim: None, - } + Manager { nvim: None } } pub fn initialize(&mut self, nvim: Rc>) { @@ -31,10 +29,18 @@ impl Manager { pub fn get_state(&self) -> State { if let Some(mut nvim) = self.nvim() { - nvim.command("exists('g:loaded_plug')"); + let loaded_plug = nvim.eval("exists('g:loaded_plug')"); + loaded_plug + .ok_and_report(&mut *nvim) + .and_then(|loaded_plug| loaded_plug.as_i64()) + .map_or(State::Unknown, |loaded_plug| if loaded_plug > 0 { + State::AlreadyLoaded + } else { + State::Unknown + }) + } else { + State::Unknown } - - State::Unknown } } diff --git a/src/plug_manager/ui.rs b/src/plug_manager/ui.rs index c5903f7..e3d1dd0 100644 --- a/src/plug_manager/ui.rs +++ b/src/plug_manager/ui.rs @@ -3,15 +3,13 @@ use gtk::prelude::*; use super::manager; -pub struct Ui <'a> { +pub struct Ui<'a> { manager: &'a manager::Manager, } -impl <'a> Ui <'a> { +impl<'a> Ui<'a> { pub fn new(manager: &'a manager::Manager) -> Ui<'a> { - Ui { - manager, - } + Ui { manager } } pub fn show>(&self, parent: &T) { @@ -27,9 +25,24 @@ impl <'a> Ui <'a> { let content = dlg.get_content_area(); let tabs = gtk::Notebook::new(); - let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0); - let get_plugins_lbl = gtk::Label::new("Get Plugins"); - tabs.append_page(&get_plugins, Some(&get_plugins_lbl)); + match self.get_state() { + manager::State::AlreadyLoaded => { + let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0); + let warn_lbl = gtk::Label::new( + "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", + ); + get_plugins.add(&warn_lbl); + let get_plugins_lbl = gtk::Label::new("Help"); + tabs.append_page(&get_plugins, Some(&get_plugins_lbl)); + } + manager::State::Unknown => { + let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0); + let get_plugins_lbl = gtk::Label::new("Get Plugins"); + tabs.append_page(&get_plugins, Some(&get_plugins_lbl)); + } + } let plugins = gtk::Box::new(gtk::Orientation::Vertical, 0); let plugins_lbl = gtk::Label::new("Plugins"); @@ -49,4 +62,8 @@ impl <'a> Ui <'a> { dlg.destroy(); } + + fn get_state(&self) -> manager::State { + self.manager.get_state() + } }