Skip to main content

codetether_rlm/oracle/tree_sitter_oracle/
impls.rs

1use anyhow::Result;
2
3use super::{ImplDefinition, TreeSitterOracle};
4
5const IMPLS_QUERY: &str = r#"
6[
7    (impl_item
8        type: (type_identifier) @type
9        trait: (type_identifier)? @trait
10        body: (declaration_list)? @body)
11    (impl_item
12        for: (type_identifier) @for
13        trait: (type_identifier) @trait
14        body: (declaration_list)? @body)
15]
16"#;
17
18impl TreeSitterOracle {
19    /// Get all impl blocks in the source.
20    pub fn get_impls(&mut self) -> Result<Vec<ImplDefinition>> {
21        let result = self.query(IMPLS_QUERY)?;
22        Ok(result.matches.into_iter().map(impl_definition).collect())
23    }
24}
25
26fn impl_definition(m: super::AstMatch) -> ImplDefinition {
27    let type_name = m
28        .captures
29        .get("type")
30        .or_else(|| m.captures.get("for"))
31        .cloned()
32        .unwrap_or_default();
33    let body = m.captures.get("body").cloned().unwrap_or_default();
34    ImplDefinition {
35        type_name,
36        trait_name: m.captures.get("trait").cloned(),
37        method_count: body.matches("fn ").count(),
38        line: m.line,
39    }
40}