diff --git a/cstree/src/syntax/element.rs b/cstree/src/syntax/element.rs index bef407a..27fbdc4 100644 --- a/cstree/src/syntax/element.rs +++ b/cstree/src/syntax/element.rs @@ -292,7 +292,7 @@ impl<'a, S: Syntax, D> SyntaxElementRef<'a, S, D> { /// Returns an iterator along the chain of parents of this node. #[inline] - pub fn ancestors(&self) -> impl Iterator> { + pub fn ancestors(&self) -> impl Iterator> + use<'a, S, D> { match self { NodeOrToken::Node(it) => it.ancestors(), NodeOrToken::Token(it) => it.parent().ancestors(), diff --git a/cstree/src/syntax/resolved.rs b/cstree/src/syntax/resolved.rs index 500d611..4c97301 100644 --- a/cstree/src/syntax/resolved.rs +++ b/cstree/src/syntax/resolved.rs @@ -713,7 +713,7 @@ impl<'a, S: Syntax, D> ResolvedElementRef<'a, S, D> { /// Returns an iterator along the chain of parents of this node. #[inline] - pub fn ancestors(&self) -> impl Iterator> { + pub fn ancestors(&self) -> impl Iterator> + use<'a, S, D> { match self { NodeOrToken::Node(it) => it.ancestors(), NodeOrToken::Token(it) => it.parent().ancestors(), diff --git a/cstree/src/syntax/text.rs b/cstree/src/syntax/text.rs index 5525e4e..a0724f4 100644 --- a/cstree/src/syntax/text.rs +++ b/cstree/src/syntax/text.rs @@ -91,6 +91,18 @@ impl<'n, 'i, I: Resolver + ?Sized, S: Syntax, D> SyntaxText<'n, 'i, I, found(res) } + /// If `self.contains_char(c)`, returns `Some(pos)`, where `pos` is the byte position of the + /// last appearance of `c`. Otherwise, returns `None`. + pub fn rfind_char(&self, c: char) -> Option { + let mut acc: TextSize = 0.into(); + let mut res = None; + self.for_each_chunk(|chunk| { + res = chunk.rfind(c).map(|pos| acc + TextSize::from(pos as u32)).or(res); + acc += TextSize::of(chunk); + }); + res + } + /// If `offset < self.len()`, returns `Some(c)`, where `c` is the first `char` at or after /// `offset` (in bytes). Otherwise, returns `None`. pub fn char_at(&self, offset: TextSize) -> Option { @@ -150,7 +162,7 @@ impl<'n, 'i, I: Resolver + ?Sized, S: Syntax, D> SyntaxText<'n, 'i, I, /// See also [`fold_chunks`](SyntaxText::fold_chunks) for folds that always succeed. pub fn try_fold_chunks(&self, init: T, mut f: F) -> Result where - F: FnMut(T, &str) -> Result, + F: FnMut(T, &'i str) -> Result, { self.tokens_with_ranges().try_fold(init, move |acc, (token, range)| { f(acc, &token.resolve_text(self.resolver)[range]) @@ -195,7 +207,7 @@ impl<'n, 'i, I: Resolver + ?Sized, S: Syntax, D> SyntaxText<'n, 'i, I, self.fold_chunks((), |(), chunk| f(chunk)) } - fn tokens_with_ranges(&self) -> impl Iterator, TextRange)> { + fn tokens_with_ranges(&self) -> impl Iterator, TextRange)> + use<'i, 'n, I, S, D> { let text_range = self.range; self.node .descendants_with_tokens()