From 60a6c1b557f2ef9137355d98db43330cabb40860 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Thu, 12 Oct 2023 18:42:18 +0300 Subject: [PATCH] Don't panic on ParallelCommand errors Instead log the errors to the logfile --- src/bridge/mod.rs | 4 +- src/bridge/ui_commands.rs | 191 +++++++++++++++++++++----------------- 2 files changed, 106 insertions(+), 89 deletions(-) diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index d36a169d24..c9c841ffd4 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -57,11 +57,11 @@ pub async fn setup_intro_message_autocommand( pub async fn show_intro_message( nvim: &Neovim, message: &[String], -) -> Result> { +) -> Result<(), Box> { let mut args = vec![Value::from("show_intro")]; let lines = message.iter().map(|line| Value::from(line.as_str())); args.extend(lines); - nvim.exec_lua(INTRO_MESSAGE_LUA, args).await + nvim.exec_lua(INTRO_MESSAGE_LUA, args).await.map(|_| ()) } async fn launch(grid_size: Option) -> Result { diff --git a/src/bridge/ui_commands.rs b/src/bridge/ui_commands.rs index 02ed5f40b7..4eb97df96a 100644 --- a/src/bridge/ui_commands.rs +++ b/src/bridge/ui_commands.rs @@ -4,7 +4,8 @@ use std::sync::Arc; use log::error; use log::trace; -use nvim_rs::{call_args, rpc::model::IntoVal, Neovim}; +use anyhow::{Context, Result}; +use nvim_rs::{call_args, error::CallError, rpc::model::IntoVal, Neovim}; use tokio::sync::mpsc::unbounded_channel; #[cfg(windows)] @@ -129,108 +130,124 @@ pub enum ParallelCommand { }, } +async fn display_awailable_fonts( + nvim: &Neovim, + fonts: Vec, +) -> Result<(), Box> { + let mut content: Vec = vec![ + "What follows are the font names available for guifont. You can try any of them with in normal mode.", + "", + "To switch to one of them, use one of them, type:", + "", + " :set guifont=:h", + "", + "where is one of the following with spaces escaped", + "and is the desired font size. As an example:", + "", + " :set guifont=Cascadia\\ Code\\ PL:h12", + "", + "You may specify multiple fonts for fallback purposes separated by commas like so:", + "", + " :set guifont=Cascadia\\ Code\\ PL,Delugia\\ Nerd\\ Font:h12", + "", + "Make sure to add the above command when you're happy with it to your .vimrc file or similar config to make it permanent.", + "------------------------------", + "Available Fonts on this System", + "------------------------------", + ].into_iter().map(|text| text.to_owned()).collect(); + content.extend(fonts); + + nvim.command("split").await?; + nvim.command("noswapfile hide enew").await?; + nvim.command("setlocal buftype=nofile").await?; + nvim.command("setlocal bufhidden=hide").await?; + nvim.command("\"setlocal nobuflisted").await?; + nvim.command("\"lcd ~").await?; + nvim.command("file scratch").await?; + let _ = nvim + .call( + "nvim_buf_set_lines", + call_args![0i64, 0i64, -1i64, false, content], + ) + .await?; + nvim.command("nnoremap lua vim.opt.guifont=vim.fn.getline('.')") + .await?; + Ok(()) +} + +#[cfg(windows)] +async fn register_right_click(nvim: &Neovim) -> Result<(), Box> { + if unregister_rightclick() { + let msg = "Could not unregister previous menu item. Possibly already registered."; + nvim.err_writeln(msg).await?; + error!("{}", msg); + } + if !register_rightclick_directory() { + let msg = "Could not register directory context menu item. Possibly already registered."; + nvim.err_writeln(msg).await?; + error!("{}", msg); + } + if !register_rightclick_file() { + let msg = "Could not register file context menu item. Possibly already registered."; + nvim.err_writeln(msg).await?; + error!("{}", msg); + } +} + +#[cfg(windows)] +async fn unregister_right_click(nvim: &Neovim) -> Result<(), Box> { + if !unregister_rightclick() { + let msg = "Could not remove context menu items. Possibly already removed."; + nvim.err_writeln(msg).await?; + error!("{}", msg); + } +} + impl ParallelCommand { async fn execute(self, nvim: &Neovim) { - match self { - ParallelCommand::Quit => { - nvim.command( + let result = match self { + ParallelCommand::Quit => nvim + .command( "if get(g:, 'neovide_confirm_quit', 0) == 1 | confirm qa | else | qa! | endif", ) .await - .ok(); - } + .context("ConfirmQuit failed"), ParallelCommand::Resize { width, height } => nvim .ui_try_resize(width.max(10) as i64, height.max(3) as i64) .await - .expect("Resize failed"), + .context("Resize failed"), ParallelCommand::FocusLost => { - nvim.ui_set_focus(false).await.expect("Focus Lost Failed") + nvim.ui_set_focus(false).await.context("FocusLost failed") } ParallelCommand::FocusGained => { - nvim.ui_set_focus(true).await.expect("Focus Gained Failed") - } - ParallelCommand::FileDrop(path) => { - nvim.command(format!("e {path}").as_str()).await.ok(); - } - ParallelCommand::SetBackground(background) => { - nvim.command(format!("set background={}", background).as_str()) - .await - .ok(); + nvim.ui_set_focus(true).await.context("FocusGained failed") } - ParallelCommand::DisplayAvailableFonts(fonts) => { - let mut content: Vec = vec![ - "What follows are the font names available for guifont. You can try any of them with in normal mode.", - "", - "To switch to one of them, use one of them, type:", - "", - " :set guifont=:h", - "", - "where is one of the following with spaces escaped", - "and is the desired font size. As an example:", - "", - " :set guifont=Cascadia\\ Code\\ PL:h12", - "", - "You may specify multiple fonts for fallback purposes separated by commas like so:", - "", - " :set guifont=Cascadia\\ Code\\ PL,Delugia\\ Nerd\\ Font:h12", - "", - "Make sure to add the above command when you're happy with it to your .vimrc file or similar config to make it permanent.", - "------------------------------", - "Available Fonts on this System", - "------------------------------", - ].into_iter().map(|text| text.to_owned()).collect(); - content.extend(fonts); - - nvim.command("split").await.ok(); - nvim.command("noswapfile hide enew").await.ok(); - nvim.command("setlocal buftype=nofile").await.ok(); - nvim.command("setlocal bufhidden=hide").await.ok(); - nvim.command("\"setlocal nobuflisted").await.ok(); - nvim.command("\"lcd ~").await.ok(); - nvim.command("file scratch").await.ok(); - nvim.call( - "nvim_buf_set_lines", - call_args![0i64, 0i64, -1i64, false, content], - ) + ParallelCommand::FileDrop(path) => nvim + .command(format!("e {path}").as_str()) .await - .ok(); - nvim.command( - "nnoremap lua vim.opt.guifont=vim.fn.getline('.')", - ) + .context("FileDrop failed"), + ParallelCommand::SetBackground(background) => nvim + .command(format!("set background={}", background).as_str()) .await - .ok(); - } + .context("SetBackground failed"), + ParallelCommand::DisplayAvailableFonts(fonts) => display_awailable_fonts(nvim, fonts) + .await + .context("DisplayAvailableFonts failed"), #[cfg(windows)] - ParallelCommand::RegisterRightClick => { - if unregister_rightclick() { - let msg = - "Could not unregister previous menu item. Possibly already registered."; - nvim.err_writeln(msg).await.ok(); - error!("{}", msg); - } - if !register_rightclick_directory() { - let msg = "Could not register directory context menu item. Possibly already registered."; - nvim.err_writeln(msg).await.ok(); - error!("{}", msg); - } - if !register_rightclick_file() { - let msg = - "Could not register file context menu item. Possibly already registered."; - nvim.err_writeln(msg).await.ok(); - error!("{}", msg); - } - } + ParallelCommand::RegisterRightClick => register_right_click() + .await + .context("RegisterRightClick failed"), #[cfg(windows)] - ParallelCommand::UnregisterRightClick => { - if !unregister_rightclick() { - let msg = "Could not remove context menu items. Possibly already removed."; - nvim.err_writeln(msg).await.ok(); - error!("{}", msg); - } - } - ParallelCommand::ShowIntro { message } => { - show_intro_message(nvim, &message).await.ok(); - } + ParallelCommand::UnregisterRightClick => unregister_righ_click() + .await + .context("UnregisterRightClick failed"), + ParallelCommand::ShowIntro { message } => show_intro_message(nvim, &message) + .await + .context("ShowIntro failed"), + }; + + if let Err(error) = result { + log::error!("{:?}", error); } } }