aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/find_path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/find_path.rs')
-rw-r--r--crates/ra_hir_def/src/find_path.rs44
1 files changed, 44 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}