minor: Remove a few unwraps.

imgbot
Blaž Hrastnik 4 years ago
parent cf0e191a6a
commit ad3325db8e

@ -24,7 +24,7 @@ fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
/// Find the highest syntax node at position. /// Find the highest syntax node at position.
/// This is to identify the column where this node (e.g., an HTML closing tag) ends. /// 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<Node> { fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Node> {
let tree = syntax.root_layer.tree.as_ref().unwrap(); let tree = syntax.tree();
// named_descendant // named_descendant
let mut node = match tree.root_node().descendant_for_byte_range(pos, pos) { let mut node = match tree.root_node().descendant_for_byte_range(pos, pos) {

@ -5,7 +5,7 @@ use crate::{Range, Rope, Selection, Syntax};
#[must_use] #[must_use]
pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> { pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option<usize> {
let tree = syntax.root_layer.tree.as_ref().unwrap(); let tree = syntax.tree();
let byte_pos = doc.char_to_byte(pos); let byte_pos = doc.char_to_byte(pos);

@ -4,7 +4,7 @@ use smallvec::smallvec;
// TODO: to contract_selection we'd need to store the previous ranges before expand. // TODO: to contract_selection we'd need to store the previous ranges before expand.
// Maybe just contract to the first child node? // Maybe just contract to the first child node?
pub fn expand_selection(syntax: &Syntax, text: RopeSlice, selection: &Selection) -> Selection { 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| { selection.transform(|range| {
let from = text.char_to_byte(range.from()); let from = text.char_to_byte(range.from());

@ -172,7 +172,7 @@ thread_local! {
pub struct Syntax { pub struct Syntax {
config: Arc<HighlightConfiguration>, config: Arc<HighlightConfiguration>,
pub(crate) root_layer: LanguageLayer, root_layer: LanguageLayer,
} }
fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<str> { fn byte_range_to_str(range: std::ops::Range<usize>, source: RopeSlice) -> Cow<str> {
@ -251,7 +251,7 @@ impl Syntax {
// //
// fn parse(language, old_tree, ranges) // fn parse(language, old_tree, ranges)
// //
fn tree(&self) -> &Tree { pub fn tree(&self) -> &Tree {
self.root_layer.tree() self.root_layer.tree()
} }
// //
@ -363,7 +363,7 @@ impl LanguageLayer {
// Self { tree: None } // Self { tree: None }
// } // }
fn tree(&self) -> &Tree { pub fn tree(&self) -> &Tree {
// TODO: no unwrap // TODO: no unwrap
self.tree.as_ref().unwrap() self.tree.as_ref().unwrap()
} }
@ -1566,7 +1566,7 @@ fn test_parser() {
", ",
); );
let syntax = Syntax::new(&source, Arc::new(config)); let syntax = Syntax::new(&source, Arc::new(config));
let tree = syntax.root_layer.tree.unwrap(); let tree = syntax.tree();
let root = tree.root_node(); let root = tree.root_node();
assert_eq!(root.kind(), "source_file"); assert_eq!(root.kind(), "source_file");

@ -831,12 +831,12 @@ pub fn buffer_picker(cx: &mut Context) {
.collect(), .collect(),
move |(id, path): &(DocumentId, Option<PathBuf>)| { move |(id, path): &(DocumentId, Option<PathBuf>)| {
// format_fn // format_fn
match path { match path.as_ref().and_then(|path| path.to_str()) {
Some(path) => { Some(path) => {
if *id == current { if *id == current {
format!("{} (*)", path.to_str().unwrap()).into() format!("{} (*)", path).into()
} else { } else {
path.to_str().unwrap().into() path.into()
} }
} }
None => "[NEW]".into(), None => "[NEW]".into(),

@ -130,9 +130,7 @@ impl Prompt {
theme.get("ui.statusline"), theme.get("ui.statusline"),
); );
for (i, (_range, completion)) in self.completion.iter().enumerate() { for (i, (_range, completion)) in self.completion.iter().enumerate() {
let color = if self.completion_selection_index.is_some() let color = if Some(i) == self.completion_selection_index {
&& i == self.completion_selection_index.unwrap()
{
Style::default().bg(Color::Rgb(104, 60, 232)) Style::default().bg(Color::Rgb(104, 60, 232))
} else { } else {
text_color text_color

Loading…
Cancel
Save