diff options
author | Florian Diebold <[email protected]> | 2019-12-30 13:25:19 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2020-01-11 22:33:04 +0000 |
commit | 22b412f1a9d245cc3d3774dab9408e3a39a52025 (patch) | |
tree | 9651434730d78f05ecceb541f5eb28e99543a084 /crates | |
parent | 877fda04c5a37241b09f155847d7e27b20875b63 (diff) |
find_path WIP
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/src/find_path.rs | 44 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir_def/src/test_db.rs | 13 |
3 files changed, 58 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs new file mode 100644 index 000000000..1ddf5fca6 --- /dev/null +++ b/crates/ra_hir_def/src/find_path.rs | |||
@@ -0,0 +1,44 @@ | |||
1 | //! An algorithm to find a path to refer to a certain item. | ||
2 | |||
3 | use crate::{ModuleDefId, path::ModPath, ModuleId}; | ||
4 | |||
5 | pub fn find_path(item: ModuleDefId, from: ModuleId) -> ModPath { | ||
6 | todo!() | ||
7 | } | ||
8 | |||
9 | #[cfg(test)] | ||
10 | mod tests { | ||
11 | use super::*; | ||
12 | use ra_db::{fixture::WithFixture, SourceDatabase}; | ||
13 | use crate::{db::DefDatabase, test_db::TestDB}; | ||
14 | use ra_syntax::ast::AstNode; | ||
15 | use hir_expand::hygiene::Hygiene; | ||
16 | |||
17 | /// `code` needs to contain a cursor marker; checks that `find_path` for the | ||
18 | /// item the `path` refers to returns that same path when called from the | ||
19 | /// module the cursor is in. | ||
20 | fn check_found_path(code: &str, path: &str) { | ||
21 | let (db, pos) = TestDB::with_position(code); | ||
22 | let module = db.module_for_file(pos.file_id); | ||
23 | let parsed_path_file = ra_syntax::SourceFile::parse(&format!("use {};", path)); | ||
24 | let ast_path = parsed_path_file.syntax_node().descendants().find_map(ra_syntax::ast::Path::cast).unwrap(); | ||
25 | let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap(); | ||
26 | |||
27 | let crate_def_map = db.crate_def_map(module.krate); | ||
28 | let resolved = crate_def_map.resolve_path(&db, module.local_id, &mod_path, crate::item_scope::BuiltinShadowMode::Module).0.take_types().unwrap(); | ||
29 | |||
30 | let found_path = find_path(resolved, module); | ||
31 | |||
32 | assert_eq!(mod_path, found_path); | ||
33 | } | ||
34 | |||
35 | #[test] | ||
36 | fn same_module() { | ||
37 | let code = r#" | ||
38 | //- /main.rs | ||
39 | struct S; | ||
40 | <|> | ||
41 | "#; | ||
42 | check_found_path(code, "S"); | ||
43 | } | ||
44 | } | ||
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 61f044ecf..ebc12e891 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -37,6 +37,7 @@ pub mod src; | |||
37 | pub mod child_by_source; | 37 | pub mod child_by_source; |
38 | 38 | ||
39 | pub mod visibility; | 39 | pub mod visibility; |
40 | pub mod find_path; | ||
40 | 41 | ||
41 | #[cfg(test)] | 42 | #[cfg(test)] |
42 | mod test_db; | 43 | mod test_db; |
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs index 54e3a84bd..a403f183f 100644 --- a/crates/ra_hir_def/src/test_db.rs +++ b/crates/ra_hir_def/src/test_db.rs | |||
@@ -6,6 +6,7 @@ use std::{ | |||
6 | }; | 6 | }; |
7 | 7 | ||
8 | use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; | 8 | use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; |
9 | use crate::db::DefDatabase; | ||
9 | 10 | ||
10 | #[salsa::database( | 11 | #[salsa::database( |
11 | ra_db::SourceDatabaseExtStorage, | 12 | ra_db::SourceDatabaseExtStorage, |
@@ -54,6 +55,18 @@ impl FileLoader for TestDB { | |||
54 | } | 55 | } |
55 | 56 | ||
56 | impl TestDB { | 57 | impl TestDB { |
58 | pub fn module_for_file(&self, file_id: FileId) -> crate::ModuleId { | ||
59 | for &krate in self.relevant_crates(file_id).iter() { | ||
60 | let crate_def_map = self.crate_def_map(krate); | ||
61 | for (local_id, data) in crate_def_map.modules.iter() { | ||
62 | if data.origin.file_id() == Some(file_id) { | ||
63 | return crate::ModuleId { krate, local_id }; | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | panic!("Can't find module for file") | ||
68 | } | ||
69 | |||
57 | pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> { | 70 | pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<TestDB>> { |
58 | *self.events.lock().unwrap() = Some(Vec::new()); | 71 | *self.events.lock().unwrap() = Some(Vec::new()); |
59 | f(); | 72 | f(); |