aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-18 12:08:39 +0000
committerAleksey Kladov <[email protected]>2019-11-18 12:08:39 +0000
commitb79d6789236bb53c5818949cc2960b5c4991cbeb (patch)
tree3985535b59ac5061f3c6fb7ce9c109b454dc2c1e /crates/ra_mbe/src/syntax_bridge.rs
parent5e8c22dbf35e815af148fbdd8dd6da4b50f7aea7 (diff)
Token-based reverse-mapping
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs6
1 files changed, 5 insertions, 1 deletions
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 {
20/// Maps relative range of the expanded syntax node to `tt::TokenId` 20/// Maps relative range of the expanded syntax node to `tt::TokenId`
21#[derive(Debug, PartialEq, Eq, Default)] 21#[derive(Debug, PartialEq, Eq, Default)]
22pub struct RevTokenMap { 22pub struct RevTokenMap {
23 pub ranges: Vec<(TextRange, tt::TokenId)>, 23 ranges: Vec<(TextRange, tt::TokenId)>,
24} 24}
25 25
26/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro 26/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
@@ -96,6 +96,10 @@ impl TokenMap {
96} 96}
97 97
98impl RevTokenMap { 98impl RevTokenMap {
99 pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> {
100 self.ranges.iter().find(|&it| it.0 == relative_range).map(|it| it.1)
101 }
102
99 pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TextRange> { 103 pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TextRange> {
100 let &(r, _) = self.ranges.iter().find(|(_, tid)| *tid == token_id)?; 104 let &(r, _) = self.ranges.iter().find(|(_, tid)| *tid == token_id)?;
101 Some(r) 105 Some(r)