Lazily compile GlobMatchers

This commit is contained in:
cyqsimon 2023-11-05 02:46:32 +08:00
parent 4ad3002543
commit f483d2df42
No known key found for this signature in database
GPG Key ID: 1D8CE2F297390D65
2 changed files with 20 additions and 13 deletions

View File

@ -78,9 +78,11 @@ fn run_cache_subcommand(
Ok(()) Ok(())
} }
fn get_syntax_mapping_to_paths<'a>( fn get_syntax_mapping_to_paths<'r, 't, I>(mappings: I) -> HashMap<&'t str, Vec<String>>
mappings: &[(&GlobMatcher, &MappingTarget<'a>)], where
) -> HashMap<&'a str, Vec<String>> { I: IntoIterator<Item = (&'r GlobMatcher, &'r MappingTarget<'t>)>,
't: 'r, // target text outlives rule
{
let mut map = HashMap::new(); let mut map = HashMap::new();
for mapping in mappings { for mapping in mappings {
if let (matcher, MappingTarget::MapTo(s)) = mapping { if let (matcher, MappingTarget::MapTo(s)) = mapping {
@ -123,7 +125,7 @@ pub fn get_languages(config: &Config, cache_dir: &Path) -> Result<String> {
languages.sort_by_key(|lang| lang.name.to_uppercase()); languages.sort_by_key(|lang| lang.name.to_uppercase());
let configured_languages = get_syntax_mapping_to_paths(&config.syntax_mapping.all_mappings()); let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.all_mappings());
for lang in &mut languages { for lang in &mut languages {
if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) { if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) {

View File

@ -56,30 +56,35 @@ impl<'a> SyntaxMapping<'a> {
Ok(()) Ok(())
} }
/// Returns all mappings. User-defined mappings are listed before builtin /// Returns an iterator over all mappings. User-defined mappings are listed
/// mappings; mappings in front have higher precedence. /// before builtin mappings; mappings in front have higher precedence.
///
/// Builtin mappings' `GlobMatcher`s are lazily compiled.
/// ///
/// Note that this function ignores builtin mappings that are invalid under /// Note that this function ignores builtin mappings that are invalid under
/// the current environment (i.e. their rules require an environment /// the current environment (i.e. their rules require an environment
/// variable that is unset). /// variable that is unset).
pub fn all_mappings(&self) -> Vec<(&GlobMatcher, &MappingTarget<'a>)> { pub fn all_mappings(&self) -> impl Iterator<Item = (&GlobMatcher, &MappingTarget<'a>)> {
self.custom_mappings() self.custom_mappings()
.iter() .iter()
.map(|(matcher, target)| (matcher, target)) // as_ref .map(|(matcher, target)| (matcher, target)) // as_ref
.chain(self.builtin_mappings()) .chain(self.builtin_mappings())
.collect()
} }
/// Returns all valid builtin mappings. Mappings in front have higher // IMPRV: ideally `Item` should be `(&'static GlobMatcher, &'static MappingTarget<'static>)`
/// precedence. // but `Iterator::chain` (used in `SyntaxMapping::all_mappings`) asserts `Item = Self::Item`
// so we need a lifetime downcast, which I'm not sure how to perform
/// Returns an iterator over all valid builtin mappings. Mappings in front
/// have higher precedence.
///
/// The `GlabMatcher`s are lazily compiled.
/// ///
/// If a mapping rule requires an environment variable that is unset, it /// If a mapping rule requires an environment variable that is unset, it
/// will be ignored. /// will be ignored.
pub fn builtin_mappings(&self) -> Vec<(&'static GlobMatcher, &'static MappingTarget<'static>)> { pub fn builtin_mappings(&self) -> impl Iterator<Item = (&GlobMatcher, &MappingTarget<'a>)> {
BUILTIN_MAPPINGS BUILTIN_MAPPINGS
.iter() .iter()
.filter_map(|(matcher, target)| matcher.as_ref().map(|glob| (glob, target))) .filter_map(|(matcher, target)| matcher.as_ref().map(|glob| (glob, target)))
.collect()
} }
/// Returns all user-defined mappings. /// Returns all user-defined mappings.
@ -91,7 +96,7 @@ impl<'a> SyntaxMapping<'a> {
// Try matching on the file name as-is. // Try matching on the file name as-is.
let candidate = Candidate::new(&path); let candidate = Candidate::new(&path);
let candidate_filename = path.as_ref().file_name().map(Candidate::new); let candidate_filename = path.as_ref().file_name().map(Candidate::new);
for (glob, syntax) in self.all_mappings().into_iter() { for (glob, syntax) in self.all_mappings() {
if glob.is_match_candidate(&candidate) if glob.is_match_candidate(&candidate)
|| candidate_filename || candidate_filename
.as_ref() .as_ref()