pub fn search_by_node_type(
parsed_file: &ParsedFile,
node_type: &str,
name_pattern: Option<&str>,
) -> Vec<CodeConstruct>
Expand description
Search for code constructs by their tree-sitter node type
This function searches through all code constructs in a parsed file and returns those that match the specified node type. Optionally, results can be filtered by a regex pattern applied to construct names.
§Arguments
parsed_file
- The parsed file to search withinnode_type
- The tree-sitter node type to search for (e.g., “function_definition”)name_pattern
- Optional regex pattern to filter results by construct name
§Returns
A vector of CodeConstruct
objects that match the search criteria.
§Examples
use tree_parser::{parse_file, search_by_node_type, Language};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let parsed = parse_file("example.py", Language::Python).await?;
// Find all function definitions
let functions = search_by_node_type(&parsed, "function_definition", None);
// Find functions with names starting with "test_"
let test_functions = search_by_node_type(&parsed, "function_definition", Some(r"^test_"));
println!("Found {} functions, {} are tests", functions.len(), test_functions.len());
Ok(())
}