Repository: helix-editor/helix
File: helix-core/src/syntax.rs (Rust)

Code before:
        source: RopeSlice,
    ) -> Option<Arc<LanguageConfiguration>> {
        let line = Cow::from(source.line(0));
        static SHEBANG_REGEX: Lazy<Regex> =
            Lazy::new(|| Regex::new(&["^", SHEBANG].concat()).unwrap());
        let configuration_id = SHEBANG_REGEX
            .captures(&line)
            .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));

        configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
    }

    pub fn language_config_for_scope(&self, scope: &str) -> Option<Arc<LanguageConfiguration>> {
        self.language_configs
            .iter()
            .find(|config| config.scope == scope)
            .cloned()
    }

    pub fn language_config_for_language_id(
        &self,
        id: impl PartialEq<String>,
    ) -> Option<Arc<LanguageConfiguration>> {
        self.language_configs
            .iter()
            .find(|config| id.eq(&config.language_id))
            .cloned()
    }

    /// Unlike `language_config_for_language_id`, which only returns Some for an exact id, this
    /// function will perform a regex match on the given string to find the closest language match.
    pub fn language_config_for_name(&self, slice: RopeSlice) -> Option<Arc<LanguageConfiguration>> {
        // PERF: If the name matches up with the id, then this saves the need to do expensive regex.
        let shortcircut = self.language_config_for_language_id(slice);
        if shortcircut.is_some() {
            return shortcircut;
        }

        // If the name did not match up with a known id, then match on injection regex.

        let mut best_match_length = 0;
        let mut best_match_position = None;
        for (i, configuration) in self.language_configs.iter().enumerate() {
            if let Some(injection_regex) = &configuration.injection_regex {
                if let Some(mat) = injection_regex.find(slice.regex_input()) {
                    let length = mat.end() - mat.start();
                    if length > best_match_length {
                        best_match_position = Some(i);
                        best_match_length = length;
                    }
                }

Review comment (suggestion):
Not really a review comment, just a note for the future. We may want to add another hashmap to `Loader`, `language_config_ids_by_language_id: HashMap<String, usize>` that ensures that this lookup takes constant time. On a flamegraph I don't see this function show up - it's nearly instant to loop through the roughly ~250 languages we have configured today - but it will future-proof the lookup.

Looking up in a `HashMap<String, T>` by `RopeSlice` is deceptively non-trivial though (I have a lengthy, half-written blog post about it) and we'll need a few other changes to get there first.

Code after:
        source: RopeSlice,
    ) -> Option<Arc<LanguageConfiguration>> {
        let line = Cow::from(source.line(0));
        static SHEBANG_REGEX: Lazy<Regex> =
            Lazy::new(|| Regex::new(&["^", SHEBANG].concat()).unwrap());
        let configuration_id = SHEBANG_REGEX
            .captures(&line)
            .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));

        configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
    }

    pub fn language_config_for_scope(&self, scope: &str) -> Option<Arc<LanguageConfiguration>> {
        self.language_configs
            .iter()
            .find(|config| config.scope == scope)
            .cloned()
    }

    pub fn language_config_for_language_id(
        &self,
        id: impl PartialEq<String>,
    ) -> Option<Arc<LanguageConfiguration>> {
        self.language_configs
            .iter()
            .find(|config| id.eq(&config.language_id))
            .cloned()
    }

    /// Unlike `language_config_for_language_id`, which only returns Some for an exact id, this
    /// function will perform a regex match on the given string to find the closest language match.
    pub fn language_config_for_name(&self, slice: RopeSlice) -> Option<Arc<LanguageConfiguration>> {
        // PERF: If the name matches up with the id, then this saves the need to do expensive regex.
        let shortcircuit = self.language_config_for_language_id(slice);
        if shortcircuit.is_some() {
            return shortcircuit;
        }

        // If the name did not match up with a known id, then match on injection regex.

        let mut best_match_length = 0;
        let mut best_match_position = None;
        for (i, configuration) in self.language_configs.iter().enumerate() {
            if let Some(injection_regex) = &configuration.injection_regex {
                if let Some(mat) = injection_regex.find(slice.regex_input()) {
                    let length = mat.end() - mat.start();
                    if length > best_match_length {
                        best_match_position = Some(i);
                        best_match_length = length;
                    }
                }