@ -1341,7 +1341,7 @@ impl Syntax {
where
where
F : Fn ( & ' a HighlightConfiguration ) -> Option < & ' a Query > ,
F : Fn ( & ' a HighlightConfiguration ) -> Option < & ' a Query > ,
{
{
let layers = self
let mut layers : Vec < _ > = self
. layers
. layers
. iter ( )
. iter ( )
. filter_map ( | ( _ , layer ) | {
. filter_map ( | ( _ , layer ) | {
@ -1373,12 +1373,14 @@ impl Syntax {
Some ( QueryIterLayer {
Some ( QueryIterLayer {
cursor ,
cursor ,
captures ,
captures : RefCell ::new ( captures ) ,
layer ,
layer ,
} )
} )
} )
} )
. collect ( ) ;
. collect ( ) ;
layers . sort_unstable_by_key ( | layer | layer . sort_key ( ) ) ;
QueryIter { layers }
QueryIter { layers }
}
}
@ -2075,11 +2077,21 @@ impl HighlightConfiguration {
}
}
}
}
impl < ' a > HighlightIterLayer < ' a > {
trait IterLayer {
type SortKey : PartialOrd ;
fn sort_key ( & self ) -> Option < Self ::SortKey > ;
fn cursor ( self ) -> QueryCursor ;
}
impl < ' a > IterLayer for HighlightIterLayer < ' a > {
type SortKey = ( usize , bool , isize ) ;
fn sort_key ( & self ) -> Option < Self ::SortKey > {
// First, sort scope boundaries by their byte offset in the document. At a
// First, sort scope boundaries by their byte offset in the document. At a
// given position, emit scope endings before scope beginnings. Finally, emit
// given position, emit scope endings before scope beginnings. Finally, emit
// scope boundaries from deeper layers first.
// scope boundaries from deeper layers first.
fn sort_key ( & self ) -> Option < ( usize , bool , isize ) > {
let depth = - ( self . depth as isize ) ;
let depth = - ( self . depth as isize ) ;
let next_start = self
let next_start = self
. captures
. captures
@ -2100,6 +2112,30 @@ impl<'a> HighlightIterLayer<'a> {
_ = > None ,
_ = > None ,
}
}
}
}
fn cursor ( self ) -> QueryCursor {
self . cursor
}
}
impl < ' a > IterLayer for QueryIterLayer < ' a > {
type SortKey = ( usize , isize ) ;
fn sort_key ( & self ) -> Option < Self ::SortKey > {
// Sort the layers so that the first layer in the Vec has the next
// capture ordered by start byte and depth (descending).
let depth = - ( self . layer . depth as isize ) ;
let mut captures = self . captures . borrow_mut ( ) ;
let ( match_ , capture_index ) = captures . peek ( ) ? ;
let start = match_ . captures [ * capture_index ] . node . start_byte ( ) ;
Some ( ( start , depth ) )
}
fn cursor ( self ) -> QueryCursor {
self . cursor
}
}
}
}
#[ derive(Clone) ]
#[ derive(Clone) ]
@ -2760,6 +2796,7 @@ impl<'a> Iterator for QueryIter<'a> {
let inner = layer . layer ;
let inner = layer . layer ;
layer
layer
. captures
. captures
. borrow_mut ( )
. next ( )
. next ( )
. map ( | ( match_ , index ) | ( inner , match_ , index ) )
. map ( | ( match_ , index ) | ( inner , match_ , index ) )
}
}