Skip to content

Commit

Permalink
perf: Restrict query running to only node bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jul 22, 2024
1 parent 78dae08 commit c903b94
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions parsing/src/identifier_extractor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tree_sitter::{Language, Node, Query, QueryCapture, QueryCursor};
use tree_sitter::{Language, Node, Query, QueryCursor};

pub trait IdentifierExtractor {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>>;
Expand All @@ -19,13 +19,9 @@ impl IdentifierExtractor for TreeSitterQuery {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>> {
let mut cursor = QueryCursor::new();
cursor
.set_byte_range(node.byte_range())
.matches(&self.0, node, src.as_bytes())
.find(|a_match| {
a_match
.captures
.iter()
.all(|a_capture| capture_is_within_node_bounds(a_capture, &node))
})
.next()
.map(|a_match| {
a_match
.captures
Expand All @@ -35,7 +31,3 @@ impl IdentifierExtractor for TreeSitterQuery {
})
}
}

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 c903b94

Please sign in to comment.