aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/data.rs
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-12-20 19:37:03 +0000
committerEdwin Cheng <[email protected]>2019-12-20 19:37:03 +0000
commitad81d1dbc19803b5ccf1b230237642944edbff13 (patch)
tree5af29382643b659b69523e65354e862d7e71087d /crates/ra_hir_def/src/data.rs
parentcfc50ff160d0af2ce5cd931c6d41161abfdb2fbd (diff)
Add support macros in impl blocks
Diffstat (limited to 'crates/ra_hir_def/src/data.rs')
-rw-r--r--crates/ra_hir_def/src/data.rs119
1 files changed, 80 insertions, 39 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 14e86936b..b0a3f1784 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -4,16 +4,16 @@ use std::sync::Arc;
4 4
5use hir_expand::{ 5use hir_expand::{
6 name::{name, AsName, Name}, 6 name::{name, AsName, Name},
7 AstId, 7 AstId, InFile,
8}; 8};
9use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 9use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner};
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase, 12 db::DefDatabase,
13 src::HasSource, 13 src::HasSource,
14 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
15 AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, ImplId, Intern, 15 AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule,
16 Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 16 ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
17}; 17};
18 18
19#[derive(Debug, Clone, PartialEq, Eq)] 19#[derive(Debug, Clone, PartialEq, Eq)]
@@ -167,46 +167,24 @@ pub struct ImplData {
167 167
168impl ImplData { 168impl ImplData {
169 pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> { 169 pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> {
170 let src = id.lookup(db).source(db); 170 let impl_loc = id.lookup(db);
171 let items = db.ast_id_map(src.file_id); 171 let src = impl_loc.source(db);
172 172
173 let target_trait = src.value.target_trait().map(TypeRef::from_ast); 173 let target_trait = src.value.target_trait().map(TypeRef::from_ast);
174 let target_type = TypeRef::from_ast_opt(src.value.target_type()); 174 let target_type = TypeRef::from_ast_opt(src.value.target_type());
175 let is_negative = src.value.is_negative(); 175 let is_negative = src.value.is_negative();
176 let module_id = impl_loc.container.module(db);
176 177
177 let items = if let Some(item_list) = src.value.item_list() { 178 let mut items = Vec::new();
178 item_list 179 if let Some(item_list) = src.value.item_list() {
179 .impl_items() 180 items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id));
180 .map(|item_node| match item_node { 181 items.extend(collect_impl_items_in_macros(
181 ast::ImplItem::FnDef(it) => { 182 db,
182 let def = FunctionLoc { 183 module_id,
183 container: AssocContainerId::ImplId(id), 184 &src.with_value(item_list),
184 ast_id: AstId::new(src.file_id, items.ast_id(&it)), 185 id,
185 } 186 ));
186 .intern(db); 187 }
187 def.into()
188 }
189 ast::ImplItem::ConstDef(it) => {
190 let def = ConstLoc {
191 container: AssocContainerId::ImplId(id),
192 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
193 }
194 .intern(db);
195 def.into()
196 }
197 ast::ImplItem::TypeAliasDef(it) => {
198 let def = TypeAliasLoc {
199 container: AssocContainerId::ImplId(id),
200 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
201 }
202 .intern(db);
203 def.into()
204 }
205 })
206 .collect()
207 } else {
208 Vec::new()
209 };
210 188
211 let res = ImplData { target_trait, target_type, items, is_negative }; 189 let res = ImplData { target_trait, target_type, items, is_negative };
212 Arc::new(res) 190 Arc::new(res)
@@ -237,3 +215,66 @@ impl ConstData {
237 ConstData { name, type_ref } 215 ConstData { name, type_ref }
238 } 216 }
239} 217}
218
219fn collect_impl_items_in_macros(
220 db: &impl DefDatabase,
221 module_id: ModuleId,
222 impl_block: &InFile<ast::ItemList>,
223 id: ImplId,
224) -> Vec<AssocItemId> {
225 let mut expander = Expander::new(db, impl_block.file_id, module_id);
226 let mut res = Vec::new();
227
228 for m in impl_block.value.syntax().children().filter_map(ast::MacroCall::cast) {
229 if let Some((mark, items)) = expander.enter_expand(db, m) {
230 let items: InFile<ast::MacroItems> = expander.to_source(items);
231 expander.exit(db, mark);
232 res.extend(collect_impl_items(
233 db,
234 items.value.items().filter_map(|it| ImplItem::cast(it.syntax().clone())),
235 items.file_id,
236 id,
237 ));
238 }
239 }
240
241 res
242}
243
244fn collect_impl_items(
245 db: &impl DefDatabase,
246 impl_items: impl Iterator<Item = ImplItem>,
247 file_id: crate::HirFileId,
248 id: ImplId,
249) -> Vec<AssocItemId> {
250 let items = db.ast_id_map(file_id);
251
252 impl_items
253 .map(|item_node| match item_node {
254 ast::ImplItem::FnDef(it) => {
255 let def = FunctionLoc {
256 container: AssocContainerId::ImplId(id),
257 ast_id: AstId::new(file_id, items.ast_id(&it)),
258 }
259 .intern(db);
260 def.into()
261 }
262 ast::ImplItem::ConstDef(it) => {
263 let def = ConstLoc {
264 container: AssocContainerId::ImplId(id),
265 ast_id: AstId::new(file_id, items.ast_id(&it)),
266 }
267 .intern(db);
268 def.into()
269 }
270 ast::ImplItem::TypeAliasDef(it) => {
271 let def = TypeAliasLoc {
272 container: AssocContainerId::ImplId(id),
273 ast_id: AstId::new(file_id, items.ast_id(&it)),
274 }
275 .intern(db);
276 def.into()
277 }
278 })
279 .collect()
280}