diff options
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 41 |
1 files changed, 11 insertions, 30 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 2f21ad6e0..0c499cd03 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -12,31 +12,12 @@ use tt::buffer::{Cursor, TokenBuffer}; | |||
12 | 12 | ||
13 | use crate::subtree_source::SubtreeTokenSource; | 13 | use crate::subtree_source::SubtreeTokenSource; |
14 | use crate::ExpandError; | 14 | use crate::ExpandError; |
15 | use std::sync::atomic::{AtomicU32, Ordering}; | ||
16 | 15 | ||
17 | /// Maps `tt::TokenId` to the relative range of the original token. | 16 | /// Maps `tt::TokenId` to the relative range of the original token. |
18 | #[derive(Debug, PartialEq, Eq)] | 17 | #[derive(Debug, PartialEq, Eq, Default)] |
19 | pub struct TokenMap { | 18 | pub struct TokenMap { |
20 | /// Maps `tt::TokenId` to the *relative* source range. | 19 | /// Maps `tt::TokenId` to the *relative* source range. |
21 | tokens: Vec<TextRange>, | 20 | tokens: Vec<TextRange>, |
22 | map_id: u32, | ||
23 | } | ||
24 | |||
25 | static TOKEN_MAP_COUNTER: AtomicU32 = AtomicU32::new(0); | ||
26 | |||
27 | /// Generate an unique token map id for each instance | ||
28 | fn make_uniq_token_map_id() -> u32 { | ||
29 | let res = TOKEN_MAP_COUNTER.fetch_add(1, Ordering::SeqCst); | ||
30 | if res == std::u32::MAX { | ||
31 | panic!("TOKEN_MAP_COUNTER is overflowed"); | ||
32 | } | ||
33 | res | ||
34 | } | ||
35 | |||
36 | impl std::default::Default for TokenMap { | ||
37 | fn default() -> TokenMap { | ||
38 | TokenMap { tokens: Default::default(), map_id: make_uniq_token_map_id() } | ||
39 | } | ||
40 | } | 21 | } |
41 | 22 | ||
42 | /// Maps relative range of the expanded syntax node to `tt::TokenId` | 23 | /// Maps relative range of the expanded syntax node to `tt::TokenId` |
@@ -140,17 +121,14 @@ pub fn token_tree_to_items( | |||
140 | 121 | ||
141 | impl TokenMap { | 122 | impl TokenMap { |
142 | pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { | 123 | pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { |
143 | if self.map_id != tt.map_id() { | 124 | let idx = tt.0 as usize; |
144 | return None; | ||
145 | } | ||
146 | let idx = tt.token_id() as usize; | ||
147 | self.tokens.get(idx).copied() | 125 | self.tokens.get(idx).copied() |
148 | } | 126 | } |
149 | 127 | ||
150 | fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { | 128 | fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { |
151 | let id = self.tokens.len(); | 129 | let id = self.tokens.len(); |
152 | self.tokens.push(relative_range); | 130 | self.tokens.push(relative_range); |
153 | tt::TokenId::new(id as u32, self.map_id) | 131 | tt::TokenId(id as u32) |
154 | } | 132 | } |
155 | } | 133 | } |
156 | 134 | ||
@@ -159,14 +137,17 @@ impl ExpandedRangeMap { | |||
159 | self.ranges.push((relative_range, token_id.clone())) | 137 | self.ranges.push((relative_range, token_id.clone())) |
160 | } | 138 | } |
161 | 139 | ||
162 | pub fn ranges(&self, to: &TokenMap, start: TextUnit) -> Vec<(TextRange, TextRange)> { | 140 | pub fn map_ranges( |
141 | &self, | ||
142 | to: &TokenMap, | ||
143 | start: TextUnit, | ||
144 | shift: u32, | ||
145 | ) -> Vec<(TextRange, TextRange)> { | ||
163 | self.ranges | 146 | self.ranges |
164 | .iter() | 147 | .iter() |
148 | .filter_map(|(r, tid)| if shift <= tid.0 { Some((r, tid.0 - shift)) } else { None }) | ||
165 | .filter_map(|(r, tid)| { | 149 | .filter_map(|(r, tid)| { |
166 | if to.map_id != tid.map_id() { | 150 | if let Some(to_range) = to.relative_range_of(tt::TokenId(tid)) { |
167 | return None; | ||
168 | } | ||
169 | if let Some(to_range) = to.relative_range_of(*tid) { | ||
170 | Some((*r, TextRange::from_to(to_range.start() + start, to_range.end() + start))) | 151 | Some((*r, TextRange::from_to(to_range.start() + start, to_range.end() + start))) |
171 | } else { | 152 | } else { |
172 | None | 153 | None |