From 0dde28d7d463b743ea155c7e2623b0ef9aeb7f0a Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Wed, 21 Dec 2022 06:07:16 -0600 Subject: [PATCH] Don't panic if api_info doesn't exist on gui_event, make Option [(Lyude: also, use map_or() for guessing channel ID for shutdown())] Resolves #51 Closes #49 --- src/nvim/client.rs | 9 ++------- src/nvim/redraw_handler.rs | 36 ++++++++++++++++++++++++++++++++---- src/shell.rs | 4 ++-- src/ui.rs | 5 ++++- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/nvim/client.rs b/src/nvim/client.rs index e5bda0a3..238f738b 100644 --- a/src/nvim/client.rs +++ b/src/nvim/client.rs @@ -104,13 +104,8 @@ impl NeovimClient { self.nvim.write().unwrap().replace(nvim); } - pub fn api_info(&self) -> Rc { - self.state - .borrow() - .api_info - .as_ref() - .expect("API info should be initialized by the time this is called") - .clone() + pub fn api_info(&self) -> Option> { + self.state.borrow().api_info.as_ref().cloned() } pub fn set_initialized(&self, api_info: NeovimApiInfo) { diff --git a/src/nvim/redraw_handler.rs b/src/nvim/redraw_handler.rs index 10e04154..f5899b50 100644 --- a/src/nvim/redraw_handler.rs +++ b/src/nvim/redraw_handler.rs @@ -239,18 +239,46 @@ pub fn call_gui_event( match opt_name.as_str() { "Popupmenu" => set_ui_opt( &nvim, - &[("ext_popupmenu", api_info.ext_popupmenu)], + &[( + "ext_popupmenu", + api_info + .as_ref() + .map(|api_info| api_info.ext_popupmenu) + .unwrap_or_default(), + )], opt_value, )?, "Tabline" => { - set_ui_opt(&nvim, &[("ext_tabline", api_info.ext_tabline)], opt_value)?; + set_ui_opt( + &nvim, + &[( + "ext_tabline", + api_info + .as_ref() + .map(|api_info| api_info.ext_tabline) + .unwrap_or_default(), + )], + opt_value, + )?; ui.set_tabline(opt_value); } "Cmdline" => set_ui_opt( &nvim, &[ - ("ext_cmdline", api_info.ext_cmdline), - ("ext_wildmenu", api_info.ext_wildmenu), + ( + "ext_cmdline", + api_info + .as_ref() + .map(|api_info| api_info.ext_cmdline) + .unwrap_or_default(), + ), + ( + "ext_wildmenu", + api_info + .as_ref() + .map(|api_info| api_info.ext_wildmenu) + .unwrap_or_default(), + ), ], opt_value, )?, diff --git a/src/shell.rs b/src/shell.rs index 33739cf7..35a426e5 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1227,12 +1227,12 @@ impl Shell { pub fn detach_ui(&self) { let state = self.state.borrow(); let nvim_client = state.nvim.clone(); + let api_info = nvim_client.api_info(); if let Some(nvim) = nvim_client.nvim() { - let api_info = nvim_client.api_info(); nvim_client.clear(); nvim.block_timeout(nvim.ui_detach()).report_err(); - nvim.block_on(nvim.shutdown(api_info.channel)); + nvim.block_on(nvim.shutdown(api_info.map_or(1, |i| i.channel))); } } diff --git a/src/ui.rs b/src/ui.rs index f046d3ce..a1115c69 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -405,7 +405,10 @@ impl Ui { let commands = commands.join("|"); let nvim_client = shell.nvim_clone(); let nvim = nvim_client.nvim().unwrap(); - let channel_id = nvim_client.api_info().channel; + let channel_id = nvim_client + .api_info() + .expect("API info should be initialized by the time this is called") + .channel; nvim.clone().spawn(async move { let res = nvim.command(&commands).await;