on master help sign in
Range based highlight_iter. Only works on limiting the start right now
b120515Blaž Hrastnik
helix-core/src/syntax.rs+10−2
@@ -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) };
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+
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 {
119
126
120 let mut result = HighlightIter {
127 let mut result = HighlightIter {
121 source,
128 source,
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);
1109+ return None;
1102 };
1110 };
1103
1111
1104 let (mut match_, capture_index) = layer.captures.next().unwrap();
1112 let (mut match_, capture_index) = layer.captures.next().unwrap();
helix-term/src/editor.rs+12−1
@@ -104,13 +104,24 @@ impl Editor {
104 state.doc().len_lines() - 1,
104 state.doc().len_lines() - 1,
105 );
105 );
106
106
107+ let range = {
108+ // calculate viewport byte ranges
109+ let start = state.doc().line_to_byte(self.first_line.into());
110+ let end = state.doc().line_to_byte(last_line)
111+ + state.doc().line(last_line).len_bytes();
112+
113+ start..end
114+ };
115+
116+ // TODO: range doesn't actually restrict source, just highlight range
117+
107 // TODO: cache highlight results
118 // TODO: cache highlight results
108 // TODO: only recalculate when state.doc is actually modified
119 // TODO: only recalculate when state.doc is actually modified
109 let highlights: Vec<_> = state
120 let highlights: Vec<_> = state
110 .syntax
121 .syntax
111 .as_mut()
122 .as_mut()
112 .unwrap()
123 .unwrap()
113− .highlight_iter(source_code.as_bytes(), None, |_| None)
124+ .highlight_iter(source_code.as_bytes(), Some(range), None, |_| None)
114 .unwrap()
125 .unwrap()
115 .collect(); // TODO: we collect here to avoid double borrow, fix later
126 .collect(); // TODO: we collect here to avoid double borrow, fix later
116
127