@@ -1092,21 +1092,14 @@ impl Syntax {
1094 _tree: None,
1094 _tree: None,
1095− captures,
1095+ captures: RefCell::new(captures),
1096 config: layer.config.as_ref(), // TODO: just reuse `layer`
1096 config: layer.config.as_ref(), // TODO: just reuse `layer`
1097 depth: layer.depth, // TODO: just reuse `layer`
1097 depth: layer.depth, // TODO: just reuse `layer`
1098− ranges: &layer.ranges, // TODO: temp
1101 .collect::<Vec<_>>();
1100 .collect::<Vec<_>>();
1103− // HAXX: arrange layers by byte range, with deeper layers positioned first
1102+ layers.sort_unstable_by_key(|layer| layer.sort_key());
1104− layers.sort_by_key(|layer| {
1106− layer.ranges.first().cloned(),
1107− std::cmp::Reverse(layer.depth),
1111 let mut result = HighlightIter {
1104 let mut result = HighlightIter {
@@ -1424,12 +1417,11 @@ impl<'a> TextProvider<'a> for RopeProvider<'a> {
1424struct HighlightIterLayer<'a> {
1417struct HighlightIterLayer<'a> {
1425 _tree: Option<Tree>,
1418 _tree: Option<Tree>,
1426 cursor: QueryCursor,
1419 cursor: QueryCursor,
1427− captures: iter::Peekable<QueryCaptures<'a, 'a, RopeProvider<'a>>>,
1420+ captures: RefCell<iter::Peekable<QueryCaptures<'a, 'a, RopeProvider<'a>>>>,
1428 config: &'a HighlightConfiguration,
1421 config: &'a HighlightConfiguration,
1429 highlight_end_stack: Vec<usize>,
1422 highlight_end_stack: Vec<usize>,
1430 scope_stack: Vec<LocalScope<'a>>,
1423 scope_stack: Vec<LocalScope<'a>>,
1431 depth: u32,
1424 depth: u32,
1432− ranges: &'a [Range],
1435impl<'a> fmt::Debug for HighlightIterLayer<'a> {
1427impl<'a> fmt::Debug for HighlightIterLayer<'a> {
@@ -1610,10 +1602,11 @@ impl<'a> HighlightIterLayer<'a> {
1610 // First, sort scope boundaries by their byte offset in the document. At a
1602 // First, sort scope boundaries by their byte offset in the document. At a
1611 // given position, emit scope endings before scope beginnings. Finally, emit
1603 // given position, emit scope endings before scope beginnings. Finally, emit
1612 // scope boundaries from deeper layers first.
1604 // scope boundaries from deeper layers first.
1613− fn sort_key(&mut self) -> Option<(usize, bool, isize)> {
1605+ fn sort_key(&self) -> Option<(usize, bool, isize)> {
1614 let depth = -(self.depth as isize);
1606 let depth = -(self.depth as isize);
1615 let next_start = self
1607 let next_start = self
1616 .captures
1608 .captures
1618 .map(|(m, i)| m.captures[*i].node.start_byte());
1611 .map(|(m, i)| m.captures[*i].node.start_byte());
1619 let next_end = self.highlight_end_stack.last().cloned();
1612 let next_end = self.highlight_end_stack.last().cloned();
@@ -1838,7 +1831,8 @@ impl<'a> Iterator for HighlightIter<'a> {
1838 // Get the next capture from whichever layer has the earliest highlight boundary.
1831 // Get the next capture from whichever layer has the earliest highlight boundary.
1839 let range;
1832 let range;
1840 let layer = &mut self.layers[0];
1833 let layer = &mut self.layers[0];
1834+ let captures = layer.captures.get_mut();
1841− if let Some((next_match, capture_index)) = layer.captures.peek() {
1835+ if let Some((next_match, capture_index)) = captures.peek() {
1842 let next_capture = next_match.captures[*capture_index];
1836 let next_capture = next_match.captures[*capture_index];
1843 range = next_capture.node.byte_range();
1837 range = next_capture.node.byte_range();
@@ -1861,7 +1855,7 @@ impl<'a> Iterator for HighlightIter<'a> {
1861 return self.emit_event(self.source.len_bytes(), None);
1855 return self.emit_event(self.source.len_bytes(), None);
1864− let (mut match_, capture_index) = layer.captures.next().unwrap();
1858+ let (mut match_, capture_index) = captures.next().unwrap();
1865 let mut capture = match_.captures[capture_index];
1859 let mut capture = match_.captures[capture_index];
1867 // Remove from the local scope stack any local scopes that have already ended.
1861 // Remove from the local scope stack any local scopes that have already ended.
@@ -1937,11 +1931,11 @@ impl<'a> Iterator for HighlightIter<'a> {
1939 // Continue processing any additional matches for the same node.
1933 // Continue processing any additional matches for the same node.
1940− if let Some((next_match, next_capture_index)) = layer.captures.peek() {
1934+ if let Some((next_match, next_capture_index)) = captures.peek() {
1941 let next_capture = next_match.captures[*next_capture_index];
1935 let next_capture = next_match.captures[*next_capture_index];
1942 if next_capture.node == capture.node {
1936 if next_capture.node == capture.node {
1943 capture = next_capture;
1937 capture = next_capture;
1944− match_ = layer.captures.next().unwrap().0;
1938+ match_ = captures.next().unwrap().0;
1945 continue;
1939 continue;
@@ -1964,11 +1958,11 @@ impl<'a> Iterator for HighlightIter<'a> {
1964 // highlighting patterns that are disabled for local variables.
1958 // highlighting patterns that are disabled for local variables.
1965 if definition_highlight.is_some() || reference_highlight.is_some() {
1959 if definition_highlight.is_some() || reference_highlight.is_some() {
1966 while layer.config.non_local_variable_patterns[match_.pattern_index] {
1960 while layer.config.non_local_variable_patterns[match_.pattern_index] {
1967− if let Some((next_match, next_capture_index)) = layer.captures.peek() {
1961+ if let Some((next_match, next_capture_index)) = captures.peek() {
1968 let next_capture = next_match.captures[*next_capture_index];
1962 let next_capture = next_match.captures[*next_capture_index];
1969 if next_capture.node == capture.node {
1963 if next_capture.node == capture.node {
1970 capture = next_capture;
1964 capture = next_capture;
1971− match_ = layer.captures.next().unwrap().0;
1965+ match_ = captures.next().unwrap().0;
1972 continue;
1966 continue;
@@ -1983,10 +1977,10 @@ impl<'a> Iterator for HighlightIter<'a> {
1983 // for a given node are ordered by pattern index, so these subsequent
1977 // for a given node are ordered by pattern index, so these subsequent
1984 // captures are guaranteed to be for highlighting, not injections or
1978 // captures are guaranteed to be for highlighting, not injections or
1985 // local variables.
1979 // local variables.
1986− while let Some((next_match, next_capture_index)) = layer.captures.peek() {
1980+ while let Some((next_match, next_capture_index)) = captures.peek() {
1987 let next_capture = next_match.captures[*next_capture_index];
1981 let next_capture = next_match.captures[*next_capture_index];
1988 if next_capture.node == capture.node {
1982 if next_capture.node == capture.node {
1989− layer.captures.next();
1983+ captures.next();
1990 } else {
1984 } else {