aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir_def/src/find_path.rs44
-rw-r--r--crates/ra_hir_def/src/lib.rs1
-rw-r--r--crates/ra_hir_def/src/test_db.rs13
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
3use crate::{ModuleDefId, path::ModPath, ModuleId};
4
5pub fn find_path(item: ModuleDefId, from: ModuleId) -> ModPath {
6 todo!()
7}
8
9#[cfg(test)]
10mod 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
39struct 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;
37pub mod child_by_source; 37pub mod child_by_source;
38 38
39pub mod visibility; 39pub mod visibility;
40pub mod find_path;
40 41
41#[cfg(test)] 42#[cfg(test)]
42mod test_db; 43mod 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
8use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; 8use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
9use 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
56impl TestDB { 57impl 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();