From b79d6789236bb53c5818949cc2960b5c4991cbeb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 18 Nov 2019 15:08:39 +0300 Subject: Token-based reverse-mapping --- crates/ra_mbe/src/syntax_bridge.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 8398c9ac7..fe3b70b8d 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -20,7 +20,7 @@ pub struct TokenMap { /// Maps relative range of the expanded syntax node to `tt::TokenId` #[derive(Debug, PartialEq, Eq, Default)] pub struct RevTokenMap { - pub ranges: Vec<(TextRange, tt::TokenId)>, + ranges: Vec<(TextRange, tt::TokenId)>, } /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro @@ -96,6 +96,10 @@ impl TokenMap { } impl RevTokenMap { + pub fn token_by_range(&self, relative_range: TextRange) -> Option { + self.ranges.iter().find(|&it| it.0 == relative_range).map(|it| it.1) + } + pub fn range_by_token(&self, token_id: tt::TokenId) -> Option { let &(r, _) = self.ranges.iter().find(|(_, tid)| *tid == token_id)?; Some(r) -- cgit v1.2.3