xtask/codegen/
queries.rs

1use std::collections::BTreeMap;
2
3use anyhow::Result;
4use once_cell::sync::Lazy;
5
6static QUERIES_DIR: Lazy<String> =
7    Lazy::new(|| format!("{}/queries", crate::WORKSPACE_DIR.display()));
8
9pub fn make_queries() -> Result<BTreeMap<String, [String; 6]>> {
10    let mut map = BTreeMap::new();
11    for lang in &crate::LANGUAGE_CONFIG.languages {
12        const CRATES_IO_SKIP_COMMENT: &str = "; crates.io skip";
13        const NON_CRATES_IO_SKIP_COMMENT: &str = "; non-crates.io skip";
14        let query_file =
15            |func: fn(&str, bool, &str, &str) -> String, enabled: bool, strip_comment: &str| {
16                if enabled {
17                    func(
18                        strip_comment,
19                        lang.queries.nvim_like,
20                        &lang.name,
21                        &QUERIES_DIR,
22                    )
23                } else {
24                    String::new()
25                }
26            };
27
28        let highlights = query_file(
29            syntastica_query_preprocessor::process_highlights_with_inherits,
30            true,
31            NON_CRATES_IO_SKIP_COMMENT,
32        );
33        let injections = query_file(
34            syntastica_query_preprocessor::process_injections_with_inherits,
35            lang.queries.injections,
36            NON_CRATES_IO_SKIP_COMMENT,
37        );
38        let locals = query_file(
39            syntastica_query_preprocessor::process_locals_with_inherits,
40            lang.queries.locals,
41            NON_CRATES_IO_SKIP_COMMENT,
42        );
43
44        let highlights_crates_io = query_file(
45            syntastica_query_preprocessor::process_highlights_with_inherits,
46            true,
47            CRATES_IO_SKIP_COMMENT,
48        );
49        let injections_crates_io = query_file(
50            syntastica_query_preprocessor::process_injections_with_inherits,
51            lang.queries.injections,
52            CRATES_IO_SKIP_COMMENT,
53        );
54        let locals_crates_io = query_file(
55            syntastica_query_preprocessor::process_locals_with_inherits,
56            lang.queries.locals,
57            CRATES_IO_SKIP_COMMENT,
58        );
59
60        map.insert(
61            lang.name.clone(),
62            [
63                highlights,
64                injections,
65                locals,
66                highlights_crates_io,
67                injections_crates_io,
68                locals_crates_io,
69            ],
70        );
71    }
72
73    Ok(map)
74}