mirror of
https://github.com/helix-editor/helix
synced 2024-06-01 07:46:05 +02:00
Range based highlight_iter. Only works on limiting the start right now
This commit is contained in:
parent
3859f6963d
commit
b120515613
|
@ -77,6 +77,7 @@ fn tree(&self) -> &Tree {
|
||||||
pub fn highlight_iter<'a>(
|
pub fn highlight_iter<'a>(
|
||||||
&'a mut self,
|
&'a mut self,
|
||||||
source: &'a [u8],
|
source: &'a [u8],
|
||||||
|
range: Option<std::ops::Range<usize>>,
|
||||||
cancellation_flag: Option<&'a AtomicUsize>,
|
cancellation_flag: Option<&'a AtomicUsize>,
|
||||||
mut injection_callback: impl FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
|
mut injection_callback: impl FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
|
||||||
) -> Result<impl Iterator<Item = Result<HighlightEvent, Error>> + 'a, Error> {
|
) -> Result<impl Iterator<Item = Result<HighlightEvent, Error>> + 'a, Error> {
|
||||||
|
@ -90,6 +91,12 @@ pub fn highlight_iter<'a>(
|
||||||
let query_ref = unsafe { mem::transmute::<_, &'static mut Query>(&mut self.config.query) };
|
let query_ref = unsafe { mem::transmute::<_, &'static mut Query>(&mut self.config.query) };
|
||||||
let config_ref =
|
let config_ref =
|
||||||
unsafe { mem::transmute::<_, &'static HighlightConfiguration>(&self.config) };
|
unsafe { mem::transmute::<_, &'static HighlightConfiguration>(&self.config) };
|
||||||
|
|
||||||
|
// TODO: if reusing cursors this might need resetting
|
||||||
|
if let Some(range) = &range {
|
||||||
|
cursor_ref.set_byte_range(range.start, range.end);
|
||||||
|
}
|
||||||
|
|
||||||
let captures = cursor_ref
|
let captures = cursor_ref
|
||||||
.captures(query_ref, tree_ref.root_node(), move |n: Node| {
|
.captures(query_ref, tree_ref.root_node(), move |n: Node| {
|
||||||
&source[n.byte_range()]
|
&source[n.byte_range()]
|
||||||
|
@ -119,7 +126,7 @@ pub fn highlight_iter<'a>(
|
||||||
|
|
||||||
let mut result = HighlightIter {
|
let mut result = HighlightIter {
|
||||||
source,
|
source,
|
||||||
byte_offset: 0,
|
byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
|
||||||
injection_callback,
|
injection_callback,
|
||||||
cancellation_flag,
|
cancellation_flag,
|
||||||
highlighter: self,
|
highlighter: self,
|
||||||
|
@ -1098,7 +1105,8 @@ fn next(&mut self) -> Option<Self::Item> {
|
||||||
layer.highlight_end_stack.pop();
|
layer.highlight_end_stack.pop();
|
||||||
return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
|
return self.emit_event(end_byte, Some(HighlightEvent::HighlightEnd));
|
||||||
} else {
|
} else {
|
||||||
return self.emit_event(self.source.len(), None);
|
// return self.emit_event(self.source.len(), None);
|
||||||
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut match_, capture_index) = layer.captures.next().unwrap();
|
let (mut match_, capture_index) = layer.captures.next().unwrap();
|
||||||
|
|
|
@ -104,13 +104,24 @@ fn render(&mut self) {
|
||||||
state.doc().len_lines() - 1,
|
state.doc().len_lines() - 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let range = {
|
||||||
|
// calculate viewport byte ranges
|
||||||
|
let start = state.doc().line_to_byte(self.first_line.into());
|
||||||
|
let end = state.doc().line_to_byte(last_line)
|
||||||
|
+ state.doc().line(last_line).len_bytes();
|
||||||
|
|
||||||
|
start..end
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: range doesn't actually restrict source, just highlight range
|
||||||
|
|
||||||
// TODO: cache highlight results
|
// TODO: cache highlight results
|
||||||
// TODO: only recalculate when state.doc is actually modified
|
// TODO: only recalculate when state.doc is actually modified
|
||||||
let highlights: Vec<_> = state
|
let highlights: Vec<_> = state
|
||||||
.syntax
|
.syntax
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.highlight_iter(source_code.as_bytes(), None, |_| None)
|
.highlight_iter(source_code.as_bytes(), Some(range), None, |_| None)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect(); // TODO: we collect here to avoid double borrow, fix later
|
.collect(); // TODO: we collect here to avoid double borrow, fix later
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue