diff options
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 1a1d6bdd4..b219b8fbf 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -12,11 +12,12 @@ pub mod hygiene; | |||
12 | pub mod diagnostics; | 12 | pub mod diagnostics; |
13 | 13 | ||
14 | use std::hash::{Hash, Hasher}; | 14 | use std::hash::{Hash, Hasher}; |
15 | use std::sync::Arc; | ||
15 | 16 | ||
16 | use ra_db::{salsa, CrateId, FileId}; | 17 | use ra_db::{salsa, CrateId, FileId}; |
17 | use ra_syntax::{ | 18 | use ra_syntax::{ |
18 | ast::{self, AstNode}, | 19 | ast::{self, AstNode}, |
19 | SyntaxNode, TextRange, | 20 | SyntaxNode, TextRange, TextUnit, |
20 | }; | 21 | }; |
21 | 22 | ||
22 | use crate::ast_id_map::FileAstId; | 23 | use crate::ast_id_map::FileAstId; |
@@ -68,29 +69,25 @@ impl HirFileId { | |||
68 | } | 69 | } |
69 | 70 | ||
70 | /// Return expansion information if it is a macro-expansion file | 71 | /// Return expansion information if it is a macro-expansion file |
71 | pub fn parent_expansion(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> { | 72 | pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> { |
72 | match self.0 { | 73 | match self.0 { |
73 | HirFileIdRepr::FileId(_) => None, | 74 | HirFileIdRepr::FileId(_) => None, |
74 | HirFileIdRepr::MacroFile(macro_file) => { | 75 | HirFileIdRepr::MacroFile(macro_file) => { |
75 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | 76 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); |
76 | 77 | ||
77 | let arg_range = loc.ast_id.to_node(db).token_tree()?.syntax().text_range(); | 78 | let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); |
78 | let def_range = loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range(); | 79 | let def_start = |
80 | loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); | ||
79 | 81 | ||
80 | let macro_def = db.macro_def(loc.def)?; | 82 | let macro_def = db.macro_def(loc.def)?; |
81 | let shift = macro_def.0.shift(); | 83 | let shift = macro_def.0.shift(); |
82 | let rev_map = db.parse_macro(macro_file)?.1; | 84 | let exp_map = db.parse_macro(macro_file)?.1; |
85 | let macro_arg = db.macro_arg(macro_file.macro_call_id)?; | ||
83 | 86 | ||
84 | let arg_token_map = db.macro_arg(macro_file.macro_call_id)?.1; | 87 | let arg_start = (loc.ast_id.file_id, arg_start); |
85 | let def_token_map = macro_def.1; | 88 | let def_start = (loc.def.ast_id.file_id, def_start); |
86 | 89 | ||
87 | let arg_map = rev_map.map_ranges(&arg_token_map, arg_range, shift); | 90 | Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) |
88 | let def_map = rev_map.map_ranges(&def_token_map, def_range, 0); | ||
89 | |||
90 | let arg_file = loc.ast_id.file_id; | ||
91 | let def_file = loc.def.ast_id.file_id; | ||
92 | |||
93 | Some(ExpansionInfo { arg_file, def_file, arg_map, def_map }) | ||
94 | } | 91 | } |
95 | } | 92 | } |
96 | } | 93 | } |
@@ -143,28 +140,30 @@ impl MacroCallId { | |||
143 | #[derive(Debug, Clone, PartialEq, Eq)] | 140 | #[derive(Debug, Clone, PartialEq, Eq)] |
144 | /// ExpansionInfo mainly describes how to map text range between src and expanded macro | 141 | /// ExpansionInfo mainly describes how to map text range between src and expanded macro |
145 | pub struct ExpansionInfo { | 142 | pub struct ExpansionInfo { |
146 | pub(crate) arg_file: HirFileId, | 143 | pub(crate) arg_start: (HirFileId, TextUnit), |
147 | pub(crate) def_file: HirFileId, | 144 | pub(crate) def_start: (HirFileId, TextUnit), |
145 | pub(crate) shift: u32, | ||
148 | 146 | ||
149 | pub(crate) arg_map: Vec<(TextRange, TextRange)>, | 147 | pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>, |
150 | pub(crate) def_map: Vec<(TextRange, TextRange)>, | 148 | pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, |
149 | pub(crate) exp_map: Arc<mbe::RevTokenMap>, | ||
151 | } | 150 | } |
152 | 151 | ||
153 | impl ExpansionInfo { | 152 | impl ExpansionInfo { |
154 | pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { | 153 | pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { |
155 | for (src, dest) in &self.arg_map { | 154 | fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option<tt::TokenId> { |
156 | if src.is_subrange(&from) { | 155 | exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1) |
157 | return Some((self.arg_file, *dest)); | ||
158 | } | ||
159 | } | 156 | } |
160 | 157 | ||
161 | for (src, dest) in &self.def_map { | 158 | let token_id = look_in_rev_map(&self.exp_map, from)?; |
162 | if src.is_subrange(&from) { | 159 | let (token_map, file_offset, token_id) = if token_id.0 >= self.shift { |
163 | return Some((self.def_file, *dest)); | 160 | (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - self.shift).into()) |
164 | } | 161 | } else { |
165 | } | 162 | (&self.macro_def.1, self.def_start, token_id) |
163 | }; | ||
166 | 164 | ||
167 | None | 165 | let range = token_map.relative_range_of(token_id)?; |
166 | Some((file_offset.0, TextRange::offset_len(range.start() + file_offset.1, range.len()))) | ||
168 | } | 167 | } |
169 | } | 168 | } |
170 | 169 | ||