Skip to content

Commit

Permalink
Merge pull request #569 from alexpasmantier/refactor/convenience-high…
Browse files Browse the repository at this point in the history
…light-from-previous-state

refactor: add convenience `HighlightLines::from_state` constructor
  • Loading branch information
keith-hall authored Jan 24, 2025
2 parents fd1a925 + b2811ce commit 7fe13c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ impl<'a> HighlightLines<'a> {
HighlightIterator::new(&mut self.highlight_state, &ops[..], line, &self.highlighter);
Ok(iter.collect())
}

/// This starts again from a previous state, useful for highlighting a file incrementally for
/// which you've cached the highlight and parse state.
pub fn from_state(
theme: &'a Theme,
highlight_state: HighlightState,
parse_state: ParseState,
) -> HighlightLines<'a> {
HighlightLines {
highlighter: Highlighter::new(theme),
parse_state,
highlight_state,
}
}

/// Returns the current highlight and parse states, useful for caching and incremental highlighting.
pub fn state(self) -> (HighlightState, ParseState) {
(self.highlight_state, self.parse_state)
}
}

/// Convenience struct containing everything you need to highlight a file
Expand Down Expand Up @@ -357,4 +376,38 @@ mod tests {
assert_eq!(all_ops.count(), iterated_ops.len() - 1); // -1 because we want to ignore the NOOP
}
}

#[cfg(all(feature = "default-syntaxes", feature = "default-themes"))]
#[test]
fn can_start_again_from_previous_state() {
let ss = SyntaxSet::load_defaults_nonewlines();
let ts = ThemeSet::load_defaults();
let mut highlighter = HighlightLines::new(
ss.find_syntax_by_extension("py").unwrap(),
&ts.themes["base16-ocean.dark"],
);

let lines = ["\"\"\"", "def foo():", "\"\"\""];

let highlighted_first_line = highlighter
.highlight_line(lines[0], &ss)
.expect("#[cfg(test)]");

let (highlight_state, parse_state) = highlighter.state();

let mut other_highlighter = HighlightLines::from_state(
&ts.themes["base16-ocean.dark"],
highlight_state,
parse_state,
);

let highlighted_second_line = other_highlighter
.highlight_line(lines[1], &ss)
.expect("#[cfg(test)]");

// Check that the second line is highlighted correctly (i.e. as a docstring)
// using the first line's previous state
assert!(highlighted_second_line.len() == 1);
assert!(highlighted_second_line[0].0 == highlighted_first_line[0].0);
}
}
2 changes: 2 additions & 0 deletions tests/snapshots/public_api__public_api.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ impl<'a> core::panic::unwind_safe::RefUnwindSafe for syntect::easy::HighlightFil
impl<'a> core::panic::unwind_safe::UnwindSafe for syntect::easy::HighlightFile<'a>
pub struct syntect::easy::HighlightLines<'a>
impl<'a> syntect::easy::HighlightLines<'a>
pub fn syntect::easy::HighlightLines<'a>::from_state(theme: &'a syntect::highlighting::Theme, highlight_state: syntect::highlighting::HighlightState, parse_state: syntect::parsing::ParseState) -> syntect::easy::HighlightLines<'a>
pub fn syntect::easy::HighlightLines<'a>::highlight<'b>(&mut self, line: &'b str, syntax_set: &syntect::parsing::SyntaxSet) -> alloc::vec::Vec<(syntect::highlighting::Style, &'b str)>
pub fn syntect::easy::HighlightLines<'a>::highlight_line<'b>(&mut self, line: &'b str, syntax_set: &syntect::parsing::SyntaxSet) -> core::result::Result<alloc::vec::Vec<(syntect::highlighting::Style, &'b str)>, syntect::Error>
pub fn syntect::easy::HighlightLines<'a>::new(syntax: &syntect::parsing::SyntaxReference, theme: &'a syntect::highlighting::Theme) -> syntect::easy::HighlightLines<'a>
pub fn syntect::easy::HighlightLines<'a>::state(self) -> (syntect::highlighting::HighlightState, syntect::parsing::ParseState)
impl<'a> !core::marker::Send for syntect::easy::HighlightLines<'a>
impl<'a> !core::marker::Sync for syntect::easy::HighlightLines<'a>
impl<'a> core::marker::Unpin for syntect::easy::HighlightLines<'a>
Expand Down

0 comments on commit 7fe13c0

Please sign in to comment.