Fix some compilation bugs, small optimizations
This commit is contained in:
parent
2ee2fa31be
commit
d9d4932b98
@ -14,10 +14,7 @@ enum NeovimClientState {
|
||||
|
||||
pub enum NeovimRef<'a> {
|
||||
SingleThreaded(RefMut<'a, Neovim>),
|
||||
MultiThreaded {
|
||||
guard: MutexGuard<'a, RefCell<Option<Neovim>>>,
|
||||
nvim: RefMut<'a, Option<Neovim>>,
|
||||
},
|
||||
MultiThreaded(MutexGuard<'a, Option<Neovim>>),
|
||||
}
|
||||
|
||||
impl<'a> NeovimRef<'a> {
|
||||
@ -25,21 +22,11 @@ impl<'a> NeovimRef<'a> {
|
||||
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>> {
|
||||
let guard = nvim_async.nvim.lock().unwrap();
|
||||
let nvim = guard.borrow_mut();
|
||||
|
||||
let nvim_ref = NeovimRef::MultiThreaded { guard, nvim };
|
||||
|
||||
if nvim_ref.is_some() {
|
||||
Some(nvim_ref)
|
||||
if guard.is_some() {
|
||||
Some(NeovimRef::MultiThreaded(guard))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -52,7 +39,7 @@ impl<'a> Deref for NeovimRef<'a> {
|
||||
fn deref(&self) -> &Neovim {
|
||||
match *self {
|
||||
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 {
|
||||
match *self {
|
||||
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 {
|
||||
nvim: Arc<Mutex<RefCell<Option<Neovim>>>>,
|
||||
nvim: Arc<Mutex<Option<Neovim>>>,
|
||||
}
|
||||
|
||||
impl NeovimClientAsync {
|
||||
fn new(nvim: Neovim) -> Self {
|
||||
NeovimClientAsync { nvim: Arc::new(Mutex::new(RefCell::new(Some(nvim)))) }
|
||||
fn new() -> Self {
|
||||
NeovimClientAsync { nvim: Arc::new(Mutex::new(None)) }
|
||||
}
|
||||
|
||||
pub fn borrow(&self) -> NeovimRef {
|
||||
NeovimRef::from_nvim_async(self).unwrap()
|
||||
pub fn borrow(&self) -> Option<NeovimRef> {
|
||||
NeovimRef::from_nvim_async(self)
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,7 +78,7 @@ impl Clone for NeovimClientAsync {
|
||||
pub struct NeovimClient {
|
||||
state: Cell<NeovimClientState>,
|
||||
nvim: RefCell<Option<Neovim>>,
|
||||
nvim_async: RefCell<Option<NeovimClientAsync>>,
|
||||
nvim_async: NeovimClientAsync,
|
||||
}
|
||||
|
||||
impl NeovimClient {
|
||||
@ -99,29 +86,22 @@ impl NeovimClient {
|
||||
NeovimClient {
|
||||
state: Cell::new(NeovimClientState::Uninitialized),
|
||||
nvim: RefCell::new(None),
|
||||
nvim_async: RefCell::new(None),
|
||||
nvim_async: NeovimClientAsync::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn async_to_sync(&self) {
|
||||
{
|
||||
let lock = self.nvim_async
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.expect("Nvim not initialized")
|
||||
.nvim
|
||||
.lock()
|
||||
.unwrap();
|
||||
let nvim = lock.borrow_mut().take().unwrap();
|
||||
*self.nvim.borrow_mut() = Some(nvim);
|
||||
}
|
||||
*self.nvim_async.borrow_mut() = None;
|
||||
let mut lock = self.nvim_async
|
||||
.nvim
|
||||
.lock()
|
||||
.unwrap();
|
||||
let nvim = lock.take().unwrap();
|
||||
*self.nvim.borrow_mut() = Some(nvim);
|
||||
}
|
||||
|
||||
pub fn set_nvim_async(&self, nvim: Neovim) -> NeovimClientAsync {
|
||||
let nvim_async = NeovimClientAsync::new(nvim);
|
||||
*self.nvim_async.borrow_mut() = Some(nvim_async.clone());
|
||||
nvim_async
|
||||
*self.nvim_async.nvim.lock().unwrap() = Some(nvim);
|
||||
self.nvim_async.clone()
|
||||
}
|
||||
|
||||
pub fn set_initialized(&self) {
|
||||
@ -155,12 +135,7 @@ impl NeovimClient {
|
||||
RefMut::map(nvim, |n| n.as_mut().unwrap()),
|
||||
))
|
||||
} else {
|
||||
let nvim_async = self.nvim_async.borrow();
|
||||
if let Some(ref nvim_async) = *nvim_async {
|
||||
NeovimRef::from_nvim_async(nvim_async)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
self.nvim_async.borrow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,22 +27,28 @@ impl NvimHandler {
|
||||
let mut repaint_mode = RepaintMode::Nothing;
|
||||
|
||||
for ev in params {
|
||||
if let Some(ev_args) = ev.as_array() {
|
||||
if let Some(ev_name) = ev_args[0].as_str() {
|
||||
for local_args in ev_args.iter().skip(1) {
|
||||
let args = match *local_args {
|
||||
Value::Array(ref ar) => ar.clone(),
|
||||
_ => vec![],
|
||||
};
|
||||
let call_reapint_mode =
|
||||
redraw_handler::call(ui, ev_name, &args)?;
|
||||
repaint_mode = repaint_mode.join(call_reapint_mode);
|
||||
if let Value::Array(ev_args) = ev {
|
||||
let mut args_iter = ev_args.into_iter();
|
||||
let ev_name = args_iter.next();
|
||||
if let Some(ev_name) = ev_name {
|
||||
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![],
|
||||
};
|
||||
let call_reapint_mode =
|
||||
redraw_handler::call(ui, &ev_name, &args)?;
|
||||
repaint_mode = repaint_mode.join(call_reapint_mode);
|
||||
}
|
||||
} else {
|
||||
error!("Unsupported event");
|
||||
}
|
||||
} else {
|
||||
println!("Unsupported event {:?}", ev_args);
|
||||
error!("Event name does not exists");
|
||||
}
|
||||
} else {
|
||||
println!("Unsupported event type {:?}", ev);
|
||||
error!("Unsupported event type {:?}", ev);
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,22 +58,31 @@ impl NvimHandler {
|
||||
}
|
||||
"Gui" => {
|
||||
if !params.is_empty() {
|
||||
if let Some(ev_name) = params[0].as_str().map(String::from) {
|
||||
let args = params.iter().skip(1).cloned().collect();
|
||||
self.safe_call(move |ui| {
|
||||
redraw_handler::call_gui_event(ui, &ev_name, &args)?;
|
||||
ui.on_redraw(&RepaintMode::All);
|
||||
Ok(())
|
||||
});
|
||||
let mut params_iter = params.into_iter();
|
||||
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| {
|
||||
redraw_handler::call_gui_event(
|
||||
ui,
|
||||
ev_name.as_str().ok_or_else(|| "Event name does not exists")?,
|
||||
&args,
|
||||
)?;
|
||||
ui.on_redraw(&RepaintMode::All);
|
||||
Ok(())
|
||||
});
|
||||
} else {
|
||||
error!("Unsupported event");
|
||||
}
|
||||
} else {
|
||||
println!("Unsupported event {:?}", params);
|
||||
error!("Event name does not exists");
|
||||
}
|
||||
} else {
|
||||
println!("Unsupported event {:?}", params);
|
||||
error!("Unsupported event {:?}", params);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("Notification {}({:?})", method, params);
|
||||
error!("Notification {}({:?})", method, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,7 +95,7 @@ impl NvimHandler {
|
||||
let shell = self.shell.clone();
|
||||
glib::idle_add(move || {
|
||||
if let Err(msg) = cb.take().unwrap()(&mut shell.borrow_mut()) {
|
||||
println!("Error call function: {}", msg);
|
||||
error!("Error call function: {}", msg);
|
||||
}
|
||||
glib::Continue(false)
|
||||
});
|
||||
|
@ -139,15 +139,15 @@ pub fn post_start_init(
|
||||
let mut opts = UiAttachOptions::new();
|
||||
opts.set_popupmenu_external(false);
|
||||
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,
|
||||
)?;
|
||||
nvim.borrow().command("runtime! ginit.vim").map_err(
|
||||
nvim.borrow().unwrap().command("runtime! ginit.vim").map_err(
|
||||
NvimInitError::new_post_init,
|
||||
)?;
|
||||
|
||||
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,
|
||||
)?;
|
||||
}
|
||||
|
@ -33,10 +33,7 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
fn before_show(&mut self,
|
||||
shell: &shell::State,
|
||||
menu_items: &[Vec<&str>],
|
||||
selected: i64) {
|
||||
fn before_show(&mut self, shell: &shell::State, menu_items: &[Vec<&str>], selected: i64) {
|
||||
if self.nvim.is_none() {
|
||||
self.nvim = Some(shell.nvim_clone());
|
||||
}
|
||||
@ -50,10 +47,15 @@ impl State {
|
||||
return;
|
||||
}
|
||||
|
||||
self.renderer
|
||||
.set_property_font(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_font(
|
||||
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()),
|
||||
);
|
||||
|
||||
let col_count = menu[0].len();
|
||||
let columns = self.tree.get_columns();
|
||||
@ -93,8 +95,13 @@ impl State {
|
||||
if selected >= 0 {
|
||||
let selected_path = gtk::TreePath::new_from_string(&format!("{}", selected));
|
||||
self.tree.get_selection().select_path(&selected_path);
|
||||
self.tree
|
||||
.scroll_to_cell(Some(&selected_path), None, false, 0.0, 0.0);
|
||||
self.tree.scroll_to_cell(
|
||||
Some(&selected_path),
|
||||
None,
|
||||
false,
|
||||
0.0,
|
||||
0.0,
|
||||
);
|
||||
} else {
|
||||
self.tree.get_selection().unselect_all();
|
||||
}
|
||||
@ -129,7 +136,10 @@ impl PopupMenu {
|
||||
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.show_all();
|
||||
@ -137,28 +147,33 @@ impl PopupMenu {
|
||||
|
||||
let state = Rc::new(RefCell::new(state));
|
||||
let state_ref = state.clone();
|
||||
state.borrow().tree.connect_button_press_event(move |tree, ev| {
|
||||
let state = state_ref.borrow();
|
||||
if let Some(mut nvim) = state.nvim.as_ref().unwrap().nvim() {
|
||||
tree_button_press(tree, ev, &mut *nvim)
|
||||
} else {
|
||||
Inhibit(false)
|
||||
}
|
||||
});
|
||||
state.borrow().tree.connect_button_press_event(
|
||||
move |tree, ev| {
|
||||
let state = state_ref.borrow();
|
||||
let nvim = state.nvim.as_ref().unwrap().nvim();
|
||||
if let Some(mut nvim) = nvim {
|
||||
tree_button_press(tree, ev, &mut *nvim)
|
||||
} else {
|
||||
Inhibit(false)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
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();
|
||||
popover.connect_key_press_event(move |_, ev| {
|
||||
let state = state_ref.borrow();
|
||||
let nvim = state.nvim.as_ref().unwrap();
|
||||
if let Some(mut nvim) = nvim.nvim() {
|
||||
input::gtk_key_press(&mut *nvim, ev)
|
||||
} else {
|
||||
Inhibit(false)
|
||||
}
|
||||
});
|
||||
let state = state_ref.borrow();
|
||||
let nvim = state.nvim.as_ref().unwrap().nvim();
|
||||
if let Some(mut nvim) = nvim {
|
||||
input::gtk_key_press(&mut *nvim, ev)
|
||||
} else {
|
||||
Inhibit(false)
|
||||
}
|
||||
});
|
||||
|
||||
PopupMenu {
|
||||
popover,
|
||||
@ -171,27 +186,30 @@ impl PopupMenu {
|
||||
self.open
|
||||
}
|
||||
|
||||
pub fn show(&mut self,
|
||||
shell: &shell::State,
|
||||
menu_items: &[Vec<&str>],
|
||||
selected: i64,
|
||||
x: i32,
|
||||
y: i32,
|
||||
width: i32,
|
||||
height: i32) {
|
||||
pub fn show(
|
||||
&mut self,
|
||||
shell: &shell::State,
|
||||
menu_items: &[Vec<&str>],
|
||||
selected: i64,
|
||||
x: i32,
|
||||
y: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
) {
|
||||
|
||||
self.open = true;
|
||||
|
||||
self.popover
|
||||
.set_pointing_to(>k::Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
});
|
||||
self.state
|
||||
.borrow_mut()
|
||||
.before_show(shell, menu_items, selected);
|
||||
self.popover.set_pointing_to(>k::Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
});
|
||||
self.state.borrow_mut().before_show(
|
||||
shell,
|
||||
menu_items,
|
||||
selected,
|
||||
);
|
||||
self.popover.popup()
|
||||
}
|
||||
|
||||
@ -260,18 +278,17 @@ fn on_treeview_allocate(state: Rc<RefCell<State>>) {
|
||||
let treeview_height = state.borrow().calc_treeview_height();
|
||||
|
||||
idle_add(move || {
|
||||
let state = state.borrow();
|
||||
let state = state.borrow();
|
||||
|
||||
// strange solution to make gtk assertions happy
|
||||
let previous_height = state.scroll.get_max_content_height();
|
||||
if previous_height < treeview_height {
|
||||
state.scroll.set_max_content_height(treeview_height);
|
||||
state.scroll.set_min_content_height(treeview_height);
|
||||
} else if previous_height > treeview_height {
|
||||
state.scroll.set_min_content_height(treeview_height);
|
||||
state.scroll.set_max_content_height(treeview_height);
|
||||
}
|
||||
Continue(false)
|
||||
});
|
||||
// strange solution to make gtk assertions happy
|
||||
let previous_height = state.scroll.get_max_content_height();
|
||||
if previous_height < treeview_height {
|
||||
state.scroll.set_max_content_height(treeview_height);
|
||||
state.scroll.set_min_content_height(treeview_height);
|
||||
} else if previous_height > treeview_height {
|
||||
state.scroll.set_min_content_height(treeview_height);
|
||||
state.scroll.set_max_content_height(treeview_height);
|
||||
}
|
||||
Continue(false)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -267,10 +267,6 @@ impl State {
|
||||
}
|
||||
|
||||
fn try_nvim_resize(&self) {
|
||||
if !self.nvim.is_initialized() {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 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();
|
||||
thread::spawn(move || {
|
||||
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 {
|
||||
let pair = Arc::new((Mutex::new(None), Condvar::new()));
|
||||
let pair2 = pair.clone();
|
||||
let mut nvim = Some(nvim);
|
||||
|
||||
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 mut started = lock.lock().unwrap();
|
||||
|
@ -33,7 +33,7 @@ impl State {
|
||||
fn switch_page(&self, idx: u32) {
|
||||
let target = &self.data[idx as usize];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user