Skip to main content

codetether_rlm/oracle/tree_sitter_oracle/
functions.rs

1use anyhow::Result;
2
3use super::{FunctionSignature, TreeSitterOracle};
4
5const FUNCTIONS_QUERY: &str = r#"
6(function_item
7    name: (identifier) @name
8    parameters: (parameters) @params
9    return_type: (_)? @return_type)
10"#;
11
12impl TreeSitterOracle {
13    /// Get all function signatures in the source.
14    pub fn get_functions(&mut self) -> Result<Vec<FunctionSignature>> {
15        let result = self.query(FUNCTIONS_QUERY)?;
16        Ok(result.matches.into_iter().map(function_signature).collect())
17    }
18}
19
20fn function_signature(m: super::AstMatch) -> FunctionSignature {
21    FunctionSignature {
22        name: m.captures.get("name").cloned().unwrap_or_default(),
23        params: m.captures.get("params").cloned().unwrap_or_default(),
24        return_type: m.captures.get("return_type").cloned(),
25        line: m.line,
26    }
27}