@@ -234,7 +234,6 @@ impl Syntax {
234 /// Iterate over the highlighted regions for a given slice of source code.
234 /// Iterate over the highlighted regions for a given slice of source code.
235 pub fn highlight_iter<'a>(
235 pub fn highlight_iter<'a>(
237− ts_parser: &'a mut TSParser,
238 source: &'a [u8],
237 source: &'a [u8],
239 range: Option<std::ops::Range<usize>>,
238 range: Option<std::ops::Range<usize>>,
240 cancellation_flag: Option<&'a AtomicUsize>,
239 cancellation_flag: Option<&'a AtomicUsize>,
@@ -288,7 +287,6 @@ impl Syntax {
288 byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
287 byte_offset: range.map(|r| r.start).unwrap_or(0), // TODO: simplify
289 injection_callback,
288 injection_callback,
290 cancellation_flag,
289 cancellation_flag,
291− highlighter: ts_parser,
292 iter_count: 0,
290 iter_count: 0,
293 layers: vec![layer],
291 layers: vec![layer],
294 next_event: None,
292 next_event: None,
@@ -659,7 +657,6 @@ where
660 source: &'a [u8],
658 source: &'a [u8],
661 byte_offset: usize,
659 byte_offset: usize,
662− highlighter: &'a mut TSParser,
663 injection_callback: F,
660 injection_callback: F,
664 cancellation_flag: Option<&'a AtomicUsize>,
661 cancellation_flag: Option<&'a AtomicUsize>,
665 layers: Vec<HighlightIterLayer<'a>>,
662 layers: Vec<HighlightIterLayer<'a>>,
@@ -846,7 +843,6 @@ impl<'a> HighlightIterLayer<'a> {
846 /// added to the returned vector.
843 /// added to the returned vector.
847 fn new<F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a>(
844 fn new<F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a>(
848 source: &'a [u8],
845 source: &'a [u8],
849− highlighter: &mut TSParser,
850 cancellation_flag: Option<&'a AtomicUsize>,
846 cancellation_flag: Option<&'a AtomicUsize>,
851 injection_callback: &mut F,
847 injection_callback: &mut F,
852 mut config: &'a HighlightConfiguration,
848 mut config: &'a HighlightConfiguration,
@@ -858,86 +854,103 @@ impl<'a> HighlightIterLayer<'a> {
859 // --> Tree parsing part
855 // --> Tree parsing part
857+ PARSER.with(|ts_parser| {
858+ let highlighter = &mut ts_parser.borrow_mut();
861− if highlighter.parser.set_included_ranges(&ranges).is_ok() {
860+ if highlighter.parser.set_included_ranges(&ranges).is_ok() {
862− highlighter
861+ highlighter
864− .set_language(config.language)
863+ .set_language(config.language)
865− .map_err(|_| Error::InvalidLanguage)?;
864+ .map_err(|_| Error::InvalidLanguage)?;
867− unsafe { highlighter.parser.set_cancellation_flag(cancellation_flag) };
866+ unsafe { highlighter.parser.set_cancellation_flag(cancellation_flag) };
868− let tree = highlighter
867+ let tree = highlighter
870− .parse(source, None)
869+ .parse(source, None)
871− .ok_or(Error::Cancelled)?;
870+ .ok_or(Error::Cancelled)?;
872− unsafe { highlighter.parser.set_cancellation_flag(None) };
871+ unsafe { highlighter.parser.set_cancellation_flag(None) };
873− let mut cursor = highlighter.cursors.pop().unwrap_or_else(QueryCursor::new);
872+ let mut cursor = highlighter.cursors.pop().unwrap_or_else(QueryCursor::new);
875− // Process combined injections.
874+ // Process combined injections.
876− if let Some(combined_injections_query) = &config.combined_injections_query {
875+ if let Some(combined_injections_query) = &config.combined_injections_query {
877− let mut injections_by_pattern_index =
876+ let mut injections_by_pattern_index = vec![
877+ (None, Vec::new(), false);
878− vec![(None, Vec::new(), false); combined_injections_query.pattern_count()];
878+ combined_injections_query
879− let matches =
881+ let matches = cursor.matches(
880− cursor.matches(combined_injections_query, tree.root_node(), |n: Node| {
882+ combined_injections_query,
881− &source[n.byte_range()]
884+ |n: Node| &source[n.byte_range()],
883− for mat in matches {
886+ for mat in matches {
884− let entry = &mut injections_by_pattern_index[mat.pattern_index];
887+ let entry = &mut injections_by_pattern_index[mat.pattern_index];
885− let (language_name, content_node, include_children) =
888+ let (language_name, content_node, include_children) =
886− injection_for_match(config, combined_injections_query, &mat, source);
889+ injection_for_match(
887− if language_name.is_some() {
890+ config,
888− entry.0 = language_name;
891+ combined_injections_query,
890− if let Some(content_node) = content_node {
893+ source,
891− entry.1.push(content_node);
893− entry.2 = include_children;
895− for (lang_name, content_nodes, includes_children) in injections_by_pattern_index
897− if let (Some(lang_name), false) = (lang_name, content_nodes.is_empty()) {
898− if let Some(next_config) = (injection_callback)(lang_name) {
899− let ranges = Self::intersect_ranges(
895+ if language_name.is_some() {
896+ entry.0 = language_name;
898+ if let Some(content_node) = content_node {
899+ entry.1.push(content_node);
901+ entry.2 = include_children;
903+ for (lang_name, content_nodes, includes_children) in
904+ injections_by_pattern_index
906+ if let (Some(lang_name), false) = (lang_name, content_nodes.is_empty())
908+ if let Some(next_config) = (injection_callback)(lang_name) {
909+ let ranges = Self::intersect_ranges(
904− if !ranges.is_empty() {
914+ if !ranges.is_empty() {
905− queue.push((next_config, depth + 1, ranges));
915+ queue.push((next_config, depth + 1, ranges));
922+ // --> Highlighting query part
924+ // The `captures` iterator borrows the `Tree` and the `QueryCursor`, which
925+ // prevents them from being moved. But both of these values are really just
926+ // pointers, so it's actually ok to move them.
927+ let tree_ref = unsafe { mem::transmute::<_, &'static Tree>(&tree) };
929+ unsafe { mem::transmute::<_, &'static mut QueryCursor>(&mut cursor) };
930+ let captures = cursor_ref
931+ .captures(&config.query, tree_ref.root_node(), move |n: Node| {
932+ &source[n.byte_range()]
936+ result.push(HighlightIterLayer {
937+ highlight_end_stack: Vec::new(),
938+ scope_stack: vec![LocalScope {
940+ range: 0..usize::MAX,
941+ local_defs: Vec::new(),
912− // --> Highlighting query part
952+ Ok(()) // so we can use the try operator
914− // The `captures` iterator borrows the `Tree` and the `QueryCursor`, which
915− // prevents them from being moved. But both of these values are really just
916− // pointers, so it's actually ok to move them.
917− let tree_ref = unsafe { mem::transmute::<_, &'static Tree>(&tree) };
919− unsafe { mem::transmute::<_, &'static mut QueryCursor>(&mut cursor) };
920− let captures = cursor_ref
921− .captures(&config.query, tree_ref.root_node(), move |n: Node| {
922− &source[n.byte_range()]
926− result.push(HighlightIterLayer {
927− highlight_end_stack: Vec::new(),
928− scope_stack: vec![LocalScope {
930− range: 0..usize::MAX,
931− local_defs: Vec::new(),
942 if queue.is_empty() {
955 if queue.is_empty() {
@@ -1114,7 +1127,10 @@ where
1115 } else {
1128 } else {
1116 let layer = self.layers.remove(0);
1129 let layer = self.layers.remove(0);
1130+ PARSER.with(|ts_parser| {
1131+ let highlighter = &mut ts_parser.borrow_mut();
1117− self.highlighter.cursors.push(layer.cursor);
1132+ highlighter.cursors.push(layer.cursor);
@@ -1228,7 +1244,6 @@ where
1228 if !ranges.is_empty() {
1244 if !ranges.is_empty() {
1229 match HighlightIterLayer::new(
1245 match HighlightIterLayer::new(
1230 self.source,
1246 self.source,
1232 self.cancellation_flag,
1247 self.cancellation_flag,
1233 &mut self.injection_callback,
1248 &mut self.injection_callback,