From 4003a533d883b945925cf389f6105a9c743c0a49 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Sun, 21 Jul 2024 12:26:50 -0300 Subject: [PATCH] refactor: condense on filter map --- parsing/src/identifier_extractor.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/parsing/src/identifier_extractor.rs b/parsing/src/identifier_extractor.rs index 12d36cd..6194a56 100644 --- a/parsing/src/identifier_extractor.rs +++ b/parsing/src/identifier_extractor.rs @@ -1,5 +1,5 @@ use regex::Regex; -use tree_sitter::{Language, Node, Query, QueryCursor}; +use tree_sitter::{Language, Node, Query, QueryCapture, QueryCursor}; pub trait IdentifierExtractor { fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option>; @@ -41,16 +41,19 @@ impl IdentifierExtractor for TreeSitterQuery { let identifier = cursor .matches(&self.0, node, src.as_bytes()) .flat_map(|a_match| { - a_match - .captures - .iter() - .filter(|capture| { - capture.node.start_byte() >= node.start_byte() - && capture.node.end_byte() <= node.end_byte() - }) - .filter_map(|capture_index| capture_index.node.utf8_text(src.as_bytes()).ok()) + a_match.captures.iter().filter_map(|capture| { + if capture_is_within_node_bounds(capture, &node) { + capture.node.utf8_text(src.as_bytes()).ok() + } else { + None + } + }) }) .collect(); Some(identifier) } } + +fn capture_is_within_node_bounds(capture: &QueryCapture, node: &Node) -> bool { + capture.node.start_byte() >= node.start_byte() && capture.node.end_byte() <= node.end_byte() +}