on master help sign in
Ignore layers without highlight captures, avoid cloning ranges
commit72eb2ce1f17650834ec4e09ee43c70f9ef55be9a authorBlaž Hrastnik <blaz@mxxn.io> dateMon 3 Jan 2022, 15:35 (5 years ago) parent5135fa3
1 file changed+13−11
helix-core/src/syntax.rs+13−11
helix-core/src/syntax.rs+13−11
@@ -638,7 +638,7 @@ impl Syntax {
638638 let mut layers = self
639639 .layers
640640 .iter()
641− .map(|(_, layer)| {
641+ .flat_map(|(_, layer)| {
642642 // Reuse a cursor from the pool if available.
643643 let mut cursor = PARSER.with(|ts_parser| {
644644 let highlighter = &mut ts_parser.borrow_mut();
@@ -656,7 +656,7 @@ impl Syntax {
656656 // cursor_ref.set_byte_range(range.clone().unwrap_or(0..usize::MAX));
657657 cursor_ref.set_byte_range(0..usize::MAX);
658658
659− let captures = cursor_ref
659+ let mut captures = cursor_ref
660660 .captures(
661661 &layer.config.query,
662662 layer.tree().root_node(),
@@ -664,7 +664,12 @@ impl Syntax {
664664 )
665665 .peekable();
666666
667− HighlightIterLayer {
667+ // If there's no captures, skip the layer
668+ if captures.peek().is_none() {
669+ return None;
670+ }
671+
672+ Some(HighlightIterLayer {
668673 highlight_end_stack: Vec::new(),
669674 scope_stack: vec![LocalScope {
670675 inherits: false,
@@ -674,10 +679,10 @@ impl Syntax {
674679 cursor,
675680 _tree: None,
676681 captures,
677− config: layer.config.as_ref(), // TODO: just reuse
678− depth: layer.depth, // TODO: just reuse
679− ranges: layer.ranges.clone(),
680− }
682+ config: layer.config.as_ref(), // TODO: just reuse `layer`
683+ depth: layer.depth, // TODO: just reuse `layer`
684+ ranges: &layer.ranges, // TODO: temp
685+ })
681686 })
682687 .collect::<Vec<_>>();
683688
@@ -701,9 +706,6 @@ impl Syntax {
701706 last_highlight_range: None,
702707 };
703708 result.sort_layers();
704− for layer in &result.layers {
705− log::info!("> {:?} {:?}", layer.depth, layer.ranges); // <- for some reason layers are reversed here
706− }
707709 result
708710 }
709711 // on_tokenize
@@ -987,7 +989,7 @@ struct HighlightIterLayer<'a> {
987989 highlight_end_stack: Vec<usize>,
988990 scope_stack: Vec<LocalScope<'a>>,
989991 depth: usize,
990− ranges: Vec<Range>, // TEMP
992+ ranges: &'a [Range],
991993}
992994
993995impl<'a> fmt::Debug for HighlightIterLayer<'a> {