1
Fork 0
mirror of https://github.com/RGBCube/cstree synced 2025-08-01 11:37:46 +00:00

Compare commits

...

3 commits

Author SHA1 Message Date
a102fdf1d4
merge feature branches 2025-07-29 19:30:11 +03:00
35c2799059
treewide: specify lifetimes correctly
don't overrestrict lifetimes, prefer `use<...>` to prevent `impl Trait` from capturing too much
2025-07-29 19:28:44 +03:00
c35e88c3da
text: add SyntaxText::rfind_char 2025-07-29 19:08:02 +03:00
3 changed files with 16 additions and 4 deletions

View file

@ -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<Item = &'a SyntaxNode<S, D>> {
pub fn ancestors(&self) -> impl Iterator<Item = &'a SyntaxNode<S, D>> + use<'a, S, D> {
match self {
NodeOrToken::Node(it) => it.ancestors(),
NodeOrToken::Token(it) => it.parent().ancestors(),

View file

@ -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<Item = &'a ResolvedNode<S, D>> {
pub fn ancestors(&self) -> impl Iterator<Item = &'a ResolvedNode<S, D>> + use<'a, S, D> {
match self {
NodeOrToken::Node(it) => it.ancestors(),
NodeOrToken::Token(it) => it.parent().ancestors(),

View file

@ -91,6 +91,18 @@ impl<'n, 'i, I: Resolver<TokenKey> + ?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<TextSize> {
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<char> {
@ -150,7 +162,7 @@ impl<'n, 'i, I: Resolver<TokenKey> + ?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<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
where
F: FnMut(T, &str) -> Result<T, E>,
F: FnMut(T, &'i str) -> Result<T, E>,
{
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<TokenKey> + ?Sized, S: Syntax, D> SyntaxText<'n, 'i, I,
self.fold_chunks((), |(), chunk| f(chunk))
}
fn tokens_with_ranges(&self) -> impl Iterator<Item = (&SyntaxToken<S, D>, TextRange)> {
fn tokens_with_ranges(&self) -> impl Iterator<Item = (&'n SyntaxToken<S, D>, TextRange)> + use<'i, 'n, I, S, D> {
let text_range = self.range;
self.node
.descendants_with_tokens()