Code reorganisation

This commit is contained in:
daa84 2017-10-18 17:49:56 +03:00
parent b0477c36f6
commit fbe25e1a1c
5 changed files with 86 additions and 42 deletions

View File

@ -1,50 +1,21 @@
use std::rc::Rc;
use std::cell::{RefCell, RefMut};
use std::cell::RefCell;
use neovim_lib::{Neovim, NeovimApi};
use nvim::{NeovimClient, ErrorReport};
use super::vim_plug;
use nvim::NeovimClient;
pub struct Manager {
nvim: Option<Rc<RefCell<NeovimClient>>>,
pub vim_plug: vim_plug::Manager,
}
impl Manager {
pub fn new() -> Self {
Manager { nvim: None }
Manager {
vim_plug: vim_plug::Manager::new(),
}
}
pub fn initialize(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
self.nvim = Some(nvim);
}
fn nvim(&self) -> Option<RefMut<Neovim>> {
let nvim_client = self.nvim.as_ref().unwrap();
if nvim_client.borrow().is_initialized() {
Some(RefMut::map(nvim_client.borrow_mut(), |n| n.nvim_mut()))
} else {
None
self.vim_plug.initialize(nvim);
}
}
pub fn get_state(&self) -> State {
if let Some(mut nvim) = self.nvim() {
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
}
}
}
pub enum State {
Unknown,
AlreadyLoaded,
}

View File

@ -1,4 +1,6 @@
mod ui;
mod vim_plug;
mod store;
mod manager;
pub use self::ui::Ui;

View File

@ -0,0 +1,9 @@
pub struct Store {
}
impl Store {
pub fn new() -> Self {
Store { }
}
}

View File

@ -2,6 +2,7 @@ use gtk;
use gtk::prelude::*;
use super::manager;
use super::vim_plug;
pub struct Ui<'a> {
manager: &'a manager::Manager,
@ -26,10 +27,10 @@ impl<'a> Ui<'a> {
let tabs = gtk::Notebook::new();
match self.get_state() {
manager::State::AlreadyLoaded => {
vim_plug::State::AlreadyLoaded => {
let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
let warn_lbl = gtk::Label::new(
"Plug manager already loaded.\n\
"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",
);
@ -37,13 +38,14 @@ impl<'a> Ui<'a> {
let get_plugins_lbl = gtk::Label::new("Help");
tabs.append_page(&get_plugins, Some(&get_plugins_lbl));
}
manager::State::Unknown => {
vim_plug::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));
}
}
self.get_plugs();
let plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
let plugins_lbl = gtk::Label::new("Plugins");
tabs.append_page(&plugins, Some(&plugins_lbl));
@ -63,7 +65,11 @@ impl<'a> Ui<'a> {
dlg.destroy();
}
fn get_state(&self) -> manager::State {
self.manager.get_state()
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

@ -0,0 +1,56 @@
use std::rc::Rc;
use std::cell::{RefCell, RefMut};
use neovim_lib::{Neovim, NeovimApi};
use nvim::{NeovimClient, ErrorReport};
pub struct Manager {
nvim: Option<Rc<RefCell<NeovimClient>>>,
}
impl Manager {
pub fn new() -> Self {
Manager { nvim: None }
}
pub fn initialize(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
self.nvim = Some(nvim);
}
fn nvim(&self) -> Option<RefMut<Neovim>> {
let nvim_client = self.nvim.as_ref().unwrap();
if nvim_client.borrow().is_initialized() {
Some(RefMut::map(nvim_client.borrow_mut(), |n| n.nvim_mut()))
} else {
None
}
}
pub fn get_plugs(&self) {
if let Some(mut nvim) = self.nvim() {
let plugs = nvim.eval("g:plugs");
}
}
pub fn get_state(&self) -> State {
if let Some(mut nvim) = self.nvim() {
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
}
}
}
pub enum State {
Unknown,
AlreadyLoaded,
}