syntax: Hide the TSParser internally, borrowing when needed.

imgbot
Blaž Hrastnik 4 years ago
parent 6cbfb050e2
commit 3445abf88e

@ -234,7 +234,6 @@ impl Syntax {
/// Iterate over the highlighted regions for a given slice of source code.
pub fn highlight_iter<'a>(
&self,
ts_parser: &'a mut TSParser,
source: &'a [u8],
range: Option<std::ops::Range<usize>>,
cancellation_flag: Option<&'a AtomicUsize>,
@ -288,7 +287,6 @@ impl Syntax {
byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
injection_callback,
cancellation_flag,
highlighter: ts_parser,
iter_count: 0,
layers: vec![layer],
next_event: None,
@ -659,7 +657,6 @@ where
{
source: &'a [u8],
byte_offset: usize,
highlighter: &'a mut TSParser,
injection_callback: F,
cancellation_flag: Option<&'a AtomicUsize>,
layers: Vec<HighlightIterLayer<'a>>,
@ -846,7 +843,6 @@ impl<'a> HighlightIterLayer<'a> {
/// added to the returned vector.
fn new<F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a>(
source: &'a [u8],
highlighter: &mut TSParser,
cancellation_flag: Option<&'a AtomicUsize>,
injection_callback: &mut F,
mut config: &'a HighlightConfiguration,
@ -858,6 +854,9 @@ impl<'a> HighlightIterLayer<'a> {
loop {
// --> Tree parsing part
PARSER.with(|ts_parser| {
let highlighter = &mut ts_parser.borrow_mut();
if highlighter.parser.set_included_ranges(&ranges).is_ok() {
highlighter
.parser
@ -874,16 +873,25 @@ impl<'a> HighlightIterLayer<'a> {
// Process combined injections.
if let Some(combined_injections_query) = &config.combined_injections_query {
let mut injections_by_pattern_index =
vec![(None, Vec::new(), false); combined_injections_query.pattern_count()];
let matches =
cursor.matches(combined_injections_query, tree.root_node(), |n: Node| {
&source[n.byte_range()]
});
let mut injections_by_pattern_index = vec![
(None, Vec::new(), false);
combined_injections_query
.pattern_count()
];
let matches = cursor.matches(
combined_injections_query,
tree.root_node(),
|n: Node| &source[n.byte_range()],
);
for mat in matches {
let entry = &mut injections_by_pattern_index[mat.pattern_index];
let (language_name, content_node, include_children) =
injection_for_match(config, combined_injections_query, &mat, source);
injection_for_match(
config,
combined_injections_query,
&mat,
source,
);
if language_name.is_some() {
entry.0 = language_name;
}
@ -892,9 +900,11 @@ impl<'a> HighlightIterLayer<'a> {
}
entry.2 = include_children;
}
for (lang_name, content_nodes, includes_children) in injections_by_pattern_index
for (lang_name, content_nodes, includes_children) in
injections_by_pattern_index
{
if let (Some(lang_name), false) = (lang_name, content_nodes.is_empty())
{
if let (Some(lang_name), false) = (lang_name, content_nodes.is_empty()) {
if let Some(next_config) = (injection_callback)(lang_name) {
let ranges = Self::intersect_ranges(
&ranges,
@ -939,6 +949,9 @@ impl<'a> HighlightIterLayer<'a> {
});
}
Ok(()) // so we can use the try operator
})?;
if queue.is_empty() {
break;
} else {
@ -1114,7 +1127,10 @@ where
break;
} else {
let layer = self.layers.remove(0);
self.highlighter.cursors.push(layer.cursor);
PARSER.with(|ts_parser| {
let highlighter = &mut ts_parser.borrow_mut();
highlighter.cursors.push(layer.cursor);
});
}
}
}
@ -1228,7 +1244,6 @@ where
if !ranges.is_empty() {
match HighlightIterLayer::new(
self.source,
self.highlighter,
self.cancellation_flag,
&mut self.injection_callback,
config,

@ -92,18 +92,10 @@ impl EditorView {
// TODO: only recalculate when state.doc is actually modified
let highlights: Vec<_> = match &view.doc.syntax {
Some(syntax) => {
syntax::PARSER.with(|ts_parser| {
syntax
.highlight_iter(
&mut ts_parser.borrow_mut(),
source_code.as_bytes(),
Some(range),
None,
|_| None,
)
.highlight_iter(source_code.as_bytes(), Some(range), None, |_| None)
.unwrap()
.collect() // TODO: we collect here to avoid holding the lock, fix later
})
}
None => vec![Ok(HighlightEvent::Source {
start: range.start,

Loading…
Cancel
Save