1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
use tree_sitter_highlight::{Highlight, HighlightConfiguration, HighlightEvent, Highlighter};
use super::theme;
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(&theme::HIGHLIGHT_NAMES);
highlight_config
}
pub fn highlight_name(h: Highlight) -> &'static str {
theme::HIGHLIGHT_NAMES[h.0]
}
pub fn highlighter_split<'a>(
s: &'a [u8],
config: &'a HighlightConfiguration,
) -> Vec<(Option<Highlight>, &'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
}
|