Starting point

This commit is contained in:
daa84 2017-07-11 18:14:46 +03:00
parent 491de706d0
commit 7a46e1cac5
2 changed files with 119 additions and 46 deletions

View File

@ -172,15 +172,24 @@ impl ModeInfo {
#[derive(Debug)]
pub struct NvimInitError {
source: Box<error::Error>,
cmd: String,
cmd: Option<String>,
}
impl NvimInitError {
pub fn new_post_init<E>(error: E) -> NvimInitError
where E: Into<Box<error::Error>>
{
NvimInitError {
cmd: None,
source: error.into(),
}
}
pub fn new<E>(cmd: &Command, error: E) -> NvimInitError
where E: Into<Box<error::Error>>
{
NvimInitError {
cmd: format!("{:?}", cmd),
cmd: Some(format!("{:?}", cmd)),
source: error.into(),
}
}
@ -189,8 +198,8 @@ impl NvimInitError {
format!("{}", self.source)
}
pub fn cmd(&self) -> &str {
&self.cmd
pub fn cmd(&self) -> Option<&String> {
self.cmd.as_ref()
}
}
@ -210,10 +219,8 @@ impl error::Error for NvimInitError {
}
}
pub fn initialize(shell: Arc<UiMutex<shell::State>>,
nvim_bin_path: Option<&String>,
cols: u64,
rows: u64)
pub fn start(shell: Arc<UiMutex<shell::State>>,
nvim_bin_path: Option<&String>)
-> result::Result<Neovim, NvimInitError> {
let mut cmd = if let Some(path) = nvim_bin_path {
Command::new(path)
@ -250,15 +257,29 @@ pub fn initialize(shell: Arc<UiMutex<shell::State>>,
nvim.session
.start_event_loop_handler(NvimHandler::new(shell));
Ok(nvim)
}
pub fn post_start_init(nvim: &mut Neovim,
open_path: Option<&String>,
cols: u64,
rows: u64)
-> result::Result<(), NvimInitError> {
let mut opts = UiAttachOptions::new();
opts.set_popupmenu_external(false);
opts.set_tabline_external(true);
nvim.ui_attach(cols, rows, opts)
.map_err(|e| NvimInitError::new(&cmd, e))?;
.map_err(|e| NvimInitError::new_post_init(e))?;
nvim.command("runtime! ginit.vim")
.map_err(|e| NvimInitError::new(&cmd, e))?;
.map_err(|e| NvimInitError::new_post_init(e))?;
Ok(nvim)
if let Some(path) = open_path {
nvim.command(&format!("e {}", path))
.map_err(|e| NvimInitError::new_post_init(e))?;
}
Ok(())
}
pub struct NvimHandler {
@ -507,11 +528,19 @@ impl RepaintMode {
enum NeovimClientWrapper {
Uninitialized,
InitInProgress,
Initialized(Neovim),
Error,
}
impl NeovimClientWrapper {
pub fn is_uninitialized(&self) -> bool {
match *self {
NeovimClientWrapper::Uninitialized => true,
_ => false,
}
}
pub fn is_initialized(&self) -> bool {
match *self {
NeovimClientWrapper::Initialized(_) => true,
@ -529,6 +558,7 @@ impl NeovimClientWrapper {
pub fn nvim(&self) -> &Neovim {
match *self {
NeovimClientWrapper::Initialized(ref nvim) => nvim,
NeovimClientWrapper::InitInProgress |
NeovimClientWrapper::Uninitialized => panic!("Access to uninitialized neovim client"),
NeovimClientWrapper::Error => {
panic!("Access to neovim client that is not started due to some error")
@ -539,6 +569,7 @@ 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 => {
panic!("Access to neovim client that is not started due to some error")
@ -568,6 +599,10 @@ impl NeovimClient {
self.nvim.is_initialized()
}
pub fn is_uninitialized(&self) -> bool {
self.nvim.is_uninitialized()
}
pub fn is_error(&self) -> bool {
self.nvim.is_error()
}

View File

@ -250,6 +250,7 @@ impl UiState {
}
}
#[derive(Clone)]
pub struct ShellOptions {
nvim_bin_path: Option<String>,
open_path: Option<String>,
@ -545,38 +546,21 @@ fn gtk_draw(state_arc: &Arc<UiMutex<State>>, ctx: &cairo::Context) -> Inhibit {
Inhibit(false)
}
fn init_nvim(state_arc: &Arc<UiMutex<State>>) {
let state = state_arc.borrow();
let mut nvim_client = state.nvim.borrow_mut();
if !nvim_client.is_initialized() && !nvim_client.is_error() {
let (cols, rows) = state.calc_nvim_size().unwrap();
let mut nvim = match nvim::initialize(state_arc.clone(),
state.options.nvim_bin_path.as_ref(),
cols as u64,
rows as u64) {
fn init_nvim_async(state_arc: Arc<UiMutex<State>>,
options: ShellOptions,
cols: usize,
rows: usize) {
// execute nvim
let mut nvim = match nvim::start(state_arc.clone(), options.nvim_bin_path.as_ref()) {
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)
});
// TODO: process error //
return;
}
};
if let Some(ref path) = state.options.open_path {
nvim.command(&format!("e {}", path)).report_err(&mut nvim);
}
// add callback on session end
let guard = nvim.session.take_dispatch_guard();
let state_ref = state_arc.clone();
thread::spawn(move || {
guard.join().expect("Can't join dispatch thread");
@ -584,7 +568,60 @@ fn init_nvim(state_arc: &Arc<UiMutex<State>>) {
idle_cb_call!(state_ref.detach_cb());
});
nvim_client.set_nvim(nvim);
// attach ui
let state_ref = state_arc.clone();
let mut post_init = Some(move || {
let mut nvim = nvim;
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 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<UiMutex<State>>) {
let state = state_arc.borrow();
if state.nvim.borrow().is_uninitialized() {
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); });
}
}
@ -1008,7 +1045,8 @@ impl RedrawEvents for State {
fn mode_info_set(&mut self,
cursor_style_enabled: bool,
mode_info: Vec<nvim::ModeInfo>) -> RepaintMode {
mode_info: Vec<nvim::ModeInfo>)
-> RepaintMode {
self.mode.set_info(cursor_style_enabled, mode_info);
RepaintMode::Nothing
}