aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-18 19:53:40 +0000
committerSeivan Heidari <[email protected]>2019-11-18 19:53:40 +0000
commit4bdb6351ac557851607df9d142c9e573c0fb5e1d (patch)
tree48b349958afceeeebecccd63e55004d5a924baff /crates/ra_hir_expand
parentaceeb0b85ee8228503f970ea602af71ff22216a0 (diff)
parenta4f21801c54c65eafa337edc5e86de2c46b37544 (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r--crates/ra_hir_expand/src/db.rs8
-rw-r--r--crates/ra_hir_expand/src/lib.rs55
2 files changed, 27 insertions, 36 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index 9de7c1ea8..3c11c8a22 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -59,10 +59,8 @@ pub trait AstDatabase: SourceDatabase {
59 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; 59 fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId;
60 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>; 60 fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
61 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>; 61 fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
62 fn parse_macro( 62 fn parse_macro(&self, macro_file: MacroFile)
63 &self, 63 -> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>;
64 macro_file: MacroFile,
65 ) -> Option<(Parse<SyntaxNode>, Arc<mbe::RevTokenMap>)>;
66 fn macro_expand(&self, macro_call: MacroCallId) -> Result<Arc<tt::Subtree>, String>; 64 fn macro_expand(&self, macro_call: MacroCallId) -> Result<Arc<tt::Subtree>, String>;
67} 65}
68 66
@@ -136,7 +134,7 @@ pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Optio
136pub(crate) fn parse_macro( 134pub(crate) fn parse_macro(
137 db: &dyn AstDatabase, 135 db: &dyn AstDatabase,
138 macro_file: MacroFile, 136 macro_file: MacroFile,
139) -> Option<(Parse<SyntaxNode>, Arc<mbe::RevTokenMap>)> { 137) -> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
140 let _p = profile("parse_macro_query"); 138 let _p = profile("parse_macro_query");
141 139
142 let macro_call_id = macro_file.macro_call_id; 140 let macro_call_id = macro_file.macro_call_id;
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 57e2e6cb1..cfe7e6d15 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -20,7 +20,7 @@ use ra_db::{salsa, CrateId, FileId};
20use ra_syntax::{ 20use ra_syntax::{
21 algo, 21 algo,
22 ast::{self, AstNode}, 22 ast::{self, AstNode},
23 SyntaxNode, SyntaxToken, TextRange, TextUnit, 23 SyntaxNode, SyntaxToken, TextUnit,
24}; 24};
25 25
26use crate::ast_id_map::FileAstId; 26use crate::ast_id_map::FileAstId;
@@ -79,22 +79,17 @@ impl HirFileId {
79 HirFileIdRepr::MacroFile(macro_file) => { 79 HirFileIdRepr::MacroFile(macro_file) => {
80 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); 80 let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
81 81
82 let arg_start = loc.ast_id.to_node(db).token_tree()?.syntax().text_range().start(); 82 let arg_tt = loc.ast_id.to_node(db).token_tree()?;
83 let def_start = 83 let def_tt = loc.def.ast_id.to_node(db).token_tree()?;
84 loc.def.ast_id.to_node(db).token_tree()?.syntax().text_range().start();
85 84
86 let macro_def = db.macro_def(loc.def)?; 85 let macro_def = db.macro_def(loc.def)?;
87 let (parse, exp_map) = db.parse_macro(macro_file)?; 86 let (parse, exp_map) = db.parse_macro(macro_file)?;
88 let expanded = Source::new(self, parse.syntax_node());
89 let macro_arg = db.macro_arg(macro_file.macro_call_id)?; 87 let macro_arg = db.macro_arg(macro_file.macro_call_id)?;
90 88
91 let arg_start = (loc.ast_id.file_id, arg_start);
92 let def_start = (loc.def.ast_id.file_id, def_start);
93
94 Some(ExpansionInfo { 89 Some(ExpansionInfo {
95 expanded, 90 expanded: Source::new(self, parse.syntax_node()),
96 arg_start, 91 arg: Source::new(loc.ast_id.file_id, arg_tt),
97 def_start, 92 def: Source::new(loc.ast_id.file_id, def_tt),
98 macro_arg, 93 macro_arg,
99 macro_def, 94 macro_def,
100 exp_map, 95 exp_map,
@@ -159,18 +154,19 @@ impl MacroCallId {
159#[derive(Debug, Clone, PartialEq, Eq)] 154#[derive(Debug, Clone, PartialEq, Eq)]
160pub struct ExpansionInfo { 155pub struct ExpansionInfo {
161 expanded: Source<SyntaxNode>, 156 expanded: Source<SyntaxNode>,
162 arg_start: (HirFileId, TextUnit), 157 arg: Source<ast::TokenTree>,
163 def_start: (HirFileId, TextUnit), 158 def: Source<ast::TokenTree>,
164 159
165 macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, 160 macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>,
166 macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, 161 macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
167 exp_map: Arc<mbe::RevTokenMap>, 162 exp_map: Arc<mbe::TokenMap>,
168} 163}
169 164
170impl ExpansionInfo { 165impl ExpansionInfo {
171 pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { 166 pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> {
172 assert_eq!(token.file_id, self.arg_start.0); 167 assert_eq!(token.file_id, self.arg.file_id);
173 let range = token.ast.text_range().checked_sub(self.arg_start.1)?; 168 let range =
169 token.ast.text_range().checked_sub(self.arg.ast.syntax().text_range().start())?;
174 let token_id = self.macro_arg.1.token_by_range(range)?; 170 let token_id = self.macro_arg.1.token_by_range(range)?;
175 let token_id = self.macro_def.0.map_id_down(token_id); 171 let token_id = self.macro_def.0.map_id_down(token_id);
176 172
@@ -181,25 +177,22 @@ impl ExpansionInfo {
181 Some(self.expanded.with_ast(token)) 177 Some(self.expanded.with_ast(token))
182 } 178 }
183 179
184 // FIXME: a more correct signature would be 180 pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> {
185 // `pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>>` 181 let token_id = self.exp_map.token_by_range(token.ast.text_range())?;
186 pub fn find_range(&self, from: TextRange) -> Option<(HirFileId, TextRange)> {
187 let token_id = look_in_rev_map(&self.exp_map, from)?;
188 182
189 let (token_id, origin) = self.macro_def.0.map_id_up(token_id); 183 let (token_id, origin) = self.macro_def.0.map_id_up(token_id);
190 184 let (token_map, tt) = match origin {
191 let (token_map, (file_id, start_offset)) = match origin { 185 mbe::Origin::Call => (&self.macro_arg.1, &self.arg),
192 mbe::Origin::Call => (&self.macro_arg.1, self.arg_start), 186 mbe::Origin::Def => (&self.macro_def.1, &self.def),
193 mbe::Origin::Def => (&self.macro_def.1, self.def_start),
194 }; 187 };
195 188
196 let range = token_map.relative_range_of(token_id)?; 189 let range = token_map.range_by_token(token_id)?;
197 190 let token = algo::find_covering_element(
198 return Some((file_id, range + start_offset)); 191 tt.ast.syntax(),
199 192 range + tt.ast.syntax().text_range().start(),
200 fn look_in_rev_map(exp_map: &mbe::RevTokenMap, from: TextRange) -> Option<tt::TokenId> { 193 )
201 exp_map.ranges.iter().find(|&it| it.0.is_subrange(&from)).map(|it| it.1) 194 .into_token()?;
202 } 195 Some(tt.with_ast(token))
203 } 196 }
204} 197}
205 198