@@ -77,6 +77,7 @@ impl Syntax {
77 pub fn highlight_iter<'a>(
77 pub fn highlight_iter<'a>(
78 &'a mut self,
78 &'a mut self,
79 source: &'a [u8],
79 source: &'a [u8],
80+ range: Option<std::ops::Range<usize>>,
80 cancellation_flag: Option<&'a AtomicUsize>,
81 cancellation_flag: Option<&'a AtomicUsize>,
81 mut injection_callback: impl FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
82 mut injection_callback: impl FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
82 ) -> Result<impl Iterator<Item = Result<HighlightEvent, Error>> + 'a, Error> {
83 ) -> Result<impl Iterator<Item = Result<HighlightEvent, Error>> + 'a, Error> {
@@ -90,6 +91,12 @@ impl Syntax {
90 let query_ref = unsafe { mem::transmute::<_, &'static mut Query>(&mut self.config.query) };
91 let query_ref = unsafe { mem::transmute::<_, &'static mut Query>(&mut self.config.query) };
91 let config_ref =
92 let config_ref =
92 unsafe { mem::transmute::<_, &'static HighlightConfiguration>(&self.config) };
93 unsafe { mem::transmute::<_, &'static HighlightConfiguration>(&self.config) };
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);
93 let captures = cursor_ref
100 let captures = cursor_ref
94 .captures(query_ref, tree_ref.root_node(), move |n: Node| {
101 .captures(query_ref, tree_ref.root_node(), move |n: Node| {
95 &source[n.byte_range()]
102 &source[n.byte_range()]
@@ -119,7 +126,7 @@ impl Syntax {
120 let mut result = HighlightIter {
127 let mut result = HighlightIter {
122− byte_offset: 0,
129+ byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
123 injection_callback,
130 injection_callback,
124 cancellation_flag,
131 cancellation_flag,
125 highlighter: self,
132 highlighter: self,
@@ -1098,7 +1105,8 @@ where
1098 layer.highlight_end_stack.pop();
1105 layer.highlight_end_stack.pop();
1099 return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
1106 return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
1100 } else {
1107 } else {
1101− return self.emit_event(self.source.len(), None);
1108+ // return self.emit_event(self.source.len(), None);
1104 let (mut match_, capture_index) = layer.captures.next().unwrap();
1112 let (mut match_, capture_index) = layer.captures.next().unwrap();