From 23b5b1e25aca1dbed87fef18cc72b16852ba40a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Tue, 21 Jun 2022 05:47:48 +0900 Subject: [PATCH] Remove a couple more unwraps --- helix-term/src/commands.rs | 9 ++++++--- helix-term/src/commands/lsp.rs | 11 +++++++++-- helix-term/src/commands/typed.rs | 6 ++++-- helix-term/src/ui/mod.rs | 3 +-- helix-term/src/ui/prompt.rs | 2 +- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 9239b49ff..a07b51091 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1683,8 +1683,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir let scrolloff = config.scrolloff; let (view, doc) = current!(cx.editor); let registers = &cx.editor.registers; - if let Some(query) = registers.read('/') { - let query = query.last().unwrap(); + if let Some(query) = registers.read('/').and_then(|query| query.last()) { let contents = doc.text().slice(..).to_string(); let search_config = &config.search; let case_insensitive = if search_config.smart_case { @@ -2124,7 +2123,11 @@ fn append_mode(cx: &mut Context) { // Make sure there's room at the end of the document if the last // selection butts up against it. let end = text.len_chars(); - let last_range = doc.selection(view.id).iter().last().unwrap(); + let last_range = doc + .selection(view.id) + .iter() + .last() + .expect("selection should always have at least one range"); if !last_range.is_empty() && last_range.head == end { let transaction = Transaction::change( doc.text(), diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index b8c5e5d1f..6f75fb497 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -61,7 +61,14 @@ fn jump_to_location( return; } }; - let _id = editor.open(&path, action).expect("editor.open failed"); + match editor.open(&path, action) { + Ok(_) => (), + Err(err) => { + let err = format!("failed to open path: {:?}: {:?}", location.uri, err); + editor.set_error(err); + return; + } + } let (view, doc) = current!(editor); let definition_pos = location.range.start; // TODO: convert inside server @@ -491,7 +498,7 @@ fn goto_impl( locations: Vec, offset_encoding: OffsetEncoding, ) { - let cwdir = std::env::current_dir().expect("couldn't determine current directory"); + let cwdir = std::env::current_dir().unwrap_or_default(); match locations.as_slice() { [location] => { diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 19c6a5dc1..70f5fa9fe 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use super::*; use helix_view::editor::{Action, ConfigEvent}; @@ -961,7 +963,7 @@ fn get_option( let key = &args[0].to_lowercase(); let key_error = || anyhow::anyhow!("Unknown key `{}`", key); - let config = serde_json::to_value(&cx.editor.config().clone()).unwrap(); + let config = serde_json::json!(cx.editor.config().deref()); let pointer = format!("/{}", key.replace('.', "/")); let value = config.pointer(&pointer).ok_or_else(key_error)?; @@ -984,7 +986,7 @@ fn set_option( let key_error = || anyhow::anyhow!("Unknown key `{}`", key); let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", arg); - let mut config = serde_json::to_value(&cx.editor.config().clone()).unwrap(); + let mut config = serde_json::json!(&cx.editor.config().deref()); let pointer = format!("/{}", key.replace('.', "/")); let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 76ddaf89e..47a68a18f 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -263,8 +263,7 @@ pub mod completers { pub fn setting(_editor: &Editor, input: &str) -> Vec { static KEYS: Lazy> = Lazy::new(|| { - serde_json::to_value(Config::default()) - .unwrap() + serde_json::json!(Config::default()) .as_object() .unwrap() .keys() diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 7744a1614..beba8ce9a 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -443,7 +443,7 @@ impl Prompt { let input: Cow = if self.line.is_empty() { // latest value in the register list self.history_register - .and_then(|reg| cx.editor.registers.first(reg).cloned()) // TODO: can we avoid cloning? + .and_then(|reg| cx.editor.registers.first(reg)) .map(|entry| entry.into()) .unwrap_or_else(|| Cow::from("")) } else {