diff --git a/parsing/src/identifier_extractor.rs b/parsing/src/identifier_extractor.rs index 7b173b1..12d36cd 100644 --- a/parsing/src/identifier_extractor.rs +++ b/parsing/src/identifier_extractor.rs @@ -1,5 +1,5 @@ use regex::Regex; -use tree_sitter::{Node, Query, QueryCursor}; +use tree_sitter::{Language, Node, Query, QueryCursor}; pub trait IdentifierExtractor { fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option>; @@ -9,9 +9,10 @@ pub struct RegularExpression(Regex); impl RegularExpression { pub fn new(regex: &'static str) -> Self { - let regex_query = regex::Regex::new(regex) - .expect("Invalid regex provided for building RegularExpression"); - Self(regex_query) + Self( + regex::Regex::new(regex) + .expect("Invalid regex provided for building RegularExpression"), + ) } } @@ -23,20 +24,22 @@ impl IdentifierExtractor for RegularExpression { } } -pub struct TreeSitterQuery(&'static str); +pub struct TreeSitterQuery(Query); impl TreeSitterQuery { - pub fn new(query: &'static str) -> Self { - Self(query) + pub fn new(query: &'static str, language: Language) -> Self { + Self( + Query::new(language, query) + .expect("Invalid Query provided for building TreeSitterQuery"), + ) } } impl IdentifierExtractor for TreeSitterQuery { fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option> { - let query = Query::new(node.language(), self.0).ok()?; let mut cursor = QueryCursor::new(); let identifier = cursor - .matches(&query, node, src.as_bytes()) + .matches(&self.0, node, src.as_bytes()) .flat_map(|a_match| { a_match .captures diff --git a/parsing/src/tree_sitter_parser.rs b/parsing/src/tree_sitter_parser.rs index bd78e60..89b085d 100644 --- a/parsing/src/tree_sitter_parser.rs +++ b/parsing/src/tree_sitter_parser.rs @@ -30,18 +30,20 @@ impl From for ParserConfiguration { identifier_extractors: { let mut map: HashMap<&'static str, Box> = HashMap::new(); - map.insert("constructor_declaration", Box::new(TreeSitterQuery::new(r#"(constructor_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#))); - map.insert("method_declaration", Box::new(TreeSitterQuery::new(r#"(method_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#))); + map.insert("constructor_declaration", Box::new(TreeSitterQuery::new(r#"(constructor_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#, tree_sitter_java::language()))); + map.insert("method_declaration", Box::new(TreeSitterQuery::new(r#"(method_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#, tree_sitter_java::language()))); map.insert( "field_declaration", Box::new(TreeSitterQuery::new( r#"(variable_declarator name: _ @name)"#, + tree_sitter_java::language(), )), ); map.insert( "import_declaration", Box::new(TreeSitterQuery::new( r#"(import_declaration "import" _ @resource)"#, + tree_sitter_java::language(), )), );