on master help sign in
Range based highlight_iter. Only works on limiting the start right now
commitb120515613ba24aff224d093b28d85049c774cad authorBlaž Hrastnik <blaz@mxxn.io> dateSat 19 Sep 2020, 11:55 (6 years ago) parent3859f69
2 files changed+22−3
helix-core/src/syntax.rs+10−2 helix-term/src/editor.rs+12−1
helix-core/src/syntax.rs+10−2
@@ -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();
helix-term/src/editor.rs+12−1
@@ -104,13 +104,24 @@ impl Editor {
104104 state.doc().len_lines() - 1,
105105 );
106106
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+
107118 // TODO: cache highlight results
108119 // TODO: only recalculate when state.doc is actually modified
109120 let highlights: Vec<_> = state
110121 .syntax
111122 .as_mut()
112123 .unwrap()
113− .highlight_iter(source_code.as_bytes(), None, |_| None)
124+ .highlight_iter(source_code.as_bytes(), Some(range), None, |_| None)
114125 .unwrap()
115126 .collect(); // TODO: we collect here to avoid double borrow, fix later
116127