pub fn search_by_query(
parsed_file: &ParsedFile,
tree_sitter_query: &str,
) -> Result<Vec<CodeConstruct>, Error>
Expand description
Execute a custom tree-sitter query for advanced searching
This function allows you to use tree-sitter’s powerful query language to perform complex searches on the syntax tree. This provides the most flexibility for finding specific code patterns.
§Arguments
parsed_file
- The parsed file to search withintree_sitter_query
- A tree-sitter query string
§Returns
A Result
containing a vector of CodeConstruct
objects that match
the query, or an Error
if the query is invalid or execution fails.
§Examples
use tree_parser::{parse_file, search_by_query, 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 with decorators
let query = r#"
(decorated_definition
(function_definition
name: (identifier) @func_name))
"#;
let decorated_functions = search_by_query(&parsed, query)?;
println!("Found {} decorated functions", decorated_functions.len());
Ok(())
}
§Errors
This function will return an error if:
- The query syntax is invalid
- The syntax tree is not available
- File I/O operations fail