Skip to content

Commit

Permalink
refactor: condense on filter map
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jul 21, 2024
1 parent 459d32a commit 4003a53
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions parsing/src/identifier_extractor.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<&'a str>>;
Expand Down Expand Up @@ -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()
}

0 comments on commit 4003a53

Please sign in to comment.