Return all ranges corresponding to a token id in TokenMap

This commit is contained in:
Lukas Wirth 2021-08-28 21:18:56 +02:00
parent 7e31c5ec0d
commit c5059e0623
6 changed files with 99 additions and 71 deletions

View file

@ -46,9 +46,23 @@ impl TokenMap {
Some(token_id)
}
pub fn range_by_token(&self, token_id: tt::TokenId, kind: SyntaxKind) -> Option<TextRange> {
let &(_, range) = self.entries.iter().find(|(tid, _)| *tid == token_id)?;
range.by_kind(kind)
pub fn ranges_by_token(
&self,
token_id: tt::TokenId,
kind: SyntaxKind,
) -> impl Iterator<Item = TextRange> + '_ {
self.entries
.iter()
.filter(move |&&(tid, _)| tid == token_id)
.filter_map(move |(_, range)| range.by_kind(kind))
}
pub fn first_range_by_token(
&self,
token_id: tt::TokenId,
kind: SyntaxKind,
) -> Option<TextRange> {
self.ranges_by_token(token_id, kind).next()
}
pub(crate) fn shrink_to_fit(&mut self) {