@@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
22use std:: path:: Path ;
33use std:: sync:: Arc ;
44
5+ use crate :: MatchStrategy ;
56use crate :: error:: { Error , GialloResult } ;
67use crate :: grammars:: caches:: RegexCache ;
78use crate :: grammars:: {
@@ -10,14 +11,13 @@ use crate::grammars::{
1011 resolve_external_references,
1112} ;
1213use crate :: highlight:: { HighlightedText , Highlighter , MergingOptions } ;
13- use serde:: { Deserialize , Serialize } ;
14-
1514#[ cfg( feature = "dump" ) ]
1615use crate :: scope:: ScopeRepository ;
1716use crate :: scope:: { Scope , ScopeInterner } ;
1817use crate :: themes:: css:: { DARK_SUFFIX , LIGHT_SUFFIX } ;
1918use crate :: themes:: { CompiledTheme , RawTheme , ThemeVariant } ;
2019use crate :: tokenizer:: { Token , Tokenizer } ;
20+ use serde:: { Deserialize , Serialize } ;
2121
2222#[ cfg( feature = "dump" ) ]
2323#[ derive( Debug , Clone , Serialize , Deserialize , Default ) ]
@@ -149,6 +149,8 @@ pub struct Registry {
149149 // highlight. To do that we had to check the end regex in the tokenizer separately from the
150150 // regset.
151151 matcher_cache : papaya:: HashMap < ( GrammarId , GlobalRuleRef ) , Arc < RuleMatcher > > ,
152+ // whether to walk or only use regset
153+ match_strategy : MatchStrategy ,
152154}
153155
154156impl Clone for Registry {
@@ -162,6 +164,7 @@ impl Clone for Registry {
162164 linked : self . linked ,
163165 regex_cache : self . regex_cache . clone ( ) ,
164166 matcher_cache : papaya:: HashMap :: new ( ) ,
167+ match_strategy : self . match_strategy ,
165168 }
166169 }
167170}
@@ -198,6 +201,7 @@ impl Registry {
198201 linked : false ,
199202 regex_cache : Arc :: new ( RegexCache :: default ( ) ) ,
200203 matcher_cache : papaya:: HashMap :: new ( ) ,
204+ match_strategy : MatchStrategy :: default ( ) ,
201205 } ;
202206 this. link_grammars ( ) ;
203207
@@ -321,6 +325,12 @@ impl Registry {
321325 Ok ( ( tokens, tokenizer. into_scope_interner ( ) ) )
322326 }
323327
328+ /// See [MatchStrategy] documentation to see which one to use for your usecase
329+ pub fn set_match_strategy ( & mut self , match_strategy : MatchStrategy ) {
330+ self . match_strategy = match_strategy;
331+ self . clear_matcher_cache ( ) ;
332+ }
333+
324334 /// Checks whether the given lang is available in the registry with its grammar name
325335 /// or aliases
326336 pub fn contains_grammar ( & self , name : & str ) -> bool {
@@ -554,10 +564,11 @@ impl Registry {
554564 pub fn clear_caches ( & self ) {
555565 self . matcher_cache . pin ( ) . clear ( ) ;
556566 self . regex_cache . clear ( ) ;
567+ self . matcher_cache . pin ( ) . clear ( ) ;
557568 }
558569
559570 #[ doc( hidden) ]
560- pub fn clear_pattern_cache ( & self ) {
571+ pub fn clear_matcher_cache ( & self ) {
561572 self . matcher_cache . pin ( ) . clear ( ) ;
562573 }
563574
@@ -578,7 +589,11 @@ impl Registry {
578589 . into_iter ( )
579590 . map ( |( rule, pat) | ( rule, pat. to_owned ( ) ) )
580591 . collect ( ) ;
581- let rule_matcher = Arc :: new ( RuleMatcher :: new ( patterns, self . regex_cache . clone ( ) ) ) ;
592+ let rule_matcher = Arc :: new ( RuleMatcher :: new (
593+ patterns,
594+ self . regex_cache . clone ( ) ,
595+ self . match_strategy ,
596+ ) ) ;
582597
583598 // Use get_or_insert for concurrent-safe lazy init
584599 let inserted = self
0 commit comments