Skip to content

Commit

Permalink
Merge pull request #259 from forkeith/json_highlight_fix
Browse files Browse the repository at this point in the history
fix panic when highlighter cache is empty
  • Loading branch information
trishume authored Sep 4, 2019
2 parents 3100088 + e484049 commit 64ac9bd
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/highlighting/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
{
Expand All @@ -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);
Expand Down

0 comments on commit 64ac9bd

Please sign in to comment.