Skip to content

Commit

Permalink
Don't panic on ParallelCommand errors
Browse files Browse the repository at this point in the history
Instead log the errors to the logfile
  • Loading branch information
fredizzimo committed Oct 12, 2023
1 parent 5c2349b commit 60a6c1b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub async fn setup_intro_message_autocommand(
pub async fn show_intro_message(
nvim: &Neovim<NeovimWriter>,
message: &[String],
) -> Result<Value, Box<CallError>> {
) -> Result<(), Box<CallError>> {
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<Dimensions>) -> Result<NeovimSession> {
Expand Down
191 changes: 104 additions & 87 deletions src/bridge/ui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -129,108 +130,124 @@ pub enum ParallelCommand {
},
}

async fn display_awailable_fonts(
nvim: &Neovim<NeovimWriter>,
fonts: Vec<String>,
) -> Result<(), Box<CallError>> {
let mut content: Vec<String> = vec![
"What follows are the font names available for guifont. You can try any of them with <CR> in normal mode.",
"",
"To switch to one of them, use one of them, type:",
"",
" :set guifont=<font name>:h<font size>",
"",
"where <font name> is one of the following with spaces escaped",
"and <font size> 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 <buffer> <CR> <cmd>lua vim.opt.guifont=vim.fn.getline('.')<CR>")
.await?;
Ok(())
}

#[cfg(windows)]
async fn register_right_click(nvim: &Neovim<NeovimWriter>) -> Result<(), Box<CallError>> {
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<NeovimWriter>) -> Result<(), Box<CallError>> {
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<NeovimWriter>) {
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<String> = vec![
"What follows are the font names available for guifont. You can try any of them with <CR> in normal mode.",
"",
"To switch to one of them, use one of them, type:",
"",
" :set guifont=<font name>:h<font size>",
"",
"where <font name> is one of the following with spaces escaped",
"and <font size> 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 <buffer> <CR> <cmd>lua vim.opt.guifont=vim.fn.getline('.')<CR>",
)
.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);
}
}
}
Expand Down

0 comments on commit 60a6c1b

Please sign in to comment.