aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-11-28 13:28:44 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-11-28 13:28:44 +0000
commit0cda188621b792265c957bdf4ac54af31cbc9947 (patch)
tree6f93cb6c8d04e2eb44154464b7e74ef549573a6f /crates/ra_hir/src/module
parent95c0c8f3986c8b3bcf0052d34d3ace09ebb9fa1b (diff)
parent555483d397db530d1d102d7828c7847a948baf6b (diff)
Merge #248
248: Hir tests r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/module')
-rw-r--r--crates/ra_hir/src/module/mod.rs8
-rw-r--r--crates/ra_hir/src/module/nameres.rs108
2 files changed, 105 insertions, 11 deletions
diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs
index a011fd53e..3ae83d8cb 100644
--- a/crates/ra_hir/src/module/mod.rs
+++ b/crates/ra_hir/src/module/mod.rs
@@ -26,8 +26,7 @@ pub use self::nameres::ModuleScope;
26pub struct Module { 26pub struct Module {
27 tree: Arc<ModuleTree>, 27 tree: Arc<ModuleTree>,
28 source_root_id: SourceRootId, 28 source_root_id: SourceRootId,
29 //TODO: make private 29 module_id: ModuleId,
30 pub module_id: ModuleId,
31} 30}
32 31
33impl Module { 32impl Module {
@@ -122,7 +121,6 @@ impl Module {
122 } 121 }
123 122
124 /// `name` is `None` for the crate's root module 123 /// `name` is `None` for the crate's root module
125 #[allow(unused)]
126 pub fn name(&self) -> Option<SmolStr> { 124 pub fn name(&self) -> Option<SmolStr> {
127 let link = self.module_id.parent_link(&self.tree)?; 125 let link = self.module_id.parent_link(&self.tree)?;
128 Some(link.name(&self.tree)) 126 Some(link.name(&self.tree))
@@ -218,7 +216,7 @@ pub enum ModuleSource {
218 216
219/// An owned syntax node for a module. Unlike `ModuleSource`, 217/// An owned syntax node for a module. Unlike `ModuleSource`,
220/// this holds onto the AST for the whole file. 218/// this holds onto the AST for the whole file.
221pub enum ModuleSourceNode { 219pub(crate) enum ModuleSourceNode {
222 SourceFile(ast::SourceFileNode), 220 SourceFile(ast::SourceFileNode),
223 Module(ast::ModuleNode), 221 Module(ast::ModuleNode),
224} 222}
@@ -338,7 +336,7 @@ impl ModuleSource {
338 } 336 }
339 } 337 }
340 338
341 pub fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { 339 pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
342 match self { 340 match self {
343 ModuleSource::SourceFile(file_id) => { 341 ModuleSource::SourceFile(file_id) => {
344 let syntax = db.source_file(file_id); 342 let syntax = db.source_file(file_id);
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
index 837a8d5ae..8529e16b3 100644
--- a/crates/ra_hir/src/module/nameres.rs
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -44,7 +44,7 @@ pub struct ItemMap {
44 44
45#[derive(Debug, Default, PartialEq, Eq, Clone)] 45#[derive(Debug, Default, PartialEq, Eq, Clone)]
46pub struct ModuleScope { 46pub struct ModuleScope {
47 pub items: FxHashMap<SmolStr, Resolution>, 47 items: FxHashMap<SmolStr, Resolution>,
48} 48}
49 49
50impl ModuleScope { 50impl ModuleScope {
@@ -200,11 +200,11 @@ impl ModuleItem {
200} 200}
201 201
202pub(crate) struct Resolver<'a, DB> { 202pub(crate) struct Resolver<'a, DB> {
203 pub db: &'a DB, 203 pub(crate) db: &'a DB,
204 pub input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>, 204 pub(crate) input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>,
205 pub source_root: SourceRootId, 205 pub(crate) source_root: SourceRootId,
206 pub module_tree: Arc<ModuleTree>, 206 pub(crate) module_tree: Arc<ModuleTree>,
207 pub result: ItemMap, 207 pub(crate) result: ItemMap,
208} 208}
209 209
210impl<'a, DB> Resolver<'a, DB> 210impl<'a, DB> Resolver<'a, DB>
@@ -336,3 +336,99 @@ where
336 f(module_items) 336 f(module_items)
337 } 337 }
338} 338}
339
340#[cfg(test)]
341mod tests {
342 use std::sync::Arc;
343
344 use salsa::Database;
345 use ra_db::FilesDatabase;
346 use ra_syntax::SmolStr;
347
348 use crate::{
349 self as hir,
350 db::HirDatabase,
351 mock::MockDatabase,
352};
353
354 fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
355 let (db, pos) = MockDatabase::with_position(fixture);
356 let source_root = db.file_source_root(pos.file_id);
357 let module = hir::Module::guess_from_position(&db, pos).unwrap().unwrap();
358 let module_id = module.module_id;
359 (db.item_map(source_root).unwrap(), module_id)
360 }
361
362 #[test]
363 fn test_item_map() {
364 let (item_map, module_id) = item_map(
365 "
366 //- /lib.rs
367 mod foo;
368
369 use crate::foo::bar::Baz;
370 <|>
371
372 //- /foo/mod.rs
373 pub mod bar;
374
375 //- /foo/bar.rs
376 pub struct Baz;
377 ",
378 );
379 let name = SmolStr::from("Baz");
380 let resolution = &item_map.per_module[&module_id].items[&name];
381 assert!(resolution.def_id.is_some());
382 }
383
384 #[test]
385 fn typing_inside_a_function_should_not_invalidate_item_map() {
386 let (mut db, pos) = MockDatabase::with_position(
387 "
388 //- /lib.rs
389 mod foo;<|>
390
391 use crate::foo::bar::Baz;
392
393 fn foo() -> i32 {
394 1 + 1
395 }
396 //- /foo/mod.rs
397 pub mod bar;
398
399 //- /foo/bar.rs
400 pub struct Baz;
401 ",
402 );
403 let source_root = db.file_source_root(pos.file_id);
404 {
405 let events = db.log_executed(|| {
406 db.item_map(source_root).unwrap();
407 });
408 assert!(format!("{:?}", events).contains("item_map"))
409 }
410
411 let new_text = "
412 mod foo;
413
414 use crate::foo::bar::Baz;
415
416 fn foo() -> i32 { 92 }
417 "
418 .to_string();
419
420 db.query_mut(ra_db::FileTextQuery)
421 .set(pos.file_id, Arc::new(new_text));
422
423 {
424 let events = db.log_executed(|| {
425 db.item_map(source_root).unwrap();
426 });
427 assert!(
428 !format!("{:?}", events).contains("_item_map"),
429 "{:#?}",
430 events
431 )
432 }
433 }
434}