aboutsummaryrefslogtreecommitdiff
path: root/build.rs
diff options
context:
space:
mode:
Diffstat (limited to 'build.rs')
-rw-r--r--build.rs63
1 files changed, 62 insertions, 1 deletions
diff --git a/build.rs b/build.rs
index a402549..f108056 100644
--- a/build.rs
+++ b/build.rs
@@ -1,7 +1,10 @@
use std::{
env::var,
+ ffi::OsStr,
fs::{
read,
+ read_dir,
+ read_to_string,
write,
},
io::Write,
@@ -15,8 +18,9 @@ use std::{
},
};
+use quote::quote;
+
fn main() {
- println!("test");
println!("cargo::rerun-if-changed=nvim-treesitter/lua/nvim-treesitter/parsers.lua");
println!("cargo::rerun-if-changed=generate_parsers.lua");
let mut child = Command::new("lua")
@@ -45,4 +49,61 @@ fn main() {
output.stdout,
)
.unwrap();
+
+ let query_dirs = read_dir(Path::new("nvim-treesitter/queries"))
+ .expect("Failed to read nvim-treesitter/queries")
+ .filter_map(|entry| {
+ let path = entry.unwrap().path();
+ path.is_dir().then_some(path)
+ })
+ .collect::<Vec<PathBuf>>();
+
+ let queries = query_dirs
+ .iter()
+ .filter_map(|path| {
+ read_dir(path)
+ .map(|entries| {
+ (
+ path.file_name().unwrap().to_str().unwrap(),
+ entries.filter_map(|entry| entry.ok()),
+ )
+ })
+ .ok()
+ })
+ .map(|(lang, entries)| {
+ let entries = entries.filter_map(|entry| {
+ let path = entry.path();
+ match path.file_stem().map(OsStr::to_str).flatten() {
+ Some(stem @ ("highlights" | "injections" | "locals")) => {
+ let content =
+ read_to_string(&path).expect("Query file should be valid UTF-8");
+ Some(quote! {
+ (#stem, #content)
+ })
+ }
+ _ => None,
+ }
+ });
+ quote! {
+ (#lang, HashMap::from([
+ #(#entries),*
+ ]))
+ }
+ });
+
+ let code = quote! {
+ pub static QUERIES: LazyLock<HashMap<&'static str, HashMap<&'static str, &'static str>>> = LazyLock::new(|| {
+ HashMap::from([
+ #(#queries),*
+ ])
+ });
+ };
+ let tree = &syn::parse2(code).unwrap();
+ let formatted = prettyplease::unparse(tree);
+
+ write(
+ PathBuf::from(format!("{}/_queries.rs", var("OUT_DIR").unwrap())),
+ formatted,
+ )
+ .unwrap();
}