fork of https://github.com/tree-sitter/tree-sitter-graph

Factor out lazy query match visit

Changed files
+22 -7
src
execution
+22 -7
src/execution/lazy.rs
··· 62 62 }; 63 63 64 64 let mut locals = VariableMap::new(); 65 - let mut cursor = QueryCursor::new(); 66 65 let mut store = LazyStore::new(); 67 66 let mut scoped_store = LazyScopedVariables::new(); 68 67 let mut lazy_graph = Vec::new(); 69 68 let mut function_parameters = Vec::new(); 70 69 let mut prev_element_debug_info = HashMap::new(); 71 70 72 - let query = &self.query.as_ref().unwrap(); 73 - let matches = cursor.matches(query, tree.root_node(), source.as_bytes()); 74 - for mat in matches { 71 + self.try_visit_matches_lazy(tree, source, |stanza, mat| { 75 72 cancellation_flag.check("processing matches")?; 76 - let stanza = &self.stanzas[mat.pattern_index]; 77 73 stanza.execute_lazy( 78 74 source, 79 75 &mat, ··· 87 83 &mut prev_element_debug_info, 88 84 &self.shorthands, 89 85 cancellation_flag, 90 - )?; 91 - } 86 + ) 87 + })?; 92 88 93 89 let mut exec = EvaluationContext { 94 90 source, ··· 108 104 store.evaluate_all(&mut exec)?; 109 105 scoped_store.evaluate_all(&mut exec)?; 110 106 107 + Ok(()) 108 + } 109 + 110 + pub(super) fn try_visit_matches_lazy<'a, 'tree, E, F>( 111 + &self, 112 + tree: &'tree Tree, 113 + source: &'tree str, 114 + mut visit: F, 115 + ) -> Result<(), E> 116 + where 117 + F: FnMut(&ast::Stanza, QueryMatch<'_, 'tree>) -> Result<(), E>, 118 + { 119 + let mut cursor = QueryCursor::new(); 120 + let query = self.query.as_ref().unwrap(); 121 + let matches = cursor.matches(query, tree.root_node(), source.as_bytes()); 122 + for mat in matches { 123 + let stanza = &self.stanzas[mat.pattern_index]; 124 + visit(stanza, mat)?; 125 + } 111 126 Ok(()) 112 127 } 113 128 }