Skip to main content

normalize_languages/
query.rs

1//! Tree-sitter query language support.
2
3use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8/// Tree-sitter query language support.
9pub struct Query;
10
11impl Language for Query {
12    fn name(&self) -> &'static str {
13        "Query"
14    }
15    fn extensions(&self) -> &'static [&'static str] {
16        &["scm"]
17    }
18    fn grammar_name(&self) -> &'static str {
19        "query"
20    }
21
22    fn has_symbols(&self) -> bool {
23        true
24    }
25
26    fn container_kinds(&self) -> &'static [&'static str] {
27        &[]
28    }
29
30    fn function_kinds(&self) -> &'static [&'static str] {
31        &[]
32    }
33
34    fn type_kinds(&self) -> &'static [&'static str] {
35        &[]
36    }
37
38    fn import_kinds(&self) -> &'static [&'static str] {
39        &[]
40    }
41
42    fn public_symbol_kinds(&self) -> &'static [&'static str] {
43        &["named_node", "capture"]
44    }
45
46    fn visibility_mechanism(&self) -> VisibilityMechanism {
47        VisibilityMechanism::AllPublic
48    }
49
50    fn extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
51        let kind = match node.kind() {
52            "named_node" => SymbolKind::Type,
53            "capture" => SymbolKind::Variable,
54            _ => return Vec::new(),
55        };
56
57        if let Some(name) = self.node_name(node, content) {
58            return vec![Export {
59                name: name.to_string(),
60                kind,
61                line: node.start_position().row + 1,
62            }];
63        }
64        Vec::new()
65    }
66
67    fn scope_creating_kinds(&self) -> &'static [&'static str] {
68        &["grouping"]
69    }
70
71    fn control_flow_kinds(&self) -> &'static [&'static str] {
72        &[]
73    }
74    fn complexity_nodes(&self) -> &'static [&'static str] {
75        &[]
76    }
77    fn nesting_nodes(&self) -> &'static [&'static str] {
78        &["grouping"]
79    }
80
81    fn signature_suffix(&self) -> &'static str {
82        ""
83    }
84
85    fn extract_function(
86        &self,
87        _node: &Node,
88        _content: &str,
89        _in_container: bool,
90    ) -> Option<Symbol> {
91        None
92    }
93    fn extract_container(&self, _node: &Node, _content: &str) -> Option<Symbol> {
94        None
95    }
96    fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
97        None
98    }
99    fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
100        None
101    }
102
103    fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
104        Vec::new()
105    }
106
107    fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
108        Vec::new()
109    }
110
111    fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
112        // Tree-sitter query has no imports
113        String::new()
114    }
115
116    fn is_public(&self, _node: &Node, _content: &str) -> bool {
117        true
118    }
119    fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
120        Visibility::Public
121    }
122
123    fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
124        false
125    }
126
127    fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
128        None
129    }
130
131    fn container_body<'a>(&self, _node: &'a Node<'a>) -> Option<Node<'a>> {
132        None
133    }
134
135    fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
136        false
137    }
138
139    fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
140        node.child_by_field_name("name")
141            .map(|n| &content[n.byte_range()])
142    }
143
144    fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
145        let ext = path.extension()?.to_str()?;
146        if ext != "scm" {
147            return None;
148        }
149        let stem = path.file_stem()?.to_str()?;
150        Some(stem.to_string())
151    }
152
153    fn module_name_to_paths(&self, module: &str) -> Vec<String> {
154        vec![format!("{}.scm", module)]
155    }
156
157    fn lang_key(&self) -> &'static str {
158        "query"
159    }
160
161    fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
162        false
163    }
164    fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
165        None
166    }
167    fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
168        None
169    }
170    fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
171        None
172    }
173    fn get_version(&self, _: &Path) -> Option<String> {
174        None
175    }
176    fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
177        None
178    }
179    fn indexable_extensions(&self) -> &'static [&'static str] {
180        &["scm"]
181    }
182    fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
183        Vec::new()
184    }
185
186    fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
187        use crate::traits::{has_extension, skip_dotfiles};
188        if skip_dotfiles(name) {
189            return true;
190        }
191        !is_dir && !has_extension(name, self.indexable_extensions())
192    }
193
194    fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
195        Vec::new()
196    }
197
198    fn package_module_name(&self, entry_name: &str) -> String {
199        entry_name
200            .strip_suffix(".scm")
201            .unwrap_or(entry_name)
202            .to_string()
203    }
204
205    fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
206        if path.is_file() {
207            Some(path.to_path_buf())
208        } else {
209            None
210        }
211    }
212}
213
214#[cfg(test)]
215mod tests {
216    use super::*;
217    use crate::validate_unused_kinds_audit;
218
219    #[test]
220    fn unused_node_kinds_audit() {
221        #[rustfmt::skip]
222        let documented_unused: &[&str] = &[
223            "identifier", "quantifier", "field_definition", "predicate_type",
224        ];
225        validate_unused_kinds_audit(&Query, documented_unused)
226            .expect("Query unused node kinds audit failed");
227    }
228}