From e4840496580da27da31e1fc38f8fcdbf863ce4db Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Tue, 3 Sep 2019 23:14:34 +0300 Subject: [PATCH] fix panic when highlighter cache is empty --- src/highlighting/highlighter.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/highlighting/highlighter.rs b/src/highlighting/highlighter.rs index c7ea60df..63d232d6 100644 --- a/src/highlighting/highlighter.rs +++ b/src/highlighting/highlighter.rs @@ -132,7 +132,7 @@ impl<'a, 'b> Iterator for RangedHighlightIterator<'a, 'b> { (self.text.len(), ScopeStackOp::Noop) }; // println!("{} - {:?} {}:{}", self.index, self.pos, self.state.path.len(), self.state.styles.len()); - let style = *self.state.styles.last().unwrap(); + let style = *self.state.styles.last().unwrap_or(&Style::default()); let text = &self.text[self.pos..end]; let range = Range { start: self.pos, end: end }; { @@ -147,8 +147,11 @@ impl<'a, 'b> Iterator for RangedHighlightIterator<'a, 'b> { BasicScopeStackOp::Push(_) => { // we can push multiple times so this might have changed let new_cache = { - let prev_cache = m_caches.last().unwrap(); - highlighter.update_single_cache_for_push(prev_cache, cur_stack) + if let Some(prev_cache) = m_caches.last() { + highlighter.update_single_cache_for_push(prev_cache, cur_stack) + } else { + highlighter.update_single_cache_for_push(&ScoredStyle::from_style(highlighter.get_default()), cur_stack) + } }; m_styles.push(highlighter.finalize_style_with_multis(&new_cache, cur_stack)); m_caches.push(new_cache);