aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/module/nameres.rs98
-rw-r--r--crates/ra_hir/src/module/nameres/tests.rs94
2 files changed, 95 insertions, 97 deletions
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
index 768836b65..5e4bb7669 100644
--- a/crates/ra_hir/src/module/nameres.rs
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -312,7 +312,6 @@ where
312 let mut segments = import.path.segments.iter().enumerate(); 312 let mut segments = import.path.segments.iter().enumerate();
313 313
314 let mut curr = match import.path.kind { 314 let mut curr = match import.path.kind {
315 // TODO: handle extern crates
316 PathKind::Plain => { 315 PathKind::Plain => {
317 let root_id = module_id.crate_root(&self.module_tree); 316 let root_id = module_id.crate_root(&self.module_tree);
318 let file_id = root_id.source(&self.module_tree).file_id(); 317 let file_id = root_id.source(&self.module_tree).file_id();
@@ -389,99 +388,4 @@ where
389} 388}
390 389
391#[cfg(test)] 390#[cfg(test)]
392mod tests { 391mod tests;
393 use std::sync::Arc;
394
395 use salsa::Database;
396 use ra_db::FilesDatabase;
397 use ra_syntax::SmolStr;
398
399 use crate::{
400 self as hir,
401 db::HirDatabase,
402 mock::MockDatabase,
403};
404
405 fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
406 let (db, pos) = MockDatabase::with_position(fixture);
407 let source_root = db.file_source_root(pos.file_id);
408 let module = hir::source_binder::module_from_position(&db, pos)
409 .unwrap()
410 .unwrap();
411 let module_id = module.module_id;
412 (db.item_map(source_root).unwrap(), module_id)
413 }
414
415 #[test]
416 fn test_item_map() {
417 let (item_map, module_id) = item_map(
418 "
419 //- /lib.rs
420 mod foo;
421
422 use crate::foo::bar::Baz;
423 <|>
424
425 //- /foo/mod.rs
426 pub mod bar;
427
428 //- /foo/bar.rs
429 pub struct Baz;
430 ",
431 );
432 let name = SmolStr::from("Baz");
433 let resolution = &item_map.per_module[&module_id].items[&name];
434 assert!(resolution.def_id.is_some());
435 }
436
437 #[test]
438 fn typing_inside_a_function_should_not_invalidate_item_map() {
439 let (mut db, pos) = MockDatabase::with_position(
440 "
441 //- /lib.rs
442 mod foo;<|>
443
444 use crate::foo::bar::Baz;
445
446 fn foo() -> i32 {
447 1 + 1
448 }
449 //- /foo/mod.rs
450 pub mod bar;
451
452 //- /foo/bar.rs
453 pub struct Baz;
454 ",
455 );
456 let source_root = db.file_source_root(pos.file_id);
457 {
458 let events = db.log_executed(|| {
459 db.item_map(source_root).unwrap();
460 });
461 assert!(format!("{:?}", events).contains("item_map"))
462 }
463
464 let new_text = "
465 mod foo;
466
467 use crate::foo::bar::Baz;
468
469 fn foo() -> i32 { 92 }
470 "
471 .to_string();
472
473 db.query_mut(ra_db::FileTextQuery)
474 .set(pos.file_id, Arc::new(new_text));
475
476 {
477 let events = db.log_executed(|| {
478 db.item_map(source_root).unwrap();
479 });
480 assert!(
481 !format!("{:?}", events).contains("_item_map"),
482 "{:#?}",
483 events
484 )
485 }
486 }
487}
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs
new file mode 100644
index 000000000..060683e27
--- /dev/null
+++ b/crates/ra_hir/src/module/nameres/tests.rs
@@ -0,0 +1,94 @@
1use std::sync::Arc;
2
3use salsa::Database;
4use ra_db::FilesDatabase;
5use ra_syntax::SmolStr;
6
7use crate::{
8 self as hir,
9 db::HirDatabase,
10 mock::MockDatabase,
11};
12
13fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
14 let (db, pos) = MockDatabase::with_position(fixture);
15 let source_root = db.file_source_root(pos.file_id);
16 let module = hir::source_binder::module_from_position(&db, pos)
17 .unwrap()
18 .unwrap();
19 let module_id = module.module_id;
20 (db.item_map(source_root).unwrap(), module_id)
21}
22
23#[test]
24fn test_item_map() {
25 let (item_map, module_id) = item_map(
26 "
27 //- /lib.rs
28 mod foo;
29
30 use crate::foo::bar::Baz;
31 <|>
32
33 //- /foo/mod.rs
34 pub mod bar;
35
36 //- /foo/bar.rs
37 pub struct Baz;
38 ",
39 );
40 let name = SmolStr::from("Baz");
41 let resolution = &item_map.per_module[&module_id].items[&name];
42 assert!(resolution.def_id.is_some());
43}
44
45#[test]
46fn typing_inside_a_function_should_not_invalidate_item_map() {
47 let (mut db, pos) = MockDatabase::with_position(
48 "
49 //- /lib.rs
50 mod foo;<|>
51
52 use crate::foo::bar::Baz;
53
54 fn foo() -> i32 {
55 1 + 1
56 }
57 //- /foo/mod.rs
58 pub mod bar;
59
60 //- /foo/bar.rs
61 pub struct Baz;
62 ",
63 );
64 let source_root = db.file_source_root(pos.file_id);
65 {
66 let events = db.log_executed(|| {
67 db.item_map(source_root).unwrap();
68 });
69 assert!(format!("{:?}", events).contains("item_map"))
70 }
71
72 let new_text = "
73 mod foo;
74
75 use crate::foo::bar::Baz;
76
77 fn foo() -> i32 { 92 }
78 "
79 .to_string();
80
81 db.query_mut(ra_db::FileTextQuery)
82 .set(pos.file_id, Arc::new(new_text));
83
84 {
85 let events = db.log_executed(|| {
86 db.item_map(source_root).unwrap();
87 });
88 assert!(
89 !format!("{:?}", events).contains("_item_map"),
90 "{:#?}",
91 events
92 )
93 }
94}