type_sitter_lib/query/
captures.rsuse crate::{raw, Query, UntypedNode};
use std::fmt::Debug;
use streaming_iterator::StreamingIterator;
#[cfg(not(feature = "yak-sitter"))]
use tree_sitter::Point;
#[cfg(feature = "yak-sitter")]
use yak_sitter::PointRange;
#[cfg(feature = "yak-sitter")]
pub struct QueryCaptures<'query, 'tree: 'query, Query: crate::Query> {
typed_query: &'query Query,
untyped_captures: raw::QueryCaptures<'query, 'tree>,
}
#[cfg(not(feature = "yak-sitter"))]
pub struct QueryCaptures<'query, 'tree: 'query, Query: crate::Query, Text: raw::TextProvider<I>, I: AsRef<[u8]>> {
typed_query: &'query Query,
untyped_captures: raw::QueryCaptures<'query, 'tree, Text, I>,
}
pub trait QueryCapture<'query, 'tree: 'query>: Debug + Clone {
type Query: Query<Capture<'query, 'tree> = Self>;
fn query(&self) -> &'query Self::Query;
#[cfg(feature = "yak-sitter")]
fn raw(&self) -> raw::QueryCapture<'query, 'tree>;
#[cfg(not(feature = "yak-sitter"))]
fn raw(&self) -> raw::QueryCapture<'tree>;
fn node(&self) -> &UntypedNode<'tree>;
fn node_mut(&mut self) -> &mut UntypedNode<'tree>;
fn name(&self) -> &'query str;
#[allow(clippy::unnecessary_cast)]
#[inline]
fn index(&self) -> usize {
self.raw().index as usize
}
}
#[cfg(feature = "yak-sitter")]
impl<'query, 'tree: 'query, Query: crate::Query> QueryCaptures<'query, 'tree, Query> {
#[inline]
pub(super) unsafe fn new(
typed_query: &'query Query,
untyped_captures: raw::QueryCaptures<'query, 'tree>,
) -> Self {
Self { typed_query, untyped_captures }
}
#[inline]
pub fn set_byte_range(&mut self, range: std::ops::Range<usize>) {
self.untyped_captures.set_byte_range(range)
}
#[inline]
pub fn set_point_range(&mut self, range: PointRange) {
self.untyped_captures.set_point_range(range)
}
}
#[cfg(not(feature = "yak-sitter"))]
impl<'query, 'tree: 'query, Query: crate::Query, Text: raw::TextProvider<I>, I: AsRef<[u8]>> QueryCaptures<'query, 'tree, Query, Text, I> {
#[inline]
pub(super) unsafe fn new(
typed_query: &'query Query,
untyped_captures: raw::QueryCaptures<'query, 'tree, Text, I>,
) -> Self {
Self { typed_query, untyped_captures }
}
#[inline]
pub fn set_byte_range(&mut self, range: std::ops::Range<usize>) {
self.untyped_captures.set_byte_range(range)
}
#[inline]
pub fn set_point_range(&mut self, range: std::ops::Range<Point>) {
self.untyped_captures.set_point_range(range)
}
}
#[cfg(feature = "yak-sitter")]
impl<'query, 'tree: 'query, Query: crate::Query> Iterator for QueryCaptures<'query, 'tree, Query> {
type Item = Query::Capture<'query, 'tree>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let query = self.untyped_captures.query();
let tree = self.untyped_captures.tree();
unsafe { self.untyped_captures.as_inner_mut().next().map(|(m, index)| {
let inner_capture = m.captures[*index];
self.typed_query.wrap_capture(
raw::QueryCapture {
node: raw::Node::new(inner_capture.node, tree),
index: inner_capture.index as usize,
name: query.capture_names()[inner_capture.index as usize],
},
)
}) }
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.untyped_captures.size_hint()
}
}
#[cfg(not(feature = "yak-sitter"))]
impl<'query, 'tree: 'query, Query: crate::Query, Text: raw::TextProvider<I>, I: AsRef<[u8]>> Iterator for QueryCaptures<'query, 'tree, Query, Text, I> {
type Item = Query::Capture<'query, 'tree>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
unsafe { self.untyped_captures.next().map(|(m, index)| {
self.typed_query.wrap_capture(m.captures[*index])
}) }
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.untyped_captures.size_hint()
}
}