From fd52d721e1ed9794048d63e546f43805d24d7ab8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 17 Nov 2019 20:15:55 +0300 Subject: More correct expansion mapping We can't really map arbitrary ranges, we only can map tokens --- crates/ra_mbe/src/syntax_bridge.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 37382d2df..8398c9ac7 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -77,14 +77,14 @@ pub fn token_tree_to_syntax_node( } impl TokenMap { - pub fn token_by_offset(&self, relative_offset: TextUnit) -> Option { + pub fn token_by_range(&self, relative_range: TextRange) -> Option { let (idx, _) = - self.tokens.iter().enumerate().find(|(_, range)| range.contains(relative_offset))?; + self.tokens.iter().enumerate().find(|(_, range)| **range == relative_range)?; Some(tt::TokenId(idx as u32)) } - pub fn relative_range_of(&self, tt: tt::TokenId) -> Option { - let idx = tt.0 as usize; + pub fn relative_range_of(&self, token_id: tt::TokenId) -> Option { + let idx = token_id.0 as usize; self.tokens.get(idx).copied() } @@ -96,6 +96,11 @@ impl TokenMap { } impl RevTokenMap { + pub fn range_by_token(&self, token_id: tt::TokenId) -> Option { + let &(r, _) = self.ranges.iter().find(|(_, tid)| *tid == token_id)?; + Some(r) + } + fn add(&mut self, relative_range: TextRange, token_id: tt::TokenId) { self.ranges.push((relative_range, token_id.clone())) } -- cgit v1.2.3