Skip to content

Commit

Permalink
Don't panic if api_info doesn't exist on gui_event, make Option
Browse files Browse the repository at this point in the history
[(Lyude: also, use map_or() for guessing channel ID for shutdown())]

Resolves #51
Closes #49
  • Loading branch information
jacobmischka authored and Lyude committed Dec 21, 2022
1 parent 8eb1253 commit 0dde28d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
9 changes: 2 additions & 7 deletions src/nvim/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,8 @@ impl NeovimClient {
self.nvim.write().unwrap().replace(nvim);
}

pub fn api_info(&self) -> Rc<NeovimApiInfo> {
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<Rc<NeovimApiInfo>> {
self.state.borrow().api_info.as_ref().cloned()
}

pub fn set_initialized(&self, api_info: NeovimApiInfo) {
Expand Down
36 changes: 32 additions & 4 deletions src/nvim/redraw_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)?,
Expand Down
4 changes: 2 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 0dde28d

Please sign in to comment.