on master help sign in
Remove a couple more unwraps
commit23b5b1e25aca1dbed87fef18cc72b16852ba40a8 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 21 Jun 2022, 05:47 (4 years ago) parent19dccad
5 files changed+21−10
helix-term/src/commands.rs+6−3 helix-term/src/commands/lsp.rs+9−2 helix-term/src/commands/typed.rs+4−2 helix-term/src/ui/mod.rs+1−2 helix-term/src/ui/prompt.rs+1−1
helix-term/src/commands.rs+6−3
@@ -1683,8 +1683,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
16831683 let scrolloff = config.scrolloff;
16841684 let (view, doc) = current!(cx.editor);
16851685 let registers = &cx.editor.registers;
1686− if let Some(query) = registers.read('/') {
1687− let query = query.last().unwrap();
1686+ if let Some(query) = registers.read('/').and_then(|query| query.last()) {
16881687 let contents = doc.text().slice(..).to_string();
16891688 let search_config = &config.search;
16901689 let case_insensitive = if search_config.smart_case {
@@ -2124,7 +2123,11 @@ fn append_mode(cx: &mut Context) {
21242123 // Make sure there's room at the end of the document if the last
21252124 // selection butts up against it.
21262125 let end = text.len_chars();
2127− let last_range = doc.selection(view.id).iter().last().unwrap();
2126+ let last_range = doc
2127+ .selection(view.id)
2128+ .iter()
2129+ .last()
2130+ .expect("selection should always have at least one range");
21282131 if !last_range.is_empty() && last_range.head == end {
21292132 let transaction = Transaction::change(
21302133 doc.text(),
helix-term/src/commands/lsp.rs+9−2
@@ -61,7 +61,14 @@ fn jump_to_location(
6161 return;
6262 }
6363 };
64− let _id = editor.open(&path, action).expect("editor.open failed");
64+ match editor.open(&path, action) {
65+ Ok(_) => (),
66+ Err(err) => {
67+ let err = format!("failed to open path: {:?}: {:?}", location.uri, err);
68+ editor.set_error(err);
69+ return;
70+ }
71+ }
6572 let (view, doc) = current!(editor);
6673 let definition_pos = location.range.start;
6774 // TODO: convert inside server
@@ -491,7 +498,7 @@ fn goto_impl(
491498 locations: Vec<lsp::Location>,
492499 offset_encoding: OffsetEncoding,
493500) {
494− let cwdir = std::env::current_dir().expect("couldn't determine current directory");
501+ let cwdir = std::env::current_dir().unwrap_or_default();
495502
496503 match locations.as_slice() {
497504 [location] => {
helix-term/src/commands/typed.rs+4−2
@@ -1,3 +1,5 @@
1+use std::ops::Deref;
2+
13use super::*;
24
35use helix_view::editor::{Action, ConfigEvent};
@@ -961,7 +963,7 @@ fn get_option(
961963 let key = &args[0].to_lowercase();
962964 let key_error = || anyhow::anyhow!("Unknown key `{}`", key);
963965
964− let config = serde_json::to_value(&cx.editor.config().clone()).unwrap();
966+ let config = serde_json::json!(cx.editor.config().deref());
965967 let pointer = format!("/{}", key.replace('.', "/"));
966968 let value = config.pointer(&pointer).ok_or_else(key_error)?;
967969
@@ -984,7 +986,7 @@ fn set_option(
984986 let key_error = || anyhow::anyhow!("Unknown key `{}`", key);
985987 let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", arg);
986988
987− let mut config = serde_json::to_value(&cx.editor.config().clone()).unwrap();
989+ let mut config = serde_json::json!(&cx.editor.config().deref());
988990 let pointer = format!("/{}", key.replace('.', "/"));
989991 let value = config.pointer_mut(&pointer).ok_or_else(key_error)?;
990992
helix-term/src/ui/mod.rs+1−2
@@ -263,8 +263,7 @@ pub mod completers {
263263
264264 pub fn setting(_editor: &Editor, input: &str) -> Vec<Completion> {
265265 static KEYS: Lazy<Vec<String>> = Lazy::new(|| {
266− serde_json::to_value(Config::default())
267− .unwrap()
266+ serde_json::json!(Config::default())
268267 .as_object()
269268 .unwrap()
270269 .keys()
helix-term/src/ui/prompt.rs+1−1
@@ -443,7 +443,7 @@ impl Prompt {
443443 let input: Cow<str> = if self.line.is_empty() {
444444 // latest value in the register list
445445 self.history_register
446− .and_then(|reg| cx.editor.registers.first(reg).cloned()) // TODO: can we avoid cloning?
446+ .and_then(|reg| cx.editor.registers.first(reg))
447447 .map(|entry| entry.into())
448448 .unwrap_or_else(|| Cow::from(""))
449449 } else {