aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r--crates/ra_hir_expand/src/db.rs12
-rw-r--r--crates/ra_hir_expand/src/lib.rs55
2 files changed, 33 insertions, 34 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index e6d2e8d9d..b789c6e7b 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -22,8 +22,8 @@ pub trait AstDatabase: SourceDatabase {
22 22
23 #[salsa::interned] 23 #[salsa::interned]
24 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; 24 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
25 fn macro_arg(&self, id: MacroCallId) -> Option<(Arc<tt::Subtree>, Arc<mbe::TokenMap>)>; 25 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
26 fn macro_def(&self, id: MacroDefId) -> Option<(Arc<mbe::MacroRules>, Arc<mbe::TokenMap>)>; 26 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>>;
27 fn parse_macro( 27 fn parse_macro(
28 &self, 28 &self,
29 macro_file: MacroFile, 29 macro_file: MacroFile,
@@ -40,7 +40,7 @@ pub(crate) fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdM
40pub(crate) fn macro_def( 40pub(crate) fn macro_def(
41 db: &dyn AstDatabase, 41 db: &dyn AstDatabase,
42 id: MacroDefId, 42 id: MacroDefId,
43) -> Option<(Arc<mbe::MacroRules>, Arc<mbe::TokenMap>)> { 43) -> Option<Arc<(mbe::MacroRules, mbe::TokenMap)>> {
44 let macro_call = id.ast_id.to_node(db); 44 let macro_call = id.ast_id.to_node(db);
45 let arg = macro_call.token_tree()?; 45 let arg = macro_call.token_tree()?;
46 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { 46 let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
@@ -51,18 +51,18 @@ pub(crate) fn macro_def(
51 log::warn!("fail on macro_def parse: {:#?}", tt); 51 log::warn!("fail on macro_def parse: {:#?}", tt);
52 None 52 None
53 })?; 53 })?;
54 Some((Arc::new(rules), Arc::new(tmap))) 54 Some(Arc::new((rules, tmap)))
55} 55}
56 56
57pub(crate) fn macro_arg( 57pub(crate) fn macro_arg(
58 db: &dyn AstDatabase, 58 db: &dyn AstDatabase,
59 id: MacroCallId, 59 id: MacroCallId,
60) -> Option<(Arc<tt::Subtree>, Arc<mbe::TokenMap>)> { 60) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
61 let loc = db.lookup_intern_macro(id); 61 let loc = db.lookup_intern_macro(id);
62 let macro_call = loc.ast_id.to_node(db); 62 let macro_call = loc.ast_id.to_node(db);
63 let arg = macro_call.token_tree()?; 63 let arg = macro_call.token_tree()?;
64 let (tt, tmap) = mbe::ast_to_token_tree(&arg)?; 64 let (tt, tmap) = mbe::ast_to_token_tree(&arg)?;
65 Some((Arc::new(tt), Arc::new(tmap))) 65 Some(Arc::new((tt, tmap)))
66} 66}
67 67
68pub(crate) fn macro_expand( 68pub(crate) fn macro_expand(
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;
12pub mod diagnostics; 12pub mod diagnostics;
13 13
14use std::hash::{Hash, Hasher}; 14use std::hash::{Hash, Hasher};
15use std::sync::Arc;
15 16
16use ra_db::{salsa, CrateId, FileId}; 17use ra_db::{salsa, CrateId, FileId};
17use ra_syntax::{ 18use ra_syntax::{
18 ast::{self, AstNode}, 19 ast::{self, AstNode},
19 SyntaxNode, TextRange, 20 SyntaxNode, TextRange, TextUnit,
20}; 21};
21 22
22use crate::ast_id_map::FileAstId; 23use 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
145pub struct ExpansionInfo { 142pub 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
153impl ExpansionInfo { 152impl 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