Update vim configuration on save

source configuration one Ok pressed
This commit is contained in:
daa
2017-11-04 23:04:03 +03:00
parent afa282833a
commit 5345d178bf
9 changed files with 113 additions and 38 deletions

View File

@@ -7,14 +7,13 @@ use super::store::{Store, PlugInfo};
use nvim::NeovimClient;
pub struct Manager {
vim_plug: vim_plug::Manager,
pub vim_plug: vim_plug::Manager,
pub store: Store,
pub plug_manage_state: PlugManageState,
}
impl Manager {
pub fn new() -> Self {
let (plug_manage_state, store) = if Store::is_config_exists() {
(PlugManageState::NvimGtk, Store::load())
} else {

View File

@@ -28,22 +28,41 @@ impl<'a> Builder<'a> {
list.set_selection_mode(gtk::SelectionMode::None);
let path = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let label = gtk::Label::new("Repo");
let entry = gtk::Entry::new();
let path_lbl = gtk::Label::new("Repo");
let path_e = gtk::Entry::new();
path.pack_start(&label, true, true, 0);
path.pack_end(&entry, false, true, 0);
path.pack_start(&path_lbl, true, true, 0);
path.pack_end(&path_e, false, true, 0);
list.add(&path);
let name = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let name_lbl = gtk::Label::new("Name");
let name_e = gtk::Entry::new();
name.pack_start(&name_lbl, true, true, 0);
name.pack_end(&name_e, false, true, 0);
list.add(&name);
content.add(&list);
content.show_all();
let ok: i32 = gtk::ResponseType::Ok.into();
let res = if dlg.run() == ok {
entry.get_text().map(|name| {
store::PlugInfo::new(name.to_owned(), name.to_owned())
path_e.get_text().map(|path| {
let name = name_e
.get_text()
.and_then(|name| if name.trim().is_empty() {
None
} else {
Some(name)
})
.or_else(|| Builder::extract_name(&path))
.unwrap_or_else(|| path.clone());
store::PlugInfo::new(name.to_owned(), path.to_owned())
})
} else {
None
@@ -53,4 +72,30 @@ impl<'a> Builder<'a> {
res
}
fn extract_name(path: &str) -> Option<String> {
if let Some(idx) = path.rfind(|c| c == '/' || c == '\\') {
if idx < path.len() - 1 {
let path = path.trim_right_matches(".git");
Some(path[idx + 1..].to_owned())
} else {
None
}
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_name() {
assert_eq!(
Some("plugin_name"),
Builder::extract_name("http://github.com/somebody/plugin_name.git")
);
}
}

View File

@@ -13,6 +13,7 @@ use super::manager;
use super::store::{Store, PlugInfo};
use super::plugin_settings_dlg;
use super::vimawesome;
use nvim_config::NvimConfig;
pub struct Ui<'a> {
manager: &'a Arc<UiMutex<manager::Manager>>,
@@ -78,17 +79,20 @@ impl<'a> Ui<'a> {
add_help_tab(
&pages,
&format!("NeovimGtk plugin manager is a GUI for vim-plug.\n\
&format!(
"NeovimGtk plugin manager is a GUI for vim-plug.\n\
It can load plugins from vim-plug configuration if vim-plug sarted and self settings is empty.\n\
When enabled it generate and load vim-plug as simple vim file at startup before init.vim is processed.\n\
So after enabling this manager <b>you must disable vim-plug</b> configuration in init.vim.\n\
This manager currently only manage vim-plug configuration and do not any actions on plugin management.\n\
So you must call all vim-plug (PlugInstall, PlugUpdate, PlugClean) commands manually.\n\
Current configuration source is <b>{}</b>", match self.manager.borrow().plug_manage_state {
manager::PlugManageState::NvimGtk => "config file",
manager::PlugManageState::VimPlug => "loaded from vim-plug",
manager::PlugManageState::Unknown => "Unknown",
}),
Current configuration source is <b>{}</b>",
match self.manager.borrow().plug_manage_state {
manager::PlugManageState::NvimGtk => "config file",
manager::PlugManageState::VimPlug => "loaded from vim-plug",
manager::PlugManageState::Unknown => "Unknown",
}
),
);
let manager_ref = self.manager.clone();
@@ -111,6 +115,11 @@ impl<'a> Ui<'a> {
let mut manager = self.manager.borrow_mut();
manager.clear_removed();
manager.save();
if let Some(config_path) = NvimConfig::new(manager.load_config()).generate_config() {
if let Some(path) = config_path.to_str() {
manager.vim_plug.reload(path);
}
}
}
dlg.destroy();

View File

@@ -87,6 +87,12 @@ impl Manager {
false
}
}
pub fn reload(&self, path: &str) {
if let Some(mut nvim) = self.nvim() {
nvim.command(&format!("source {}", path)).report_err(&mut *nvim);
}
}
}
#[derive(Debug)]

View File

@@ -16,9 +16,11 @@ where
F: FnOnce(io::Result<DescriptionList>) + Send + 'static,
{
thread::spawn(move || {
let mut result = Some(request(query.as_ref().map(|s| s.as_ref())));
let mut cb = Some(cb);
glib::idle_add(move || {
cb.take().unwrap()(request(query.as_ref().map(|s| s.as_ref())));
cb.take().unwrap()(result.take().unwrap());
Continue(false)
})
});
@@ -37,10 +39,14 @@ fn request(query: Option<&str>) -> io::Result<DescriptionList> {
let out = child.wait_with_output()?;
if out.status.success() {
let description_list: DescriptionList = serde_json::from_slice(&out.stdout).map_err(|e| {
io::Error::new(io::ErrorKind::Other, e)
})?;
Ok(description_list)
if out.stdout.is_empty() {
Ok(DescriptionList::empty())
} else {
let description_list: DescriptionList = serde_json::from_slice(&out.stdout).map_err(|e| {
io::Error::new(io::ErrorKind::Other, e)
})?;
Ok(description_list)
}
} else {
Err(io::Error::new(
io::ErrorKind::Other,
@@ -139,6 +145,14 @@ pub struct DescriptionList {
pub plugins: Box<[Description]>,
}
impl DescriptionList {
fn empty() -> DescriptionList {
DescriptionList {
plugins: Box::new([]),
}
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct Description {
pub name: String,