From 9085e773e3b6bbaa698e301761001597bd39ae7d Mon Sep 17 00:00:00 2001 From: delta Date: Tue, 26 Aug 2025 21:24:12 +0200 Subject: include the queries in the binary --- build.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'build.rs') 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::>(); + + 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>> = 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(); } -- cgit v1.2.3