fn highlight()

in highlight/src/c_lib.rs [153:218]


    fn highlight(
        &self,
        source_code: &[u8],
        scope_name: &str,
        output: &mut TSHighlightBuffer,
        cancellation_flag: Option<&AtomicUsize>,
    ) -> ErrorCode {
        let configuration = self.languages.get(scope_name);
        if configuration.is_none() {
            return ErrorCode::UnknownScope;
        }
        let configuration = configuration.unwrap();
        let languages = &self.languages;

        let highlighter = Highlighter::new(
            source_code,
            configuration.language,
            &configuration.property_sheet,
            |injection_string| {
                languages.values().find_map(|conf| {
                    conf.injection_regex.as_ref().and_then(|regex| {
                        if regex.is_match(injection_string) {
                            Some((conf.language, &conf.property_sheet))
                        } else {
                            None
                        }
                    })
                })
            },
            cancellation_flag,
        );

        if let Ok(highlighter) = highlighter {
            output.html.clear();
            output.line_offsets.clear();
            output.line_offsets.push(0);
            let mut highlights = Vec::new();
            for event in highlighter {
                match event {
                    Ok(HighlightEvent::HighlightStart(s)) => {
                        highlights.push(s);
                        output.start_highlight(s, &self.attribute_strings);
                    }
                    Ok(HighlightEvent::HighlightEnd) => {
                        highlights.pop();
                        output.end_highlight();
                    }
                    Ok(HighlightEvent::Source(src)) => {
                        output.add_text(src, &highlights, &self.attribute_strings);
                    },
                    Err(Error::Cancelled) => {
                        return ErrorCode::Timeout;
                    },
                    Err(Error::InvalidLanguage) => {
                        return ErrorCode::InvalidLanguage;
                    },
                    Err(Error::Unknown) => {
                        return ErrorCode::Timeout;
                    }
                }
            }
            ErrorCode::Ok
        } else {
            ErrorCode::Timeout
        }
    }