binding_rust: Unbind nodes lifitime from a source for QueryCursor.captures

This commit is contained in:
Andrew Hlynskyi 2021-04-22 12:31:32 +03:00
parent 9c91affe56
commit de23c9219a
2 changed files with 16 additions and 16 deletions

View file

@ -83,7 +83,7 @@ struct LocalScope<'a> {
local_defs: Vec<LocalDef<'a>>,
}
struct HighlightIter<'a, F>
struct HighlightIter<'a, 'tree: 'a, F>
where
F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
{
@ -92,16 +92,16 @@ where
highlighter: &'a mut Highlighter,
injection_callback: F,
cancellation_flag: Option<&'a AtomicUsize>,
layers: Vec<HighlightIterLayer<'a>>,
layers: Vec<HighlightIterLayer<'a, 'tree>>,
iter_count: usize,
next_event: Option<HighlightEvent>,
last_highlight_range: Option<(usize, usize, usize)>,
}
struct HighlightIterLayer<'a> {
struct HighlightIterLayer<'a, 'tree: 'a> {
_tree: Tree,
cursor: QueryCursor,
captures: iter::Peekable<QueryCaptures<'a, &'a [u8]>>,
captures: iter::Peekable<QueryCaptures<'a, 'tree, &'a [u8]>>,
config: &'a HighlightConfiguration,
highlight_end_stack: Vec<usize>,
scope_stack: Vec<LocalScope<'a>>,
@ -319,7 +319,7 @@ impl HighlightConfiguration {
}
}
impl<'a> HighlightIterLayer<'a> {
impl<'a, 'tree: 'a> HighlightIterLayer<'a, 'tree> {
/// Create a new 'layer' of highlighting for this document.
///
/// In the even that the new layer contains "combined injections" (injections where multiple
@ -548,7 +548,7 @@ impl<'a> HighlightIterLayer<'a> {
}
}
impl<'a, F> HighlightIter<'a, F>
impl<'a, 'tree: 'a, F> HighlightIter<'a, 'tree, F>
where
F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
{
@ -596,7 +596,7 @@ where
}
}
fn insert_layer(&mut self, mut layer: HighlightIterLayer<'a>) {
fn insert_layer(&mut self, mut layer: HighlightIterLayer<'a, 'tree>) {
if let Some(sort_key) = layer.sort_key() {
let mut i = 1;
while i < self.layers.len() {
@ -615,7 +615,7 @@ where
}
}
impl<'a, F> Iterator for HighlightIter<'a, F>
impl<'a, 'tree: 'a, F> Iterator for HighlightIter<'a, 'tree, F>
where
F: FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
{

View file

@ -134,10 +134,10 @@ pub struct QueryMatch<'a> {
}
/// A sequence of `QueryCapture`s within a `QueryMatch`.
pub struct QueryCaptures<'a, T: AsRef<[u8]>> {
pub struct QueryCaptures<'a, 'tree: 'a, T: AsRef<[u8]>> {
ptr: *mut ffi::TSQueryCursor,
query: &'a Query,
text_callback: Box<dyn FnMut(Node<'a>) -> T + 'a>,
text_callback: Box<dyn FnMut(Node<'tree>) -> T + 'a>,
}
/// A particular `Node` that has been captured with a particular name within a `Query`.
@ -1633,12 +1633,12 @@ impl<'a> QueryCursor {
///
/// This is useful if don't care about which pattern matched, and just want a single,
/// ordered sequence of captures.
pub fn captures<T: AsRef<[u8]>>(
pub fn captures<'tree, T: AsRef<[u8]>>(
&'a mut self,
query: &'a Query,
node: Node<'a>,
text_callback: impl FnMut(Node<'a>) -> T + 'a,
) -> QueryCaptures<'a, T> {
node: Node<'tree>,
text_callback: impl FnMut(Node<'tree>) -> T + 'a,
) -> QueryCaptures<'a, 'tree, T> {
let ptr = self.0.as_ptr();
unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) };
QueryCaptures {
@ -1732,8 +1732,8 @@ impl QueryProperty {
}
}
impl<'a, T: AsRef<[u8]>> Iterator for QueryCaptures<'a, T> {
type Item = (QueryMatch<'a>, usize);
impl<'a, 'tree: 'a, T: AsRef<[u8]>> Iterator for QueryCaptures<'a, 'tree, T> {
type Item = (QueryMatch<'tree>, usize);
fn next(&mut self) -> Option<Self::Item> {
loop {