Check vim-plug loaded

This commit is contained in:
daa 2017-10-17 21:34:49 +03:00
parent a3eba12b24
commit b0477c36f6
3 changed files with 49 additions and 19 deletions

View File

@ -505,17 +505,24 @@ fn call(
Ok(repaint_mode)
}
pub trait ErrorReport {
pub trait ErrorReport<T> {
fn report_err(&self, nvim: &mut NeovimApi);
fn ok_and_report(&self, nvim: &mut NeovimApi) -> Option<&T>;
}
impl<T> ErrorReport for result::Result<T, CallError> {
impl<T> ErrorReport<T> for result::Result<T, CallError> {
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)]

View File

@ -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<Rc<RefCell<NeovimClient>>>,
nvim: Option<Rc<RefCell<NeovimClient>>>,
}
impl Manager {
pub fn new() -> Self {
Manager {
nvim: None,
}
Manager { nvim: None }
}
pub fn initialize(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
@ -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
}
}

View File

@ -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<T: IsA<gtk::Window>>(&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()
}
}