diff options
author | Aleksey Kladov <[email protected]> | 2019-11-17 16:11:43 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-17 16:11:43 +0000 |
commit | c8f858d04323f93a4bacb143d92c976b2bc1e179 (patch) | |
tree | ebef32480be69fe1fad58678f8ad9ac97669453a /crates/ra_hir_expand | |
parent | 64dac40a8665c851e48c50e6f778a51aeac2838e (diff) |
Make shift an implementation detail of mbe
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r-- | crates/ra_hir_expand/src/db.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 12 |
2 files changed, 16 insertions, 9 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 5eadee9c2..9de7c1ea8 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs | |||
@@ -32,10 +32,17 @@ impl TokenExpander { | |||
32 | } | 32 | } |
33 | } | 33 | } |
34 | 34 | ||
35 | pub fn shift(&self) -> u32 { | 35 | pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId { |
36 | match self { | 36 | match self { |
37 | TokenExpander::MacroRules(it) => it.shift(), | 37 | TokenExpander::MacroRules(it) => it.map_id_down(id), |
38 | TokenExpander::Builtin(_) => 0, | 38 | TokenExpander::Builtin(..) => id, |
39 | } | ||
40 | } | ||
41 | |||
42 | pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) { | ||
43 | match self { | ||
44 | TokenExpander::MacroRules(it) => it.map_id_up(id), | ||
45 | TokenExpander::Builtin(..) => (id, mbe::Origin::Def), | ||
39 | } | 46 | } |
40 | } | 47 | } |
41 | } | 48 | } |
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 1774cb58b..5927a03ba 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -161,7 +161,7 @@ impl ExpansionInfo { | |||
161 | pub fn translate_offset(&self, offset: TextUnit) -> Option<TextUnit> { | 161 | pub fn translate_offset(&self, offset: TextUnit) -> Option<TextUnit> { |
162 | let offset = offset.checked_sub(self.arg_start.1)?; | 162 | let offset = offset.checked_sub(self.arg_start.1)?; |
163 | let token_id = self.macro_arg.1.token_by_offset(offset)?; | 163 | let token_id = self.macro_arg.1.token_by_offset(offset)?; |
164 | let token_id = tt::TokenId(token_id.0 + self.macro_def.0.shift()); | 164 | let token_id = self.macro_def.0.map_id_down(token_id); |
165 | 165 | ||
166 | let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?; | 166 | let (r, _) = self.exp_map.ranges.iter().find(|(_, tid)| *tid == token_id)?; |
167 | Some(r.start()) | 167 | Some(r.start()) |
@@ -170,11 +170,11 @@ impl ExpansionInfo { | |||
170 | pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { | 170 | pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { |
171 | let token_id = look_in_rev_map(&self.exp_map, from)?; | 171 | let token_id = look_in_rev_map(&self.exp_map, from)?; |
172 | 172 | ||
173 | let shift = self.macro_def.0.shift(); | 173 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); |
174 | let (token_map, (file_id, start_offset), token_id) = if token_id.0 >= shift { | 174 | |
175 | (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - shift).into()) | 175 | let (token_map, (file_id, start_offset)) = match origin { |
176 | } else { | 176 | mbe::Origin::Call => (&self.macro_arg.1, self.arg_start), |
177 | (&self.macro_def.1, self.def_start, token_id) | 177 | mbe::Origin::Def => (&self.macro_def.1, self.def_start), |
178 | }; | 178 | }; |
179 | 179 | ||
180 | let range = token_map.relative_range_of(token_id)?; | 180 | let range = token_map.relative_range_of(token_id)?; |