Fix some compilation bugs, small optimizations

This commit is contained in:
daa 2017-11-11 18:56:23 +03:00
parent 2ee2fa31be
commit d9d4932b98
6 changed files with 142 additions and 138 deletions

View File

@ -14,10 +14,7 @@ enum NeovimClientState {
pub enum NeovimRef<'a> { pub enum NeovimRef<'a> {
SingleThreaded(RefMut<'a, Neovim>), SingleThreaded(RefMut<'a, Neovim>),
MultiThreaded { MultiThreaded(MutexGuard<'a, Option<Neovim>>),
guard: MutexGuard<'a, RefCell<Option<Neovim>>>,
nvim: RefMut<'a, Option<Neovim>>,
},
} }
impl<'a> NeovimRef<'a> { impl<'a> NeovimRef<'a> {
@ -25,21 +22,11 @@ impl<'a> NeovimRef<'a> {
NeovimRef::SingleThreaded(nvim) NeovimRef::SingleThreaded(nvim)
} }
fn is_some(&self) -> bool {
match *self {
NeovimRef::MultiThreaded{ref nvim, ..} => nvim.is_some(),
NeovimRef::SingleThreaded(_) => true,
}
}
fn from_nvim_async(nvim_async: &'a NeovimClientAsync) -> Option<NeovimRef<'a>> { fn from_nvim_async(nvim_async: &'a NeovimClientAsync) -> Option<NeovimRef<'a>> {
let guard = nvim_async.nvim.lock().unwrap(); let guard = nvim_async.nvim.lock().unwrap();
let nvim = guard.borrow_mut();
let nvim_ref = NeovimRef::MultiThreaded { guard, nvim }; if guard.is_some() {
Some(NeovimRef::MultiThreaded(guard))
if nvim_ref.is_some() {
Some(nvim_ref)
} else { } else {
None None
} }
@ -52,7 +39,7 @@ impl<'a> Deref for NeovimRef<'a> {
fn deref(&self) -> &Neovim { fn deref(&self) -> &Neovim {
match *self { match *self {
NeovimRef::SingleThreaded(ref nvim) => &*nvim, NeovimRef::SingleThreaded(ref nvim) => &*nvim,
NeovimRef::MultiThreaded { ref nvim, .. } => (&*nvim).as_ref().unwrap(), NeovimRef::MultiThreaded(ref nvim) => (&*nvim).as_ref().unwrap(),
} }
} }
} }
@ -61,22 +48,22 @@ impl<'a> DerefMut for NeovimRef<'a> {
fn deref_mut(&mut self) -> &mut Neovim { fn deref_mut(&mut self) -> &mut Neovim {
match *self { match *self {
NeovimRef::SingleThreaded(ref mut nvim) => &mut *nvim, NeovimRef::SingleThreaded(ref mut nvim) => &mut *nvim,
NeovimRef::MultiThreaded { ref mut nvim, .. } => (&mut *nvim).as_mut().unwrap(), NeovimRef::MultiThreaded(ref mut nvim) => (&mut *nvim).as_mut().unwrap(),
} }
} }
} }
pub struct NeovimClientAsync { pub struct NeovimClientAsync {
nvim: Arc<Mutex<RefCell<Option<Neovim>>>>, nvim: Arc<Mutex<Option<Neovim>>>,
} }
impl NeovimClientAsync { impl NeovimClientAsync {
fn new(nvim: Neovim) -> Self { fn new() -> Self {
NeovimClientAsync { nvim: Arc::new(Mutex::new(RefCell::new(Some(nvim)))) } NeovimClientAsync { nvim: Arc::new(Mutex::new(None)) }
} }
pub fn borrow(&self) -> NeovimRef { pub fn borrow(&self) -> Option<NeovimRef> {
NeovimRef::from_nvim_async(self).unwrap() NeovimRef::from_nvim_async(self)
} }
} }
@ -91,7 +78,7 @@ impl Clone for NeovimClientAsync {
pub struct NeovimClient { pub struct NeovimClient {
state: Cell<NeovimClientState>, state: Cell<NeovimClientState>,
nvim: RefCell<Option<Neovim>>, nvim: RefCell<Option<Neovim>>,
nvim_async: RefCell<Option<NeovimClientAsync>>, nvim_async: NeovimClientAsync,
} }
impl NeovimClient { impl NeovimClient {
@ -99,29 +86,22 @@ impl NeovimClient {
NeovimClient { NeovimClient {
state: Cell::new(NeovimClientState::Uninitialized), state: Cell::new(NeovimClientState::Uninitialized),
nvim: RefCell::new(None), nvim: RefCell::new(None),
nvim_async: RefCell::new(None), nvim_async: NeovimClientAsync::new(),
} }
} }
pub fn async_to_sync(&self) { pub fn async_to_sync(&self) {
{ let mut lock = self.nvim_async
let lock = self.nvim_async
.borrow()
.as_ref()
.expect("Nvim not initialized")
.nvim .nvim
.lock() .lock()
.unwrap(); .unwrap();
let nvim = lock.borrow_mut().take().unwrap(); let nvim = lock.take().unwrap();
*self.nvim.borrow_mut() = Some(nvim); *self.nvim.borrow_mut() = Some(nvim);
} }
*self.nvim_async.borrow_mut() = None;
}
pub fn set_nvim_async(&self, nvim: Neovim) -> NeovimClientAsync { pub fn set_nvim_async(&self, nvim: Neovim) -> NeovimClientAsync {
let nvim_async = NeovimClientAsync::new(nvim); *self.nvim_async.nvim.lock().unwrap() = Some(nvim);
*self.nvim_async.borrow_mut() = Some(nvim_async.clone()); self.nvim_async.clone()
nvim_async
} }
pub fn set_initialized(&self) { pub fn set_initialized(&self) {
@ -155,12 +135,7 @@ impl NeovimClient {
RefMut::map(nvim, |n| n.as_mut().unwrap()), RefMut::map(nvim, |n| n.as_mut().unwrap()),
)) ))
} else { } else {
let nvim_async = self.nvim_async.borrow(); self.nvim_async.borrow()
if let Some(ref nvim_async) = *nvim_async {
NeovimRef::from_nvim_async(nvim_async)
} else {
None
}
} }
} }
} }

View File

@ -27,22 +27,28 @@ impl NvimHandler {
let mut repaint_mode = RepaintMode::Nothing; let mut repaint_mode = RepaintMode::Nothing;
for ev in params { for ev in params {
if let Some(ev_args) = ev.as_array() { if let Value::Array(ev_args) = ev {
if let Some(ev_name) = ev_args[0].as_str() { let mut args_iter = ev_args.into_iter();
for local_args in ev_args.iter().skip(1) { let ev_name = args_iter.next();
let args = match *local_args { if let Some(ev_name) = ev_name {
Value::Array(ref ar) => ar.clone(), if let Some(ev_name) = ev_name.as_str() {
for local_args in args_iter {
let args = match local_args {
Value::Array(ar) => ar,
_ => vec![], _ => vec![],
}; };
let call_reapint_mode = let call_reapint_mode =
redraw_handler::call(ui, ev_name, &args)?; redraw_handler::call(ui, &ev_name, &args)?;
repaint_mode = repaint_mode.join(call_reapint_mode); repaint_mode = repaint_mode.join(call_reapint_mode);
} }
} else { } else {
println!("Unsupported event {:?}", ev_args); error!("Unsupported event");
} }
} else { } else {
println!("Unsupported event type {:?}", ev); error!("Event name does not exists");
}
} else {
error!("Unsupported event type {:?}", ev);
} }
} }
@ -52,22 +58,31 @@ impl NvimHandler {
} }
"Gui" => { "Gui" => {
if !params.is_empty() { if !params.is_empty() {
if let Some(ev_name) = params[0].as_str().map(String::from) { let mut params_iter = params.into_iter();
let args = params.iter().skip(1).cloned().collect(); if let Some(ev_name) = params_iter.next() {
if let Value::String(ev_name) = ev_name {
let args = params_iter.collect();
self.safe_call(move |ui| { self.safe_call(move |ui| {
redraw_handler::call_gui_event(ui, &ev_name, &args)?; redraw_handler::call_gui_event(
ui,
ev_name.as_str().ok_or_else(|| "Event name does not exists")?,
&args,
)?;
ui.on_redraw(&RepaintMode::All); ui.on_redraw(&RepaintMode::All);
Ok(()) Ok(())
}); });
} else { } else {
println!("Unsupported event {:?}", params); error!("Unsupported event");
} }
} else { } else {
println!("Unsupported event {:?}", params); error!("Event name does not exists");
}
} else {
error!("Unsupported event {:?}", params);
} }
} }
_ => { _ => {
println!("Notification {}({:?})", method, params); error!("Notification {}({:?})", method, params);
} }
} }
} }
@ -80,7 +95,7 @@ impl NvimHandler {
let shell = self.shell.clone(); let shell = self.shell.clone();
glib::idle_add(move || { glib::idle_add(move || {
if let Err(msg) = cb.take().unwrap()(&mut shell.borrow_mut()) { if let Err(msg) = cb.take().unwrap()(&mut shell.borrow_mut()) {
println!("Error call function: {}", msg); error!("Error call function: {}", msg);
} }
glib::Continue(false) glib::Continue(false)
}); });

View File

@ -139,15 +139,15 @@ pub fn post_start_init(
let mut opts = UiAttachOptions::new(); let mut opts = UiAttachOptions::new();
opts.set_popupmenu_external(false); opts.set_popupmenu_external(false);
opts.set_tabline_external(true); opts.set_tabline_external(true);
nvim.borrow().ui_attach(cols, rows, &opts).map_err( nvim.borrow().unwrap().ui_attach(cols, rows, &opts).map_err(
NvimInitError::new_post_init, NvimInitError::new_post_init,
)?; )?;
nvim.borrow().command("runtime! ginit.vim").map_err( nvim.borrow().unwrap().command("runtime! ginit.vim").map_err(
NvimInitError::new_post_init, NvimInitError::new_post_init,
)?; )?;
if let Some(path) = open_path { if let Some(path) = open_path {
nvim.borrow().command(&format!("e {}", path)).map_err( nvim.borrow().unwrap().command(&format!("e {}", path)).map_err(
NvimInitError::new_post_init, NvimInitError::new_post_init,
)?; )?;
} }

View File

@ -33,10 +33,7 @@ impl State {
} }
} }
fn before_show(&mut self, fn before_show(&mut self, shell: &shell::State, menu_items: &[Vec<&str>], selected: i64) {
shell: &shell::State,
menu_items: &[Vec<&str>],
selected: i64) {
if self.nvim.is_none() { if self.nvim.is_none() {
self.nvim = Some(shell.nvim_clone()); self.nvim = Some(shell.nvim_clone());
} }
@ -50,10 +47,15 @@ impl State {
return; return;
} }
self.renderer self.renderer.set_property_font(
.set_property_font(Some(&shell.get_font_desc().to_string())); Some(&shell.get_font_desc().to_string()),
self.renderer.set_property_foreground_rgba(Some(&shell.get_foreground().into())); );
self.renderer.set_property_background_rgba(Some(&shell.get_background().into())); self.renderer.set_property_foreground_rgba(
Some(&shell.get_foreground().into()),
);
self.renderer.set_property_background_rgba(
Some(&shell.get_background().into()),
);
let col_count = menu[0].len(); let col_count = menu[0].len();
let columns = self.tree.get_columns(); let columns = self.tree.get_columns();
@ -93,8 +95,13 @@ impl State {
if selected >= 0 { if selected >= 0 {
let selected_path = gtk::TreePath::new_from_string(&format!("{}", selected)); let selected_path = gtk::TreePath::new_from_string(&format!("{}", selected));
self.tree.get_selection().select_path(&selected_path); self.tree.get_selection().select_path(&selected_path);
self.tree self.tree.scroll_to_cell(
.scroll_to_cell(Some(&selected_path), None, false, 0.0, 0.0); Some(&selected_path),
None,
false,
0.0,
0.0,
);
} else { } else {
self.tree.get_selection().unselect_all(); self.tree.get_selection().unselect_all();
} }
@ -129,7 +136,10 @@ impl PopupMenu {
state.tree.set_can_focus(false); state.tree.set_can_focus(false);
state.scroll.set_policy(gtk::PolicyType::Never, gtk::PolicyType::Automatic); state.scroll.set_policy(
gtk::PolicyType::Never,
gtk::PolicyType::Automatic,
);
state.scroll.add(&state.tree); state.scroll.add(&state.tree);
state.scroll.show_all(); state.scroll.show_all();
@ -137,23 +147,28 @@ impl PopupMenu {
let state = Rc::new(RefCell::new(state)); let state = Rc::new(RefCell::new(state));
let state_ref = state.clone(); let state_ref = state.clone();
state.borrow().tree.connect_button_press_event(move |tree, ev| { state.borrow().tree.connect_button_press_event(
move |tree, ev| {
let state = state_ref.borrow(); let state = state_ref.borrow();
if let Some(mut nvim) = state.nvim.as_ref().unwrap().nvim() { let nvim = state.nvim.as_ref().unwrap().nvim();
if let Some(mut nvim) = nvim {
tree_button_press(tree, ev, &mut *nvim) tree_button_press(tree, ev, &mut *nvim)
} else { } else {
Inhibit(false) Inhibit(false)
} }
}); },
);
let state_ref = state.clone(); let state_ref = state.clone();
state.borrow().tree.connect_size_allocate(move |_, _| on_treeview_allocate(state_ref.clone())); state.borrow().tree.connect_size_allocate(move |_, _| {
on_treeview_allocate(state_ref.clone())
});
let state_ref = state.clone(); let state_ref = state.clone();
popover.connect_key_press_event(move |_, ev| { popover.connect_key_press_event(move |_, ev| {
let state = state_ref.borrow(); let state = state_ref.borrow();
let nvim = state.nvim.as_ref().unwrap(); let nvim = state.nvim.as_ref().unwrap().nvim();
if let Some(mut nvim) = nvim.nvim() { if let Some(mut nvim) = nvim {
input::gtk_key_press(&mut *nvim, ev) input::gtk_key_press(&mut *nvim, ev)
} else { } else {
Inhibit(false) Inhibit(false)
@ -171,27 +186,30 @@ impl PopupMenu {
self.open self.open
} }
pub fn show(&mut self, pub fn show(
&mut self,
shell: &shell::State, shell: &shell::State,
menu_items: &[Vec<&str>], menu_items: &[Vec<&str>],
selected: i64, selected: i64,
x: i32, x: i32,
y: i32, y: i32,
width: i32, width: i32,
height: i32) { height: i32,
) {
self.open = true; self.open = true;
self.popover self.popover.set_pointing_to(&gtk::Rectangle {
.set_pointing_to(&gtk::Rectangle {
x, x,
y, y,
width, width,
height, height,
}); });
self.state self.state.borrow_mut().before_show(
.borrow_mut() shell,
.before_show(shell, menu_items, selected); menu_items,
selected,
);
self.popover.popup() self.popover.popup()
} }
@ -274,4 +292,3 @@ fn on_treeview_allocate(state: Rc<RefCell<State>>) {
Continue(false) Continue(false)
}); });
} }

View File

@ -267,10 +267,6 @@ impl State {
} }
fn try_nvim_resize(&self) { fn try_nvim_resize(&self) {
if !self.nvim.is_initialized() {
return;
}
let (columns, rows) = self.calc_nvim_size(); let (columns, rows) = self.calc_nvim_size();
@ -774,7 +770,7 @@ fn init_nvim_async(
let nvim = set_nvim_to_state(state_arc.clone(), nvim); let nvim = set_nvim_to_state(state_arc.clone(), nvim);
// add callback on session end // add callback on session end
let guard = nvim.borrow().session.take_dispatch_guard(); let guard = nvim.borrow().unwrap().session.take_dispatch_guard();
let state_ref = state_arc.clone(); let state_ref = state_arc.clone();
thread::spawn(move || { thread::spawn(move || {
guard.join().expect("Can't join dispatch thread"); guard.join().expect("Can't join dispatch thread");
@ -799,9 +795,10 @@ fn init_nvim_async(
fn set_nvim_to_state(state_arc: Arc<UiMutex<State>>, nvim: Neovim) -> NeovimClientAsync { fn set_nvim_to_state(state_arc: Arc<UiMutex<State>>, nvim: Neovim) -> NeovimClientAsync {
let pair = Arc::new((Mutex::new(None), Condvar::new())); let pair = Arc::new((Mutex::new(None), Condvar::new()));
let pair2 = pair.clone(); let pair2 = pair.clone();
let mut nvim = Some(nvim);
glib::idle_add(move || { glib::idle_add(move || {
let nvim_aync = state_arc.borrow().nvim.set_nvim_async(nvim); let nvim_aync = state_arc.borrow().nvim.set_nvim_async(nvim.take().unwrap());
let &(ref lock, ref cvar) = &*pair2; let &(ref lock, ref cvar) = &*pair2;
let mut started = lock.lock().unwrap(); let mut started = lock.lock().unwrap();

View File

@ -33,7 +33,7 @@ impl State {
fn switch_page(&self, idx: u32) { fn switch_page(&self, idx: u32) {
let target = &self.data[idx as usize]; let target = &self.data[idx as usize];
if Some(target) != self.selected.as_ref() { if Some(target) != self.selected.as_ref() {
if let Some(nvim) = self.nvim.as_ref().unwrap().nvim() { 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(&mut *nvim);
} }
} }