aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/nameres/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/nameres/tests.rs')
-rw-r--r--crates/hir_def/src/nameres/tests.rs68
1 files changed, 2 insertions, 66 deletions
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs
index b36d0b59b..723481c36 100644
--- a/crates/hir_def/src/nameres/tests.rs
+++ b/crates/hir_def/src/nameres/tests.rs
@@ -4,16 +4,14 @@ mod macros;
4mod mod_resolution; 4mod mod_resolution;
5mod diagnostics; 5mod diagnostics;
6mod primitives; 6mod primitives;
7mod block;
8 7
9use std::sync::Arc; 8use std::sync::Arc;
10 9
11use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; 10use base_db::{fixture::WithFixture, SourceDatabase};
12use expect_test::{expect, Expect}; 11use expect_test::{expect, Expect};
13use syntax::AstNode;
14use test_utils::mark; 12use test_utils::mark;
15 13
16use crate::{db::DefDatabase, nameres::*, test_db::TestDB, Lookup}; 14use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
17 15
18fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { 16fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
19 let db = TestDB::with_files(ra_fixture); 17 let db = TestDB::with_files(ra_fixture);
@@ -21,74 +19,12 @@ fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
21 db.crate_def_map(krate) 19 db.crate_def_map(krate)
22} 20}
23 21
24fn compute_block_def_map(ra_fixture: &str) -> Arc<DefMap> {
25 let (db, position) = TestDB::with_position(ra_fixture);
26
27 // FIXME: perhaps we should make this use body lowering tests instead?
28
29 let module = db.module_for_file(position.file_id);
30 let mut def_map = db.crate_def_map(module.krate);
31 while let Some(new_def_map) = descend_def_map_at_position(&db, position, def_map.clone()) {
32 def_map = new_def_map;
33 }
34
35 // FIXME: select the right module, not the root
36
37 def_map
38}
39
40fn descend_def_map_at_position(
41 db: &dyn DefDatabase,
42 position: FilePosition,
43 def_map: Arc<DefMap>,
44) -> Option<Arc<DefMap>> {
45 for (local_id, module_data) in def_map.modules() {
46 let mod_def = module_data.origin.definition_source(db);
47 let ast_map = db.ast_id_map(mod_def.file_id);
48 let item_tree = db.item_tree(mod_def.file_id);
49 let root = db.parse_or_expand(mod_def.file_id).unwrap();
50 for item in module_data.scope.declarations() {
51 match item {
52 ModuleDefId::FunctionId(it) => {
53 // Technically blocks can be inside any type (due to arrays and const generics),
54 // and also in const/static initializers. For tests we only really care about
55 // functions though.
56
57 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
58
59 if ast.syntax().text_range().contains(position.offset) {
60 // Cursor inside function, descend into its body's DefMap.
61 // Note that we don't handle block *expressions* inside function bodies.
62 let ast_map = db.ast_id_map(position.file_id.into());
63 let ast_id = ast_map.ast_id(&ast.body().unwrap());
64 let block = BlockLoc {
65 ast_id: InFile::new(position.file_id.into(), ast_id),
66 module: def_map.module_id(local_id),
67 };
68 let block_id = db.intern_block(block);
69 return Some(db.block_def_map(block_id));
70 }
71 }
72 _ => continue,
73 }
74 }
75 }
76
77 None
78}
79
80fn check(ra_fixture: &str, expect: Expect) { 22fn check(ra_fixture: &str, expect: Expect) {
81 let def_map = compute_crate_def_map(ra_fixture); 23 let def_map = compute_crate_def_map(ra_fixture);
82 let actual = def_map.dump(); 24 let actual = def_map.dump();
83 expect.assert_eq(&actual); 25 expect.assert_eq(&actual);
84} 26}
85 27
86fn check_at(ra_fixture: &str, expect: Expect) {
87 let def_map = compute_block_def_map(ra_fixture);
88 let actual = def_map.dump();
89 expect.assert_eq(&actual);
90}
91
92#[test] 28#[test]
93fn crate_def_map_smoke_test() { 29fn crate_def_map_smoke_test() {
94 check( 30 check(