@@ -65,6 +65,32 @@ impl LanguageLayer {
6565// -- refactored from tree-sitter-highlight to be able to retain state
6666// TODO: add seek() to iter
6767
68+// problem: any time a layer is updated it must update it's injections on the parent (potentially
69+// removing some from use)
70+// can't modify to vec and exist in it at the same time since that would violate borrows
71+// maybe we can do with an arena
72+// maybe just caching on the top layer and nevermind the injections for now?
73+//
74+// Grammar {
75+// layers: Vec<Box<Layer>> to prevent memory moves when vec is modified
76+// }
77+// injections tracked by marker:
78+// if marker areas match it's fine and update
79+// if not found add new layer
80+// if length 0 then area got removed, clean up the layer
81+//
82+// layer update:
83+// if range.len = 0 then remove the layer
84+// for change in changes { tree.edit(change) }
85+// tree = parser.parse(.., tree, ..)
86+// calculate affected range and update injections
87+// injection update:
88+// look for existing injections
89+// if present, range = (first injection start, last injection end)
90+//
91+// For now cheat and just throw out non-root layers if they exist. This should still improve
92+// parsing in majority of cases.
93+
6894use std::sync::atomic::{AtomicUsize, Ordering};
6995use std::{iter, mem, ops, str, usize};
7096use tree_sitter::{
@@ -73,8 +99,6 @@ use tree_sitter::{
7399};
74100
75101const CANCELLATION_CHECK_INTERVAL: usize = 100;
76−const BUFFER_HTML_RESERVE_CAPACITY: usize = 10 * 1024;
77−const BUFFER_LINES_RESERVE_CAPACITY: usize = 1000;
78102
79103/// Indicates which highlight should be applied to a region of source code.
80104#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -207,7 +231,7 @@ impl Highlighter {
207231 cancellation_flag,
208232 highlighter: self,
209233 iter_count: 0,
210− layers: layers,
234+ layers,
211235 next_event: None,
212236 last_highlight_range: None,
213237 };
@@ -405,7 +429,7 @@ impl<'a> HighlightIterLayer<'a> {
405429 .parse(source, None)
406430 .ok_or(Error::Cancelled)?;
407431 unsafe { highlighter.parser.set_cancellation_flag(None) };
408− let mut cursor = highlighter.cursors.pop().unwrap_or(QueryCursor::new());
432+ let mut cursor = highlighter.cursors.pop().unwrap_or_else(QueryCursor::new);
409433
410434 // Process combined injections.
411435 if let Some(combined_injections_query) = &config.combined_injections_query {
@@ -642,7 +666,7 @@ where
642666 break;
643667 }
644668 if i > 0 {
645− &self.layers[0..(i + 1)].rotate_left(1);
669+ self.layers[0..(i + 1)].rotate_left(1);
646670 }
647671 break;
648672 } else {
@@ -802,12 +826,9 @@ where
802826 local_defs: Vec::new(),
803827 };
804828 for prop in layer.config.query.property_settings(match_.pattern_index) {
805− match prop.key.as_ref() {
806− "local.scope-inherits" => {
807− scope.inherits =
808− prop.value.as_ref().map_or(true, |r| r.as_ref() == "true");
809− }
810− _ => {}
829+ if let "local.scope-inherits" = prop.key.as_ref() {
830+ scope.inherits =
831+ prop.value.as_ref().map_or(true, |r| r.as_ref() == "true");
811832 }
812833 }
813834 layer.scope_stack.push(scope);