mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
Return all ranges corresponding to a token id in TokenMap
This commit is contained in:
parent
7e31c5ec0d
commit
c5059e0623
6 changed files with 99 additions and 71 deletions
|
@ -58,8 +58,9 @@ macro_rules! foobar {
|
|||
let (node, token_map) = token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap();
|
||||
let content = node.syntax_node().to_string();
|
||||
|
||||
let get_text =
|
||||
|id, kind| -> String { content[token_map.range_by_token(id, kind).unwrap()].to_string() };
|
||||
let get_text = |id, kind| -> String {
|
||||
content[token_map.first_range_by_token(id, kind).unwrap()].to_string()
|
||||
};
|
||||
|
||||
assert_eq!(expanded.token_trees.len(), 4);
|
||||
// {($e:ident) => { fn $e() {} }}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue