diff options
Diffstat (limited to 'crates/hir_def/src/nameres/tests.rs')
-rw-r--r-- | crates/hir_def/src/nameres/tests.rs | 75 |
1 files changed, 9 insertions, 66 deletions
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index b36d0b59b..bd3e2701b 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs | |||
@@ -4,16 +4,16 @@ mod macros; | |||
4 | mod mod_resolution; | 4 | mod mod_resolution; |
5 | mod diagnostics; | 5 | mod diagnostics; |
6 | mod primitives; | 6 | mod primitives; |
7 | mod block; | ||
8 | 7 | ||
9 | use std::sync::Arc; | 8 | use std::sync::Arc; |
10 | 9 | ||
11 | use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; | 10 | use base_db::{fixture::WithFixture, SourceDatabase}; |
12 | use expect_test::{expect, Expect}; | 11 | use expect_test::{expect, Expect}; |
13 | use syntax::AstNode; | ||
14 | use test_utils::mark; | 12 | use test_utils::mark; |
15 | 13 | ||
16 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB, Lookup}; | 14 | use crate::{db::DefDatabase, test_db::TestDB}; |
15 | |||
16 | use super::DefMap; | ||
17 | 17 | ||
18 | fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { | 18 | fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { |
19 | let db = TestDB::with_files(ra_fixture); | 19 | let db = TestDB::with_files(ra_fixture); |
@@ -21,71 +21,14 @@ fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { | |||
21 | db.crate_def_map(krate) | 21 | db.crate_def_map(krate) |
22 | } | 22 | } |
23 | 23 | ||
24 | fn compute_block_def_map(ra_fixture: &str) -> Arc<DefMap> { | 24 | fn render_crate_def_map(ra_fixture: &str) -> String { |
25 | let (db, position) = TestDB::with_position(ra_fixture); | 25 | let db = TestDB::with_files(ra_fixture); |
26 | 26 | let krate = db.crate_graph().iter().next().unwrap(); | |
27 | // FIXME: perhaps we should make this use body lowering tests instead? | 27 | db.crate_def_map(krate).dump(&db) |
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 | |||
40 | fn 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 | } | 28 | } |
79 | 29 | ||
80 | fn check(ra_fixture: &str, expect: Expect) { | 30 | fn check(ra_fixture: &str, expect: Expect) { |
81 | let def_map = compute_crate_def_map(ra_fixture); | 31 | let actual = render_crate_def_map(ra_fixture); |
82 | let actual = def_map.dump(); | ||
83 | expect.assert_eq(&actual); | ||
84 | } | ||
85 | |||
86 | fn 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); | 32 | expect.assert_eq(&actual); |
90 | } | 33 | } |
91 | 34 | ||