diff options
| author | delta <darkussdelta@gmail.com> | 2025-08-26 21:24:12 +0200 |
|---|---|---|
| committer | delta <darkussdelta@gmail.com> | 2025-08-26 21:24:12 +0200 |
| commit | 9085e773e3b6bbaa698e301761001597bd39ae7d (patch) | |
| tree | 321305f63b0a24ca7d058b1cf115e855f4dc9f7e /build.rs | |
| parent | 7d0c2f4617bc0119e748f59e0a6bd8da79494087 (diff) | |
Diffstat (limited to 'build.rs')
| -rw-r--r-- | build.rs | 63 |
1 files changed, 62 insertions, 1 deletions
@@ -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(); } |
