From ad3325db8e6dce3a10b9f8e0319ab9814c7ade1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Fri, 26 Mar 2021 11:02:32 +0900 Subject: [PATCH] minor: Remove a few unwraps. --- helix-core/src/indent.rs | 2 +- helix-core/src/match_brackets.rs | 2 +- helix-core/src/object.rs | 2 +- helix-core/src/syntax.rs | 8 ++++---- helix-term/src/commands.rs | 6 +++--- helix-term/src/ui/prompt.rs | 4 +--- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 7fedb7d3..4bd644d4 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -24,7 +24,7 @@ fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize { /// Find the highest syntax node at position. /// This is to identify the column where this node (e.g., an HTML closing tag) ends. fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option { - let tree = syntax.root_layer.tree.as_ref().unwrap(); + let tree = syntax.tree(); // named_descendant let mut node = match tree.root_node().descendant_for_byte_range(pos, pos) { diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index f641d094..fd161776 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -5,7 +5,7 @@ use crate::{Range, Rope, Selection, Syntax}; #[must_use] pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option { - let tree = syntax.root_layer.tree.as_ref().unwrap(); + let tree = syntax.tree(); let byte_pos = doc.char_to_byte(pos); diff --git a/helix-core/src/object.rs b/helix-core/src/object.rs index 19ff9d96..1c644fb2 100644 --- a/helix-core/src/object.rs +++ b/helix-core/src/object.rs @@ -4,7 +4,7 @@ use smallvec::smallvec; // TODO: to contract_selection we'd need to store the previous ranges before expand. // Maybe just contract to the first child node? pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: &Selection) -> Selection { - let tree = syntax.root_layer.tree.as_ref().unwrap(); + let tree = syntax.tree(); selection.transform(|range| { let from = text.char_to_byte(range.from()); diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index a6b1cf61..42bfb855 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -172,7 +172,7 @@ thread_local! { pub struct Syntax { config: Arc, - pub(crate) root_layer: LanguageLayer, + root_layer: LanguageLayer, } fn byte_range_to_str(range: std::ops::Range, source: RopeSlice) -> Cow { @@ -251,7 +251,7 @@ impl Syntax { // // fn parse(language, old_tree, ranges) // - fn tree(&self) -> &Tree { + pub fn tree(&self) -> &Tree { self.root_layer.tree() } // @@ -363,7 +363,7 @@ impl LanguageLayer { // Self { tree: None } // } - fn tree(&self) -> &Tree { + pub fn tree(&self) -> &Tree { // TODO: no unwrap self.tree.as_ref().unwrap() } @@ -1566,7 +1566,7 @@ fn test_parser() { ", ); let syntax = Syntax::new(&source, Arc::new(config)); - let tree = syntax.root_layer.tree.unwrap(); + let tree = syntax.tree(); let root = tree.root_node(); assert_eq!(root.kind(), "source_file"); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index dd7de06e..5fdf6a0a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -831,12 +831,12 @@ pub fn buffer_picker(cx: &mut Context) { .collect(), move |(id, path): &(DocumentId, Option)| { // format_fn - match path { + match path.as_ref().and_then(|path| path.to_str()) { Some(path) => { if *id == current { - format!("{} (*)", path.to_str().unwrap()).into() + format!("{} (*)", path).into() } else { - path.to_str().unwrap().into() + path.into() } } None => "[NEW]".into(), diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 8b3a1ca2..c61f0bd1 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -130,9 +130,7 @@ impl Prompt { theme.get("ui.statusline"), ); for (i, (_range, completion)) in self.completion.iter().enumerate() { - let color = if self.completion_selection_index.is_some() - && i == self.completion_selection_index.unwrap() - { + let color = if Some(i) == self.completion_selection_index { Style::default().bg(Color::Rgb(104, 60, 232)) } else { text_color