@@ -550,26 +550,17 @@ impl<Id: TreeId> Iterator for ChildrenIter<'_, Id> {
550550 type Item = Id ;
551551
552552 fn next ( & mut self ) -> Option < Self :: Item > {
553- match self . field_children . as_mut ( ) {
554- None => match self . field_order . next ( ) {
555- Some ( field) => {
556- self . current_field = Some ( field) ;
557- self . field_children = Some ( self . fields [ & field] . iter ( ) ) ;
558- self . next ( )
559- }
560- None => None ,
561- } ,
562- Some ( children) => match children. next ( ) {
563- None => match self . field_order . next ( ) {
564- None => None ,
565- Some ( field) => {
566- self . current_field = Some ( field) ;
567- self . field_children = Some ( self . fields [ & field] . iter ( ) ) ;
568- self . next ( )
569- }
570- } ,
571- Some ( child_id) => Some ( * child_id) ,
572- } ,
553+ loop {
554+ if let Some ( child_id) = self
555+ . field_children
556+ . as_mut ( )
557+ . and_then ( |children| children. next ( ) )
558+ {
559+ return Some ( * child_id) ;
560+ }
561+ let field = self . field_order . next ( ) ?;
562+ self . current_field = Some ( field) ;
563+ self . field_children = Some ( self . fields [ & field] . iter ( ) ) ;
573564 }
574565 }
575566}
@@ -884,55 +875,11 @@ impl<Id: TreeId> Ast<Id> {
884875 }
885876
886877 fn union_source_range_of_children ( & self , fields : & BTreeMap < FieldId , Vec < Id > > ) -> Option < Range > {
887- let mut start_byte: Option < usize > = None ;
888- let mut end_byte: Option < usize > = None ;
889- let mut start_point = Point { row : 0 , column : 0 } ;
890- let mut end_point = Point { row : 0 , column : 0 } ;
891-
892- for child_ids in fields. values ( ) {
893- for & child_id in child_ids {
894- let Some ( child) = self . get_node ( child_id) else {
895- continue ;
896- } ;
897- let Some ( child_range) = child. source_range ( ) else {
898- continue ;
899- } ;
900-
901- match start_byte {
902- None => {
903- start_byte = Some ( child_range. start_byte ) ;
904- start_point = child_range. start_point ;
905- }
906- Some ( current_start) if child_range. start_byte < current_start => {
907- start_byte = Some ( child_range. start_byte ) ;
908- start_point = child_range. start_point ;
909- }
910- _ => { }
911- }
912-
913- match end_byte {
914- None => {
915- end_byte = Some ( child_range. end_byte ) ;
916- end_point = child_range. end_point ;
917- }
918- Some ( current_end) if child_range. end_byte > current_end => {
919- end_byte = Some ( child_range. end_byte ) ;
920- end_point = child_range. end_point ;
921- }
922- _ => { }
923- }
924- }
925- }
926-
927- match ( start_byte, end_byte) {
928- ( Some ( start_byte) , Some ( end_byte) ) => Some ( Range {
929- start_byte,
930- end_byte,
931- start_point,
932- end_point,
933- } ) ,
934- _ => None ,
935- }
878+ fields
879+ . values ( )
880+ . flatten ( )
881+ . filter_map ( |& id| self . get_node ( id) ?. source_range ( ) )
882+ . reduce ( Range :: union)
936883 }
937884
938885 pub fn create_named_token ( & mut self , kind : & ' static str , content : String ) -> Id {
@@ -1008,13 +955,13 @@ impl<Id: TreeId> Ast<Id> {
1008955 }
1009956
1010957 pub fn id_for_node_kind ( & self , kind : & str ) -> Option < KindId > {
1011- let id = self . schema . id_for_node_kind ( kind) . unwrap_or ( 0 ) ;
1012- if id == 0 { None } else { Some ( id) }
958+ self . schema . id_for_node_kind ( kind) . filter ( |& id| id != 0 )
1013959 }
1014960
1015961 pub fn id_for_unnamed_node_kind ( & self , kind : & str ) -> Option < KindId > {
1016- let id = self . schema . id_for_unnamed_node_kind ( kind) . unwrap_or ( 0 ) ;
1017- if id == 0 { None } else { Some ( id) }
962+ self . schema
963+ . id_for_unnamed_node_kind ( kind)
964+ . filter ( |& id| id != 0 )
1018965 }
1019966}
1020967
@@ -1259,20 +1206,17 @@ impl<C> Rule<C> {
12591206 /// transform.
12601207 fn match_query ( & self , ast : & Ast , node : Id ) -> Option < Captures > {
12611208 let mut captures = Captures :: new ( ) ;
1262- if !self . query . do_match ( ast, node, & mut captures) {
1263- return None ;
1264- }
1265- Some ( captures)
1209+ self . query
1210+ . do_match ( ast, node, & mut captures)
1211+ . then_some ( captures)
12661212 }
12671213
12681214 /// Evaluate this rule's guard against a successful query match. An
12691215 /// unguarded rule always succeeds.
12701216 fn guard_matches ( & self , ast : & Ast , captures : & Captures , user_ctx : & C ) -> bool {
1271- if let Some ( guard) = & self . guard {
1272- guard ( ast, captures, user_ctx)
1273- } else {
1274- true
1275- }
1217+ self . guard
1218+ . as_ref ( )
1219+ . is_none_or ( |guard| guard ( ast, captures, user_ctx) )
12761220 }
12771221
12781222 /// Run this rule's transform with the given captures, making `node`'s
0 commit comments