on master help sign in
Ignore layers without highlight captures, avoid cloning ranges
72eb2ceBlaž Hrastnik
helix-core/src/syntax.rs+13−11
@@ -638,7 +638,7 @@ impl Syntax {
638 let mut layers = self
638 let mut layers = self
639 .layers
639 .layers
640 .iter()
640 .iter()
641− .map(|(_, layer)| {
641+ .flat_map(|(_, layer)| {
642 // Reuse a cursor from the pool if available.
642 // Reuse a cursor from the pool if available.
643 let mut cursor = PARSER.with(|ts_parser| {
643 let mut cursor = PARSER.with(|ts_parser| {
644 let highlighter = &mut ts_parser.borrow_mut();
644 let highlighter = &mut ts_parser.borrow_mut();
@@ -656,7 +656,7 @@ impl Syntax {
656 // cursor_ref.set_byte_range(range.clone().unwrap_or(0..usize::MAX));
656 // cursor_ref.set_byte_range(range.clone().unwrap_or(0..usize::MAX));
657 cursor_ref.set_byte_range(0..usize::MAX);
657 cursor_ref.set_byte_range(0..usize::MAX);
658
658
659− let captures = cursor_ref
659+ let mut captures = cursor_ref
660 .captures(
660 .captures(
661 &layer.config.query,
661 &layer.config.query,
662 layer.tree().root_node(),
662 layer.tree().root_node(),
@@ -664,7 +664,12 @@ impl Syntax {
664 )
664 )
665 .peekable();
665 .peekable();
666
666
667+ // If there's no captures, skip the layer
668+ if captures.peek().is_none() {
669+ return None;
670+ }
671+
667− HighlightIterLayer {
672+ Some(HighlightIterLayer {
668 highlight_end_stack: Vec::new(),
673 highlight_end_stack: Vec::new(),
669 scope_stack: vec![LocalScope {
674 scope_stack: vec![LocalScope {
670 inherits: false,
675 inherits: false,
@@ -674,10 +679,10 @@ impl Syntax {
674 cursor,
679 cursor,
675 _tree: None,
680 _tree: None,
676 captures,
681 captures,
677− config: layer.config.as_ref(), // TODO: just reuse
682+ config: layer.config.as_ref(), // TODO: just reuse `layer`
678− depth: layer.depth, // TODO: just reuse
683+ depth: layer.depth, // TODO: just reuse `layer`
679− ranges: layer.ranges.clone(),
684+ ranges: &layer.ranges, // TODO: temp
680− }
685+ })
681 })
686 })
682 .collect::<Vec<_>>();
687 .collect::<Vec<_>>();
683
688
@@ -701,9 +706,6 @@ impl Syntax {
701 last_highlight_range: None,
706 last_highlight_range: None,
702 };
707 };
703 result.sort_layers();
708 result.sort_layers();
704− for layer in &result.layers {
705− log::info!("> {:?} {:?}", layer.depth, layer.ranges); // <- for some reason layers are reversed here
706− }
707 result
709 result
708 }
710 }
709 // on_tokenize
711 // on_tokenize
@@ -987,7 +989,7 @@ struct HighlightIterLayer<'a> {
987 highlight_end_stack: Vec<usize>,
989 highlight_end_stack: Vec<usize>,
988 scope_stack: Vec<LocalScope<'a>>,
990 scope_stack: Vec<LocalScope<'a>>,
989 depth: usize,
991 depth: usize,
990− ranges: Vec<Range>, // TEMP
992+ ranges: &'a [Range],
991}
993}
992
994
993impl<'a> fmt::Debug for HighlightIterLayer<'a> {
995impl<'a> fmt::Debug for HighlightIterLayer<'a> {