aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs15
1 files changed, 13 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 3f57ce3b5..8398c9ac7 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -77,8 +77,14 @@ pub fn token_tree_to_syntax_node(
77} 77}
78 78
79impl TokenMap { 79impl TokenMap {
80 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { 80 pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> {
81 let idx = tt.0 as usize; 81 let (idx, _) =
82 self.tokens.iter().enumerate().find(|(_, range)| **range == relative_range)?;
83 Some(tt::TokenId(idx as u32))
84 }
85
86 pub fn relative_range_of(&self, token_id: tt::TokenId) -> Option<TextRange> {
87 let idx = token_id.0 as usize;
82 self.tokens.get(idx).copied() 88 self.tokens.get(idx).copied()
83 } 89 }
84 90
@@ -90,6 +96,11 @@ impl TokenMap {
90} 96}
91 97
92impl RevTokenMap { 98impl RevTokenMap {
99 pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TextRange> {
100 let &(r, _) = self.ranges.iter().find(|(_, tid)| *tid == token_id)?;
101 Some(r)
102 }
103
93 fn add(&mut self, relative_range: TextRange, token_id: tt::TokenId) { 104 fn add(&mut self, relative_range: TextRange, token_id: tt::TokenId) {
94 self.ranges.push((relative_range, token_id.clone())) 105 self.ranges.push((relative_range, token_id.clone()))
95 } 106 }