@@ -77,6 +77,7 @@ impl Syntax {
7777 pub fn highlight_iter<'a>(
7878 &'a mut self,
7979 source: &'a [u8],
80+ range: Option<std::ops::Range<usize>>,
8081 cancellation_flag: Option<&'a AtomicUsize>,
8182 mut injection_callback: impl FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
8283 ) -> Result<impl Iterator<Item = Result<HighlightEvent, Error>> + 'a, Error> {
@@ -90,6 +91,12 @@ impl Syntax {
9091 let query_ref = unsafe { mem::transmute::<_, &'static mut Query>(&mut self.config.query) };
9192 let config_ref =
9293 unsafe { mem::transmute::<_, &'static HighlightConfiguration>(&self.config) };
94+
95+ // TODO: if reusing cursors this might need resetting
96+ if let Some(range) = &range {
97+ cursor_ref.set_byte_range(range.start, range.end);
98+ }
99+
93100 let captures = cursor_ref
94101 .captures(query_ref, tree_ref.root_node(), move |n: Node| {
95102 &source[n.byte_range()]
@@ -119,7 +126,7 @@ impl Syntax {
119126
120127 let mut result = HighlightIter {
121128 source,
122− byte_offset: 0,
129+ byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
123130 injection_callback,
124131 cancellation_flag,
125132 highlighter: self,
@@ -1098,7 +1105,8 @@ where
10981105 layer.highlight_end_stack.pop();
10991106 return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
11001107 } else {
1101− return self.emit_event(self.source.len(), None);
1108+ // return self.emit_event(self.source.len(), None);
1109+ return None;
11021110 };
11031111
11041112 let (mut match_, capture_index) = layer.captures.next().unwrap();