From ba20088bc6d2e3c24d34dd1a8df4b2089890b69a Mon Sep 17 00:00:00 2001 From: daa84 Date: Wed, 12 Jul 2017 18:32:10 +0300 Subject: [PATCH] Fix compilation, show errors --- src/error.rs | 13 +++++++++ src/nvim.rs | 50 ++++++++++++++++++---------------- src/shell.rs | 76 +++++++++++++++++++++++++++------------------------- 3 files changed, 80 insertions(+), 59 deletions(-) diff --git a/src/error.rs b/src/error.rs index ccda4bb..de58f88 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,6 +28,19 @@ impl ErrorArea { ErrorArea { base, label } } + pub fn show_nvim_init_error(&self, err: &str) { + error!("Can't initialize nvim: {}", err); + self.label.set_markup(&format!("Can't initialize nvim:\n\ + {}\n\n\ + Possible error reasons:\n\ + ● Not supported nvim version (minimum supported version is {})\n\ + ● Error in configuration file (init.vim or ginit.vim)\n\ + ● Wrong nvim binary path \ + (right path can be passed with --nvim-bin-path=path_here)", + encode_minimal(err), shell::MINIMUM_SUPPORTED_NVIM_VERSION)); + self.base.show_all(); + } + pub fn show_nvim_start_error(&self, err: &str, cmd: &str) { error!("Can't start nvim: {}\nCommand line: {}", err, cmd); self.label.set_markup(&format!("Can't start nvim instance:\n\ diff --git a/src/nvim.rs b/src/nvim.rs index 0e4f800..a0e7185 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -526,41 +526,41 @@ impl RepaintMode { } -enum NeovimClientWrapper { +enum NeovimClientState { Uninitialized, InitInProgress, Initialized(Neovim), Error, } -impl NeovimClientWrapper { +impl NeovimClientState { pub fn is_uninitialized(&self) -> bool { match *self { - NeovimClientWrapper::Uninitialized => true, + NeovimClientState::Uninitialized => true, _ => false, } } pub fn is_initialized(&self) -> bool { match *self { - NeovimClientWrapper::Initialized(_) => true, + NeovimClientState::Initialized(_) => true, _ => false, } } pub fn is_error(&self) -> bool { match *self { - NeovimClientWrapper::Error => true, + NeovimClientState::Error => true, _ => false, } } pub fn nvim(&self) -> &Neovim { match *self { - NeovimClientWrapper::Initialized(ref nvim) => nvim, - NeovimClientWrapper::InitInProgress | - NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"), - NeovimClientWrapper::Error => { + NeovimClientState::Initialized(ref nvim) => nvim, + NeovimClientState::InitInProgress | + NeovimClientState::Uninitialized => panic!("Access to uninitialized neovim client"), + NeovimClientState::Error => { panic!("Access to neovim client that is not started due to some error") } } @@ -568,10 +568,10 @@ impl NeovimClientWrapper { pub fn nvim_mut(&mut self) -> &mut Neovim { match *self { - NeovimClientWrapper::Initialized(ref mut nvim) => nvim, - NeovimClientWrapper::InitInProgress | - NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"), - NeovimClientWrapper::Error => { + NeovimClientState::Initialized(ref mut nvim) => nvim, + NeovimClientState::InitInProgress | + NeovimClientState::Uninitialized => panic!("Access to uninitialized neovim client"), + NeovimClientState::Error => { panic!("Access to neovim client that is not started due to some error") } } @@ -579,40 +579,44 @@ impl NeovimClientWrapper { } pub struct NeovimClient { - nvim: NeovimClientWrapper, + state: NeovimClientState, } impl NeovimClient { pub fn new() -> Self { - NeovimClient { nvim: NeovimClientWrapper::Uninitialized } + NeovimClient { state: NeovimClientState::Uninitialized } } - pub fn set_nvim(&mut self, nvim: Neovim) { - self.nvim = NeovimClientWrapper::Initialized(nvim); + pub fn set_initialized(&mut self, nvim: Neovim) { + self.state = NeovimClientState::Initialized(nvim); } pub fn set_error(&mut self) { - self.nvim = NeovimClientWrapper::Error; + self.state = NeovimClientState::Error; + } + + pub fn set_in_progress(&mut self) { + self.state = NeovimClientState::InitInProgress; } pub fn is_initialized(&self) -> bool { - self.nvim.is_initialized() + self.state.is_initialized() } pub fn is_uninitialized(&self) -> bool { - self.nvim.is_uninitialized() + self.state.is_uninitialized() } pub fn is_error(&self) -> bool { - self.nvim.is_error() + self.state.is_error() } pub fn nvim(&self) -> &Neovim { - self.nvim.nvim() + self.state.nvim() } pub fn nvim_mut(&mut self) -> &mut Neovim { - self.nvim.nvim_mut() + self.state.nvim_mut() } } diff --git a/src/shell.rs b/src/shell.rs index 49a7e36..f11fd6b 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -238,6 +238,14 @@ impl State { None } + + fn show_error_area(&self) { + let stack = self.stack.clone(); + gtk::idle_add(move || { + stack.set_visible_child_name("Error"); + Continue(false) + }); + } } pub struct UiState { @@ -554,7 +562,17 @@ fn init_nvim_async(state_arc: Arc>, let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref()) { Ok(nvim) => nvim, Err(err) => { - // TODO: process error // + let source = err.source(); + let cmd = err.cmd().unwrap().to_owned(); + + glib::idle_add(move || { + let state = state_arc.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_start_error(&source, &cmd); + state.show_error_area(); + + Continue(false) + }); return; } }; @@ -569,59 +587,45 @@ fn init_nvim_async(state_arc: Arc>, }); // attach ui - let state_ref = state_arc.clone(); - let mut post_init = Some(move || { - let mut nvim = nvim; + let mut nvim = Some(nvim); + glib::idle_add(move || { + let mut nvim = nvim.take().unwrap(); if let Err(err) = nvim::post_start_init(&mut nvim, options.open_path.as_ref(), cols as u64, rows as u64) { - // TODO: process error // + let source = err.source(); + + let state_ref = state_arc.clone(); + glib::idle_add(move || { + let state = state_ref.borrow(); + state.nvim.borrow_mut().set_error(); + state.error_area.show_nvim_init_error(&source); + state.show_error_area(); + + Continue(false) + }); + } else { + let state = state_arc.borrow_mut(); + state.nvim.borrow_mut().set_initialized(nvim); } - let state = state_arc.borrow_mut(); - state.nvim.borrow_mut().set_nvim(nvim); - }); - - glib::idle_add(move || { - let cl = post_init.take(); - cl(); Continue(false) }); - - //{ - //Ok(nvim) => nvim, - //Err(err) => { - //nvim_client.set_error(); - //state - //.error_area - //.show_nvim_start_error(&err.source(), err.cmd()); - - //let stack = state.stack.clone(); - //gtk::idle_add(move || { - //stack.set_visible_child_name("Error"); - //Continue(false) - //}); - - //return; - //} - //}; - - - - //nvim_client.set_nvim(nvim); } fn init_nvim(state_arc: &Arc>) { let state = state_arc.borrow(); - if state.nvim.borrow().is_uninitialized() { + let mut nvim = state.nvim.borrow_mut(); + if nvim.is_uninitialized() { + nvim.set_in_progress(); + let (cols, rows) = state.calc_nvim_size().unwrap(); let state_arc = state_arc.clone(); let options = state.options.clone(); thread::spawn(move || { init_nvim_async(state_arc, options, cols, rows); }); - } }