Skip to content

Commit

Permalink
Merge pull request #263 from yonilevy/master
Browse files Browse the repository at this point in the history
bugfix for HighlightState with non-empty initial stack
  • Loading branch information
trishume authored Sep 17, 2019
2 parents 262bf9a + 2307dc6 commit df670a7
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/highlighting/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl HighlightState {
let mut styles = vec![highlighter.get_default()];
let mut single_caches = vec![ScoredStyle::from_style(styles[0])];
for i in 0..initial_stack.len() {
let prefix = initial_stack.bottom_n(i);
let prefix = initial_stack.bottom_n(i + 1);
let new_cache = highlighter.update_single_cache_for_push(&single_caches[i], prefix);
styles.push(highlighter.finalize_style_with_multis(&new_cache, prefix));
single_caches.push(new_cache);
Expand Down Expand Up @@ -389,6 +389,53 @@ mod tests {
"5"));
}

#[test]
fn can_parse_with_highlight_state_from_cache() {
let ps = SyntaxSet::load_from_folder("testdata/Packages").unwrap();
let mut state = {
let syntax = ps.find_syntax_by_scope(
Scope::new("source.python").unwrap()).unwrap();
ParseState::new(syntax)
};
let ts = ThemeSet::load_defaults();
let highlighter = Highlighter::new(&ts.themes["base16-ocean.dark"]);

// We start by parsing a python multiline-comment: """
let mut highlight_state = HighlightState::new(&highlighter, ScopeStack::new());
let line = r#"""""#;
let ops = state.parse_line(line, &ps);
let iter = HighlightIterator::new(&mut highlight_state, &ops[..], line, &highlighter);
let regions: Vec<(Style, &str)> = iter.collect();
let path = highlight_state.path;

// We then parse the next line with a highlight state built from the previous state
let mut highlight_state = HighlightState::new(&highlighter, path);
let line = "multiline comment";
let ops = state.parse_line(line, &ps);
let iter = HighlightIterator::new(&mut highlight_state, &ops[..], line, &highlighter);
let regions: Vec<(Style, &str)> = iter.collect();

// We expect the line to be styled as a comment.
assert_eq!(regions[0],
(Style {
foreground: Color {
// (Comment: #65737E)
r: 101,
g: 115,
b: 126,
a: 0xFF,
},
background: Color {
r: 43,
g: 48,
b: 59,
a: 0xFF,
},
font_style: FontStyle::empty(),
},
"multiline comment"));
}

// see issues #133 and #203, this test tests the fixes for those issues
#[test]
fn tricky_cases() {
Expand Down

0 comments on commit df670a7

Please sign in to comment.