aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/module/nameres.rs89
-rw-r--r--crates/ra_hir/src/query_definitions.rs7
2 files changed, 49 insertions, 47 deletions
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
index b30dd3491..27a76a293 100644
--- a/crates/ra_hir/src/module/nameres.rs
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -25,13 +25,12 @@ use ra_syntax::{
25use ra_db::SourceRootId; 25use ra_db::SourceRootId;
26 26
27use crate::{ 27use crate::{
28 Cancelable, FileId, 28 Cancelable, MFileId, FileId,
29 DefId, DefLoc, DefKind, 29 DefId, DefLoc, DefKind,
30 SourceItemId, SourceFileItemId, SourceFileItems, 30 SourceItemId, SourceFileItemId, SourceFileItems,
31 Path, PathKind, 31 Path, PathKind,
32 HirDatabase, Crate, 32 HirDatabase, Crate,
33 Name, AsName, 33 Name, AsName,
34 macros::MacroCallLoc,
35 module::{Module, ModuleId, ModuleTree}, 34 module::{Module, ModuleId, ModuleTree},
36}; 35};
37 36
@@ -71,7 +70,7 @@ pub struct InputModuleItems {
71 70
72#[derive(Debug, PartialEq, Eq)] 71#[derive(Debug, PartialEq, Eq)]
73struct ModuleItem { 72struct ModuleItem {
74 id: SourceFileItemId, 73 id: SourceItemId,
75 name: Name, 74 name: Name,
76 kind: SyntaxKind, 75 kind: SyntaxKind,
77 vis: Vis, 76 vis: Vis,
@@ -210,24 +209,28 @@ impl<T> PerNs<T> {
210} 209}
211 210
212impl InputModuleItems { 211impl InputModuleItems {
213 pub(crate) fn new<'a>( 212 pub(crate) fn add_item(
213 &mut self,
214 mfile_id: MFileId,
214 file_items: &SourceFileItems, 215 file_items: &SourceFileItems,
215 items: impl Iterator<Item = ast::ModuleItem<'a>>, 216 item: ast::ModuleItem,
216 ) -> InputModuleItems { 217 ) -> Option<()> {
217 let mut res = InputModuleItems::default();
218 for item in items {
219 res.add_item(file_items, item);
220 }
221 res
222 }
223
224 fn add_item(&mut self, file_items: &SourceFileItems, item: ast::ModuleItem) -> Option<()> {
225 match item { 218 match item {
226 ast::ModuleItem::StructDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 219 ast::ModuleItem::StructDef(it) => {
227 ast::ModuleItem::EnumDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 220 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
228 ast::ModuleItem::FnDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 221 }
229 ast::ModuleItem::TraitDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 222 ast::ModuleItem::EnumDef(it) => {
230 ast::ModuleItem::TypeDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 223 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
224 }
225 ast::ModuleItem::FnDef(it) => {
226 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
227 }
228 ast::ModuleItem::TraitDef(it) => {
229 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
230 }
231 ast::ModuleItem::TypeDef(it) => {
232 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
233 }
231 ast::ModuleItem::ImplItem(_) => { 234 ast::ModuleItem::ImplItem(_) => {
232 // impls don't define items 235 // impls don't define items
233 } 236 }
@@ -235,9 +238,15 @@ impl InputModuleItems {
235 ast::ModuleItem::ExternCrateItem(_) => { 238 ast::ModuleItem::ExternCrateItem(_) => {
236 // TODO 239 // TODO
237 } 240 }
238 ast::ModuleItem::ConstDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 241 ast::ModuleItem::ConstDef(it) => {
239 ast::ModuleItem::StaticDef(it) => self.items.push(ModuleItem::new(file_items, it)?), 242 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
240 ast::ModuleItem::Module(it) => self.items.push(ModuleItem::new(file_items, it)?), 243 }
244 ast::ModuleItem::StaticDef(it) => {
245 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
246 }
247 ast::ModuleItem::Module(it) => {
248 self.items.push(ModuleItem::new(mfile_id, file_items, it)?)
249 }
241 } 250 }
242 Some(()) 251 Some(())
243 } 252 }
@@ -259,11 +268,16 @@ impl InputModuleItems {
259} 268}
260 269
261impl ModuleItem { 270impl ModuleItem {
262 fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> { 271 fn new<'a>(
272 mfile_id: MFileId,
273 file_items: &SourceFileItems,
274 item: impl ast::NameOwner<'a>,
275 ) -> Option<ModuleItem> {
263 let name = item.name()?.as_name(); 276 let name = item.name()?.as_name();
264 let kind = item.syntax().kind(); 277 let kind = item.syntax().kind();
265 let vis = Vis::Other; 278 let vis = Vis::Other;
266 let id = file_items.id_of_unchecked(item.syntax()); 279 let item_id = Some(file_items.id_of_unchecked(item.syntax()));
280 let id = SourceItemId { mfile_id, item_id };
267 let res = ModuleItem { 281 let res = ModuleItem {
268 id, 282 id,
269 name, 283 name,
@@ -303,7 +317,7 @@ where
303 317
304 pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> { 318 pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> {
305 for (&module_id, items) in self.input.iter() { 319 for (&module_id, items) in self.input.iter() {
306 self.populate_module(module_id, items)?; 320 self.populate_module(module_id, Arc::clone(items))?;
307 } 321 }
308 322
309 for &module_id in self.input.keys() { 323 for &module_id in self.input.keys() {
@@ -313,9 +327,11 @@ where
313 Ok(self.result) 327 Ok(self.result)
314 } 328 }
315 329
316 fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) -> Cancelable<()> { 330 fn populate_module(
317 let file_id = module_id.source(&self.module_tree).file_id(); 331 &mut self,
318 332 module_id: ModuleId,
333 input: Arc<InputModuleItems>,
334 ) -> Cancelable<()> {
319 let mut module_items = ModuleScope::default(); 335 let mut module_items = ModuleScope::default();
320 336
321 // Populate extern crates prelude 337 // Populate extern crates prelude
@@ -355,18 +371,6 @@ where
355 if item.kind == MODULE { 371 if item.kind == MODULE {
356 continue; 372 continue;
357 } 373 }
358 if item.kind == MACRO_CALL {
359 let loc = MacroCallLoc {
360 source_root_id: self.source_root,
361 module_id,
362 source_item_id: SourceItemId {
363 mfile_id: file_id.into(),
364 item_id: Some(item.id),
365 },
366 };
367 let id = loc.id(self.db);
368 continue;
369 }
370 // depending on the item kind, the location can define something in 374 // depending on the item kind, the location can define something in
371 // the values namespace, the types namespace, or both 375 // the values namespace, the types namespace, or both
372 let kind = DefKind::for_syntax_kind(item.kind); 376 let kind = DefKind::for_syntax_kind(item.kind);
@@ -375,10 +379,7 @@ where
375 kind: k, 379 kind: k,
376 source_root_id: self.source_root, 380 source_root_id: self.source_root,
377 module_id, 381 module_id,
378 source_item_id: SourceItemId { 382 source_item_id: item.id,
379 mfile_id: file_id.into(),
380 item_id: Some(item.id),
381 },
382 }; 383 };
383 def_loc.id(self.db) 384 def_loc.id(self.db)
384 }); 385 });
diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs
index 0c07f1444..3c90e29fe 100644
--- a/crates/ra_hir/src/query_definitions.rs
+++ b/crates/ra_hir/src/query_definitions.rs
@@ -128,11 +128,12 @@ pub(super) fn input_module_items(
128) -> Cancelable<Arc<InputModuleItems>> { 128) -> Cancelable<Arc<InputModuleItems>> {
129 let module_tree = db.module_tree(source_root)?; 129 let module_tree = db.module_tree(source_root)?;
130 let source = module_id.source(&module_tree); 130 let source = module_id.source(&module_tree);
131 let file_items = db.file_items(source.file_id().into()); 131 let mfile_id = source.file_id().into();
132 let file_items = db.file_items(mfile_id);
132 let res = match source.resolve(db) { 133 let res = match source.resolve(db) {
133 ModuleSourceNode::SourceFile(it) => { 134 ModuleSourceNode::SourceFile(it) => {
134 let items = it.borrowed().items(); 135 let items = it.borrowed().items();
135 InputModuleItems::new(&file_items, items) 136 InputModuleItems::new(mfile_id, &file_items, items)
136 } 137 }
137 ModuleSourceNode::Module(it) => { 138 ModuleSourceNode::Module(it) => {
138 let items = it 139 let items = it
@@ -140,7 +141,7 @@ pub(super) fn input_module_items(
140 .item_list() 141 .item_list()
141 .into_iter() 142 .into_iter()
142 .flat_map(|it| it.items()); 143 .flat_map(|it| it.items());
143 InputModuleItems::new(&file_items, items) 144 InputModuleItems::new(mfile_id, &file_items, items)
144 } 145 }
145 }; 146 };
146 Ok(Arc::new(res)) 147 Ok(Arc::new(res))