Function search_functions

Source
pub fn search_functions(
    parsed_file: &ParsedFile,
    name_pattern: Option<&str>,
) -> Vec<CodeConstruct>
Expand description

Search for function definitions in a parsed file

This is a convenience function that searches for function-like constructs across different programming languages. It automatically selects the appropriate node types based on the file’s language.

§Arguments

  • parsed_file - The parsed file to search within
  • name_pattern - Optional regex pattern to filter results by function name

§Returns

A vector of CodeConstruct objects representing functions, methods, or other callable constructs.

§Supported Languages

  • Python: function_definition
  • Rust: function_item
  • JavaScript/TypeScript: function_declaration, function_expression, arrow_function
  • Java: method_declaration, constructor_declaration
  • C/C++: function_definition
  • Go: function_declaration, method_declaration

§Examples

use tree_parser::{parse_file, search_functions, Language};
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let parsed = parse_file("example.rs", Language::Rust).await?;
     
    // Find all functions
    let all_functions = search_functions(&parsed, None);
     
    // Find functions starting with "test_"
    let test_functions = search_functions(&parsed, Some(r"^test_"));
     
    println!("Found {} functions, {} are tests", all_functions.len(), test_functions.len());
    Ok(())
}