aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-09 04:00:46 +0000
committerEdwin Cheng <[email protected]>2019-11-09 04:00:46 +0000
commit0a5ec69404a2556dd82e5bb00b295aebaa291f04 (patch)
tree1ae5ae1ef0ca23dc917d845312c6753d9b46b1ab /crates
parentd01e0abdb57c5be340934cb51e2f6b1a2e6c0373 (diff)
Remove map_ranges in RevTokenMap
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_expand/src/db.rs12
-rw-r--r--crates/ra_hir_expand/src/lib.rs55
-rw-r--r--crates/ra_ide_api/src/display/navigation_target.rs2
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs32
4 files changed, 35 insertions, 66 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
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs
index b77e19231..1bf81e7d5 100644
--- a/crates/ra_ide_api/src/display/navigation_target.rs
+++ b/crates/ra_ide_api/src/display/navigation_target.rs
@@ -36,7 +36,7 @@ fn find_range_from_node(
36) -> (FileId, TextRange) { 36) -> (FileId, TextRange) {
37 let text_range = node.text_range(); 37 let text_range = node.text_range();
38 let (file_id, text_range) = src 38 let (file_id, text_range) = src
39 .parent_expansion(db) 39 .expansion_info(db)
40 .and_then(|expansion_info| expansion_info.find_range(text_range)) 40 .and_then(|expansion_info| expansion_info.find_range(text_range))
41 .unwrap_or((src, text_range)); 41 .unwrap_or((src, text_range));
42 42
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)]
25pub struct RevTokenMap { 25pub 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