diff options
author | Edwin Cheng <[email protected]> | 2019-11-09 04:00:46 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-11-09 04:00:46 +0000 |
commit | 0a5ec69404a2556dd82e5bb00b295aebaa291f04 (patch) | |
tree | 1ae5ae1ef0ca23dc917d845312c6753d9b46b1ab /crates/ra_mbe | |
parent | d01e0abdb57c5be340934cb51e2f6b1a2e6c0373 (diff) |
Remove map_ranges in RevTokenMap
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 32 |
1 files changed, 1 insertions, 31 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index a49e63ace..9653f7fef 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -23,7 +23,7 @@ pub struct TokenMap { | |||
23 | /// Maps relative range of the expanded syntax node to `tt::TokenId` | 23 | /// Maps relative range of the expanded syntax node to `tt::TokenId` |
24 | #[derive(Debug, PartialEq, Eq, Default)] | 24 | #[derive(Debug, PartialEq, Eq, Default)] |
25 | pub struct RevTokenMap { | 25 | pub struct RevTokenMap { |
26 | ranges: Vec<(TextRange, tt::TokenId)>, | 26 | pub ranges: Vec<(TextRange, tt::TokenId)>, |
27 | } | 27 | } |
28 | 28 | ||
29 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro | 29 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro |
@@ -121,36 +121,6 @@ impl RevTokenMap { | |||
121 | fn add(&mut self, relative_range: TextRange, token_id: tt::TokenId) { | 121 | fn add(&mut self, relative_range: TextRange, token_id: tt::TokenId) { |
122 | self.ranges.push((relative_range, token_id.clone())) | 122 | self.ranges.push((relative_range, token_id.clone())) |
123 | } | 123 | } |
124 | |||
125 | /// Map a given token map to (Expanded syntax node, Input tokens) text-ranges pair | ||
126 | /// | ||
127 | /// This function do the following things: | ||
128 | /// | ||
129 | /// 1. Undo the increment of token-id `shift`: | ||
130 | /// When we output a token from from macro argument, we increased its id | ||
131 | /// by `shift` (so it's guaranteed to not to collide with anything from the definition) | ||
132 | /// We undo the increment here to rollback to its original token id. | ||
133 | /// 2. Offset the input tokens (`to`) by `parent` text-range: | ||
134 | /// We transforms the input tokens text-ranges from relative to original first token | ||
135 | /// to parent text-range | ||
136 | /// 3. Maps expanded tokens text-ranges to parent text-ranges | ||
137 | /// | ||
138 | pub fn map_ranges( | ||
139 | &self, | ||
140 | to: &TokenMap, | ||
141 | parent: TextRange, | ||
142 | shift: u32, | ||
143 | ) -> Vec<(TextRange, TextRange)> { | ||
144 | self.ranges | ||
145 | .iter() | ||
146 | .filter_map(|(r, tid)| { | ||
147 | let adjusted_id = tt::TokenId(tid.0.checked_sub(shift)?); | ||
148 | let to_range = to.relative_range_of(adjusted_id)?; | ||
149 | |||
150 | Some((*r, TextRange::offset_len(to_range.start() + parent.start(), to_range.len()))) | ||
151 | }) | ||
152 | .collect() | ||
153 | } | ||
154 | } | 124 | } |
155 | 125 | ||
156 | /// Returns the textual content of a doc comment block as a quoted string | 126 | /// Returns the textual content of a doc comment block as a quoted string |