pub fn search_classes(
parsed_file: &ParsedFile,
name_pattern: Option<&str>,
) -> Vec<CodeConstruct>
Expand description
Search for class and type definitions in a parsed file
This is a convenience function that searches for class-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 withinname_pattern
- Optional regex pattern to filter results by class/type name
§Returns
A vector of CodeConstruct
objects representing classes, structs,
interfaces, enums, or other type definitions.
§Supported Languages
- Python:
class_definition
- Rust:
struct_item
,enum_item
- JavaScript:
class_declaration
- TypeScript:
class_declaration
,interface_declaration
- Java:
class_declaration
,interface_declaration
- C:
struct_specifier
,union_specifier
,enum_specifier
- C++:
class_specifier
,struct_specifier
,union_specifier
,enum_specifier
- Go:
type_declaration
§Examples
use tree_parser::{parse_file, search_classes, Language};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let parsed = parse_file("example.py", Language::Python).await?;
// Find all class definitions
let all_classes = search_classes(&parsed, None);
// Find classes with names ending in "Error"
let error_classes = search_classes(&parsed, Some(r"Error$"));
println!("Found {} classes, {} are error types", all_classes.len(), error_classes.len());
Ok(())
}