diff --git a/src/nvim/client.rs b/src/nvim/client.rs index cbd8150..a70b98b 100644 --- a/src/nvim/client.rs +++ b/src/nvim/client.rs @@ -2,7 +2,8 @@ use std::ops::{Deref, DerefMut}; use std::cell::{Cell, RefCell, RefMut}; use std::sync::{Arc, Mutex, MutexGuard}; -use neovim_lib::Neovim; +use super::ErrorReport; +use neovim_lib::{Neovim, NeovimApi}; #[derive(Clone, Copy, PartialEq)] enum NeovimClientState { @@ -31,6 +32,17 @@ impl<'a> NeovimRef<'a> { None } } + + pub fn non_blocked(mut self) -> Option { + self.get_mode().ok_and_report().and_then(|mode| { + mode.iter() + .find(|kv| { + kv.0.as_str().map(|key| key == "blocking").unwrap_or(false) + }) + .map(|kv| kv.1.as_bool().unwrap_or(false)) + .and_then(|block| if block { None } else { Some(self) }) + }) + } } impl<'a> Deref for NeovimRef<'a> { diff --git a/src/nvim/ext.rs b/src/nvim/ext.rs index 9a69507..8dc7283 100644 --- a/src/nvim/ext.rs +++ b/src/nvim/ext.rs @@ -1,42 +1,23 @@ use std::result; -use super::client::NeovimRef; -use neovim_lib::{NeovimApi, CallError}; +use neovim_lib::CallError; pub trait ErrorReport { - fn report_err(&self, nvim: &mut NeovimApi); + fn report_err(&self); - fn ok_and_report(self, nvim: &mut NeovimApi) -> Option; + fn ok_and_report(self) -> Option; } impl ErrorReport for result::Result { - fn report_err(&self, _: &mut NeovimApi) { + fn report_err(&self) { if let Err(ref err) = *self { error!("{}", err); - //nvim.report_error(&err_msg).expect("Error report error :)"); } } - fn ok_and_report(self, nvim: &mut NeovimApi) -> Option { - self.report_err(nvim); + fn ok_and_report(self) -> Option { + self.report_err(); self.ok() } } - -pub trait NeovimExt: Sized { - fn non_blocked(self) -> Option; -} - -impl <'a>NeovimExt for NeovimRef<'a> { - fn non_blocked(mut self) -> Option { - self.get_mode().ok_and_report(&mut *self).and_then(|mode| { - mode.iter() - .find(|kv| { - kv.0.as_str().map(|key| key == "blocking").unwrap_or(false) - }) - .map(|kv| kv.1.as_bool().unwrap_or(false)) - .and_then(|block| if block { None } else { Some(self) }) - }) - } -} diff --git a/src/nvim/mod.rs b/src/nvim/mod.rs index 3f4035a..6873e1d 100644 --- a/src/nvim/mod.rs +++ b/src/nvim/mod.rs @@ -10,7 +10,7 @@ pub use self::redraw_handler::{RedrawEvents, GuiApi}; pub use self::repaint_mode::RepaintMode; pub use self::client::{NeovimClient, NeovimClientAsync, NeovimRef}; pub use self::mode_info::{ModeInfo, CursorShape}; -pub use self::ext::{ErrorReport, NeovimExt}; +pub use self::ext::ErrorReport; use std::error; use std::fmt; diff --git a/src/plug_manager/vim_plug.rs b/src/plug_manager/vim_plug.rs index 45034e4..e30e5b0 100644 --- a/src/plug_manager/vim_plug.rs +++ b/src/plug_manager/vim_plug.rs @@ -70,7 +70,7 @@ impl Manager { if let Some(mut nvim) = self.nvim() { let loaded_plug = nvim.eval("exists('g:loaded_plug')"); loaded_plug - .ok_and_report(&mut *nvim) + .ok_and_report() .and_then(|loaded_plug| loaded_plug.as_i64()) .map_or(false, |loaded_plug| if loaded_plug > 0 { true @@ -84,9 +84,7 @@ impl Manager { pub fn reload(&self, path: &str) { if let Some(mut nvim) = self.nvim() { - nvim.command(&format!("source {}", path)).report_err( - &mut *nvim, - ); + nvim.command(&format!("source {}", path)).report_err(); } } } diff --git a/src/popup_menu.rs b/src/popup_menu.rs index fdb7224..1dec3fa 100644 --- a/src/popup_menu.rs +++ b/src/popup_menu.rs @@ -286,7 +286,7 @@ fn tree_button_press(tree: >k::TreeView, ev: &EventButton, nvim: &mut Neovim) } apply_command.push_str(""); - nvim.input(&apply_command).report_err(nvim); + nvim.input(&apply_command).report_err(); } Inhibit(false) diff --git a/src/project.rs b/src/project.rs index 9d554bb..8ce293e 100644 --- a/src/project.rs +++ b/src/project.rs @@ -394,7 +394,7 @@ fn list_old_files(nvim: &mut Neovim) -> Vec { } } err @ Err(_) => { - err.report_err(nvim); + err.report_err(); vec![] } } @@ -427,7 +427,7 @@ impl EntryStore { println!("Error get current directory"); } } - err @ Err(_) => err.report_err(nvim), + err @ Err(_) => err.report_err(), } let old_files = list_old_files(nvim); diff --git a/src/shell.rs b/src/shell.rs index 7d6f09a..13e0081 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -21,7 +21,7 @@ use neovim_lib::neovim_api::Tabpage; use settings::{Settings, FontSource}; use ui_model::{UiModel, Attrs, ModelRect}; use color::{ColorModel, Color, COLOR_BLACK, COLOR_WHITE, COLOR_RED}; -use nvim::{self, RedrawEvents, GuiApi, RepaintMode, ErrorReport, NeovimExt, NeovimClient, +use nvim::{self, RedrawEvents, GuiApi, RepaintMode, ErrorReport, NeovimClient, NeovimRef, NeovimClientAsync}; use input; use input::keyval_to_input_string; @@ -122,7 +122,7 @@ impl State { /// /// Note that this call also do neovim api call get_mode pub fn nvim_non_blocked(&self) -> Option { - self.nvim().and_then(NeovimExt::non_blocked) + self.nvim().and_then(NeovimRef::non_blocked) } pub fn nvim(&self) -> Option { @@ -177,20 +177,20 @@ impl State { pub fn open_file(&self, path: &str) { if let Some(mut nvim) = self.nvim() { - nvim.command(&format!("e {}", path)).report_err(&mut *nvim); + nvim.command(&format!("e {}", path)).report_err(); } } pub fn cd(&self, path: &str) { if let Some(mut nvim) = self.nvim() { - nvim.command(&format!("cd {}", path)).report_err(&mut *nvim); + nvim.command(&format!("cd {}", path)).report_err(); } } fn close_popup_menu(&self) { if self.popup_menu.borrow().is_open() { if let Some(mut nvim) = self.nvim() { - nvim.input("").report_err(&mut *nvim); + nvim.input("").report_err(); } } } @@ -298,7 +298,7 @@ impl State { gtk::timeout_add(250, move || { resize_state.set(ResizeState::NvimResizeRequest(columns, rows)); - if let Some(mut nvim) = nvim.nvim() { + if let Some(mut nvim) = nvim.nvim().and_then(NeovimRef::non_blocked) { if let Err(err) = nvim.ui_try_resize(columns as u64, rows as u64) { error!("Error trying resize nvim {}", err); } @@ -341,10 +341,10 @@ impl State { if let Some(mut nvim) = nvim { if self.mode.is(&mode::NvimMode::Insert) || self.mode.is(&mode::NvimMode::Normal) { let paste_code = format!("normal! \"{}P", clipboard); - nvim.command(&paste_code).report_err(&mut *nvim); + nvim.command(&paste_code).report_err(); } else { let paste_code = format!("{}", clipboard); - nvim.input(&paste_code).report_err(&mut *nvim); + nvim.input(&paste_code).report_err(); }; } @@ -583,7 +583,7 @@ impl Shell { let nvim = state.nvim(); if let Some(mut nvim) = nvim { - nvim.command(":wa").report_err(&mut *nvim); + nvim.command(":wa").report_err(); } } @@ -615,7 +615,7 @@ impl Deref for Shell { fn gtk_focus_in(state: &mut State) -> Inhibit { if let Some(mut nvim) = state.nvim_non_blocked() { nvim.command("if exists('#FocusGained') | doautocmd FocusGained | endif") - .report_err(&mut *nvim); + .report_err(); } state.im_context.focus_in(); @@ -628,7 +628,7 @@ fn gtk_focus_in(state: &mut State) -> Inhibit { fn gtk_focus_out(state: &mut State) -> Inhibit { if let Some(mut nvim) = state.nvim_non_blocked() { nvim.command("if exists('#FocusLost') | doautocmd FocusLost | endif") - .report_err(&mut *nvim); + .report_err(); } state.im_context.focus_out(); diff --git a/src/tabline.rs b/src/tabline.rs index b6c58af..49ead25 100644 --- a/src/tabline.rs +++ b/src/tabline.rs @@ -34,7 +34,7 @@ impl State { let target = &self.data[idx as usize]; if Some(target) != self.selected.as_ref() { if let Some(mut nvim) = self.nvim.as_ref().unwrap().nvim() { - nvim.set_current_tabpage(target).report_err(&mut *nvim); + nvim.set_current_tabpage(target).report_err(); } } } diff --git a/src/theme.rs b/src/theme.rs index 637a9f9..92c7d92 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -55,7 +55,7 @@ fn get_hl_color(map: &HashMap<&str, &Value>, color_name: &str) -> Option fn get_hl_colors(nvim: &mut Neovim, hl: &str) -> (Option, Option) { nvim.get_hl_by_name(hl, true) - .ok_and_report(nvim) + .ok_and_report() .and_then(|m| if let Some(m) = m.to_attrs_map_report() { Some(( get_hl_color(&m, "background"),