use tree_sitter_highlight::{Highlight, HighlightConfiguration, HighlightEvent, Highlighter}; static HIGHLIGHT_NAMES: [&str; 29] = [ "attribute", "boolean", "comment", "conditional", "constant", "constant.builtin", "constructor", "field", "function", "function.builtin", "function.call", "keyword", "keyword.function", "keyword.operator", "keyword.return", "label", "method", "method.call", "number", "operator", "parameter", "preproc", "punctuation.bracket", "punctuation.delimiter", "repeat", "string", "string.escape", "variable", "variable.builtin", ]; pub fn new_highlight_configuration() -> HighlightConfiguration { let mut highlight_config = HighlightConfiguration::new( tree_sitter_lua::language(), "lua", tree_sitter_lua::HIGHLIGHTS_QUERY, tree_sitter_lua::INJECTIONS_QUERY, tree_sitter_lua::LOCALS_QUERY, ) .unwrap(); highlight_config.configure(&HIGHLIGHT_NAMES); highlight_config } pub fn highlight_name(h: Highlight) -> &'static str { HIGHLIGHT_NAMES[h.0] } pub fn highlighter_split<'a>( s: &'a [u8], config: &'a HighlightConfiguration, ) -> Vec<(Option, &'a str)> { let mut splits = Vec::new(); let mut current = None; let mut highlighter = Highlighter::new(); let highlights = highlighter.highlight(config, s, None, |_| None).unwrap(); for event in highlights { match event.unwrap() { HighlightEvent::Source { start, end } => { let group = std::str::from_utf8(&s[start..end]).unwrap(); for line in group.lines() { splits.push((current, line)); splits.push((current, "\n")); } if !group.ends_with('\n') { splits.pop(); } } HighlightEvent::HighlightStart(s) => current = Some(s), HighlightEvent::HighlightEnd => current = None, } } splits }