-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.rs
80 lines (71 loc) · 2.22 KB
/
build.rs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use tree_sitter::Language;
extern crate napi_build;
fn main() {
let mut queries = String::new();
queries.push_str(tree_sitter_javascript::HIGHLIGHT_QUERY);
queries.push_str(tree_sitter_javascript::JSX_HIGHLIGHT_QUERY);
let mut highlight_names = Vec::new();
add_highlight_names(
tree_sitter_javascript::LANGUAGE.into(),
&queries,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_typescript::LANGUAGE_TSX.into(),
tree_sitter_typescript::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_css::LANGUAGE.into(),
tree_sitter_css::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_regex::LANGUAGE.into(),
tree_sitter_regex::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_jsdoc::LANGUAGE.into(),
tree_sitter_jsdoc::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_json::LANGUAGE.into(),
tree_sitter_json::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_yaml::LANGUAGE.into(),
tree_sitter_yaml::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
add_highlight_names(
tree_sitter_html::LANGUAGE.into(),
tree_sitter_html::HIGHLIGHTS_QUERY,
&mut highlight_names,
);
highlight_names.sort();
let class_names: Vec<String> = highlight_names
.iter()
.map(|s| s.replace('.', " "))
.collect();
std::fs::write(
"src/highlight_names.rs",
format!(
"use napi_derive::napi;\n\n#[napi]\npub const HIGHLIGHT_NAMES: &[&str] = &{:#?};\n\npub const CLASS_NAMES: &[&str] = &{:#?};\n",
highlight_names,
class_names
),
)
.expect("write error");
napi_build::setup();
}
fn add_highlight_names(lang: Language, source: &str, highlights: &mut Vec<String>) {
let query = tree_sitter::Query::new(&lang, source).unwrap();
for capture in query.capture_names() {
if !highlights.iter().any(|h| h == capture) {
highlights.push(capture.to_string());
}
}
}