on master help sign in
minor: Remove a few unwraps.
commitad3325db8e6dce3a10b9f8e0319ab9814c7ade1b authorBlaž Hrastnik <blaz@mxxn.io> dateFri 26 Mar 2021, 11:02 (6 years ago) parentcf0e191
6 files changed+11−13
helix-core/src/indent.rs+1−1 helix-core/src/match_brackets.rs+1−1 helix-core/src/object.rs+1−1 helix-core/src/syntax.rs+4−4 helix-term/src/commands.rs+3−3 helix-term/src/ui/prompt.rs+1−3
helix-core/src/indent.rs+1−1
@@ -24,7 +24,7 @@ fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
2424/// Find the highest syntax node at position.
2525/// This is to identify the column where this node (e.g., an HTML closing tag) ends.
2626fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Node> {
27− let tree = syntax.root_layer.tree.as_ref().unwrap();
27+ let tree = syntax.tree();
2828
2929 // named_descendant
3030 let mut node = match tree.root_node().descendant_for_byte_range(pos, pos) {
helix-core/src/match_brackets.rs+1−1
@@ -5,7 +5,7 @@ use crate::{Range, Rope, Selection, Syntax};
55
66#[must_use]
77pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
8− let tree = syntax.root_layer.tree.as_ref().unwrap();
8+ let tree = syntax.tree();
99
1010 let byte_pos = doc.char_to_byte(pos);
1111
helix-core/src/object.rs+1−1
@@ -4,7 +4,7 @@ use smallvec::smallvec;
44// TODO: to contract_selection we'd need to store the previous ranges before expand.
55// Maybe just contract to the first child node?
66pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: &Selection) -> Selection {
7− let tree = syntax.root_layer.tree.as_ref().unwrap();
7+ let tree = syntax.tree();
88
99 selection.transform(|range| {
1010 let from = text.char_to_byte(range.from());
helix-core/src/syntax.rs+4−4
@@ -172,7 +172,7 @@ thread_local! {
172172pub struct Syntax {
173173 config: Arc<HighlightConfiguration>,
174174
175− pub(crate) root_layer: LanguageLayer,
175+ root_layer: LanguageLayer,
176176}
177177
178178fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<str> {
@@ -251,7 +251,7 @@ impl Syntax {
251251 //
252252 // fn parse(language, old_tree, ranges)
253253 //
254− fn tree(&self) -> &Tree {
254+ pub fn tree(&self) -> &Tree {
255255 self.root_layer.tree()
256256 }
257257 //
@@ -363,7 +363,7 @@ impl LanguageLayer {
363363 // Self { tree: None }
364364 // }
365365
366− fn tree(&self) -> &Tree {
366+ pub fn tree(&self) -> &Tree {
367367 // TODO: no unwrap
368368 self.tree.as_ref().unwrap()
369369 }
@@ -1566,7 +1566,7 @@ fn test_parser() {
15661566 ",
15671567 );
15681568 let syntax = Syntax::new(&source, Arc::new(config));
1569− let tree = syntax.root_layer.tree.unwrap();
1569+ let tree = syntax.tree();
15701570 let root = tree.root_node();
15711571 assert_eq!(root.kind(), "source_file");
15721572
helix-term/src/commands.rs+3−3
@@ -831,12 +831,12 @@ pub fn buffer_picker(cx: &mut Context) {
831831 .collect(),
832832 move |(id, path): &(DocumentId, Option<PathBuf>)| {
833833 // format_fn
834− match path {
834+ match path.as_ref().and_then(|path| path.to_str()) {
835835 Some(path) => {
836836 if *id == current {
837− format!("{} (*)", path.to_str().unwrap()).into()
837+ format!("{} (*)", path).into()
838838 } else {
839− path.to_str().unwrap().into()
839+ path.into()
840840 }
841841 }
842842 None => "[NEW]".into(),
helix-term/src/ui/prompt.rs+1−3
@@ -130,9 +130,7 @@ impl Prompt {
130130 theme.get("ui.statusline"),
131131 );
132132 for (i, (_range, completion)) in self.completion.iter().enumerate() {
133− let color = if self.completion_selection_index.is_some()
134− && i == self.completion_selection_index.unwrap()
135− {
133+ let color = if Some(i) == self.completion_selection_index {
136134 Style::default().bg(Color::Rgb(104, 60, 232))
137135 } else {
138136 text_color