diff options
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index dd07a16b4..151d1d785 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, | 20 | SyntaxNode, TextRange, TextUnit, |
20 | }; | 21 | }; |
21 | 22 | ||
22 | use crate::ast_id_map::FileAstId; | 23 | use crate::ast_id_map::FileAstId; |
@@ -66,6 +67,30 @@ impl HirFileId { | |||
66 | } | 67 | } |
67 | } | 68 | } |
68 | } | 69 | } |
70 | |||
71 | /// Return expansion information if it is a macro-expansion file | ||
72 | pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> { | ||
73 | match self.0 { | ||
74 | HirFileIdRepr::FileId(_) => None, | ||
75 | HirFileIdRepr::MacroFile(macro_file) => { | ||
76 | let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); | ||
77 | |||
78 | let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); | ||
79 | let def_start = | ||
80 | loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); | ||
81 | |||
82 | let macro_def = db.macro_def(loc.def)?; | ||
83 | let shift = macro_def.0.shift(); | ||
84 | let exp_map = db.parse_macro(macro_file)?.1; | ||
85 | let macro_arg = db.macro_arg(macro_file.macro_call_id)?; | ||
86 | |||
87 | let arg_start = (loc.ast_id.file_id, arg_start); | ||
88 | let def_start = (loc.def.ast_id.file_id, def_start); | ||
89 | |||
90 | Some(ExpansionInfo { arg_start, def_start, macro_arg, macro_def, exp_map, shift }) | ||
91 | } | ||
92 | } | ||
93 | } | ||
69 | } | 94 | } |
70 | 95 | ||
71 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 96 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -112,6 +137,38 @@ impl MacroCallId { | |||
112 | } | 137 | } |
113 | } | 138 | } |
114 | 139 | ||
140 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
141 | /// ExpansionInfo mainly describes how to map text range between src and expanded macro | ||
142 | pub struct ExpansionInfo { | ||
143 | pub(crate) arg_start: (HirFileId, TextUnit), | ||
144 | pub(crate) def_start: (HirFileId, TextUnit), | ||
145 | pub(crate) shift: u32, | ||
146 | |||
147 | pub(crate) macro_def: Arc<(mbe::MacroRules, mbe::TokenMap)>, | ||
148 | pub(crate) macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, | ||
149 | pub(crate) exp_map: Arc<mbe::RevTokenMap>, | ||
150 | } | ||
151 | |||
152 | impl ExpansionInfo { | ||
153 | pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> { | ||
154 | let token_id = look_in_rev_map(&self.exp_map, from)?; | ||
155 | |||
156 | let (token_map, (file_id, start_offset), token_id) = if token_id.0 >= self.shift { | ||
157 | (&self.macro_arg.1, self.arg_start, tt::TokenId(token_id.0 - self.shift).into()) | ||
158 | } else { | ||
159 | (&self.macro_def.1, self.def_start, token_id) | ||
160 | }; | ||
161 | |||
162 | let range = token_map.relative_range_of(token_id)?; | ||
163 | |||
164 | return Some((file_id, range + start_offset)); | ||
165 | |||
166 | fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option<tt::TokenId> { | ||
167 | exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1) | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | |||
115 | /// `AstId` points to an AST node in any file. | 172 | /// `AstId` points to an AST node in any file. |
116 | /// | 173 | /// |
117 | /// It is stable across reparses, and can be used as salsa key/value. | 174 | /// It is stable across reparses, and can be used as salsa key/value. |