@@ -27,6 +27,11 @@ impl ScopeMatches {
2727 }
2828}
2929
30+ struct CompiledAstGrepRules {
31+ rules : RuleCollection < SupportLang > ,
32+ ids_to_scope : HashMap < String , String > ,
33+ }
34+
3035#[ derive( Debug , Clone ) ]
3136enum PathPatternMatcher {
3237 Regex ( Regex ) ,
@@ -166,16 +171,12 @@ fn normalize_relative_path(path: &Path) -> String {
166171 }
167172}
168173
169- fn compile_ast_grep_rules (
170- config : & Config ,
171- ) -> Result < Option < ( RuleCollection < SupportLang > , HashMap < String , String > ) > > {
172- if config. scope_ast_grep . is_empty ( ) {
173- return Ok ( None ) ;
174- }
174+ fn compile_ast_grep_rules ( config : & Config ) -> Result < CompiledAstGrepRules > {
175+ let capacity = config. scope_ast_grep . len ( ) ;
175176
176177 let globals = GlobalRules :: default ( ) ;
177178 let mut ids_to_scope = HashMap :: new ( ) ;
178- let mut compiled_rules = Vec :: with_capacity ( config . scope_ast_grep . len ( ) ) ;
179+ let mut compiled_rules = Vec :: with_capacity ( capacity ) ;
179180
180181 for ( index, rule) in config. scope_ast_grep . iter ( ) . enumerate ( ) {
181182 let mut serializable = rule. rule . clone ( ) ;
@@ -190,23 +191,27 @@ fn compile_ast_grep_rules(
190191 compiled_rules. push ( RuleConfig :: try_from ( serializable, & globals) ?) ;
191192 }
192193
193- let collection = RuleCollection :: try_new ( compiled_rules) ?;
194- Ok ( Some ( ( collection, ids_to_scope) ) )
194+ Ok ( CompiledAstGrepRules {
195+ rules : RuleCollection :: try_new ( compiled_rules) ?,
196+ ids_to_scope,
197+ } )
195198}
196199
197200fn detect_ast_grep_scopes (
198201 config : & Config ,
199202 workdir : & Path ,
200203 changed_paths : & [ PathBuf ] ,
201204) -> Result < Vec < String > > {
202- let Some ( ( rules , ids_to_scope ) ) = compile_ast_grep_rules ( config ) ? else {
205+ if config . scope_ast_grep . is_empty ( ) {
203206 return Ok ( Vec :: new ( ) ) ;
204- } ;
207+ }
208+
209+ let compiled_rules = compile_ast_grep_rules ( config) ?;
205210
206211 let mut matched_scopes = IndexSet :: new ( ) ;
207212
208213 for relative_path in changed_paths {
209- let applicable_rules = rules. for_path ( relative_path) ;
214+ let applicable_rules = compiled_rules . rules . for_path ( relative_path) ;
210215 if applicable_rules. is_empty ( ) {
211216 continue ;
212217 }
@@ -219,7 +224,7 @@ fn detect_ast_grep_scopes(
219224 for rule in applicable_rules {
220225 let root = rule. language . ast_grep ( & source) ;
221226 if root. root ( ) . find ( & rule. matcher ) . is_some ( ) {
222- if let Some ( scope) = ids_to_scope. get ( & rule. id ) {
227+ if let Some ( scope) = compiled_rules . ids_to_scope . get ( & rule. id ) {
223228 matched_scopes. insert ( scope. clone ( ) ) ;
224229 }
225230 }
0 commit comments