diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/module_tree.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 36 |
2 files changed, 40 insertions, 2 deletions
diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs index d1dc3fa4b..893c375b5 100644 --- a/crates/ra_hir/src/module_tree.rs +++ b/crates/ra_hir/src/module_tree.rs | |||
@@ -172,6 +172,7 @@ impl ModuleTree { | |||
172 | file_id: HirFileId, | 172 | file_id: HirFileId, |
173 | decl_id: Option<SourceFileItemId>, | 173 | decl_id: Option<SourceFileItemId>, |
174 | ) -> ModuleId { | 174 | ) -> ModuleId { |
175 | let is_root = parent.is_none(); | ||
175 | let id = self.alloc_mod(ModuleData { | 176 | let id = self.alloc_mod(ModuleData { |
176 | file_id, | 177 | file_id, |
177 | decl_id, | 178 | decl_id, |
@@ -191,7 +192,7 @@ impl ModuleTree { | |||
191 | }); | 192 | }); |
192 | 193 | ||
193 | let (points_to, problem) = if sub.is_declaration { | 194 | let (points_to, problem) = if sub.is_declaration { |
194 | let (points_to, problem) = resolve_submodule(db, file_id, &sub.name); | 195 | let (points_to, problem) = resolve_submodule(db, file_id, &sub.name, is_root); |
195 | let points_to = points_to | 196 | let points_to = points_to |
196 | .into_iter() | 197 | .into_iter() |
197 | .map(|file_id| { | 198 | .map(|file_id| { |
@@ -295,6 +296,7 @@ fn resolve_submodule( | |||
295 | db: &impl HirDatabase, | 296 | db: &impl HirDatabase, |
296 | file_id: HirFileId, | 297 | file_id: HirFileId, |
297 | name: &Name, | 298 | name: &Name, |
299 | is_root: bool, | ||
298 | ) -> (Vec<FileId>, Option<Problem>) { | 300 | ) -> (Vec<FileId>, Option<Problem>) { |
299 | // FIXME: handle submodules of inline modules properly | 301 | // FIXME: handle submodules of inline modules properly |
300 | let file_id = file_id.original_file(db); | 302 | let file_id = file_id.original_file(db); |
@@ -303,7 +305,7 @@ fn resolve_submodule( | |||
303 | let root = RelativePathBuf::default(); | 305 | let root = RelativePathBuf::default(); |
304 | let dir_path = path.parent().unwrap_or(&root); | 306 | let dir_path = path.parent().unwrap_or(&root); |
305 | let mod_name = path.file_stem().unwrap_or("unknown"); | 307 | let mod_name = path.file_stem().unwrap_or("unknown"); |
306 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; | 308 | let is_dir_owner = is_root || mod_name == "mod"; |
307 | 309 | ||
308 | let file_mod = dir_path.join(format!("{}.rs", name)); | 310 | let file_mod = dir_path.join(format!("{}.rs", name)); |
309 | let dir_mod = dir_path.join(format!("{}/mod.rs", name)); | 311 | let dir_mod = dir_path.join(format!("{}/mod.rs", name)); |
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index e72781f51..3d420467c 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs | |||
@@ -19,6 +19,20 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) { | |||
19 | (db.item_map(krate.crate_id), module_id) | 19 | (db.item_map(krate.crate_id), module_id) |
20 | } | 20 | } |
21 | 21 | ||
22 | /// Sets the crate root to the file of the cursor marker | ||
23 | fn item_map_custom_crate_root(fixture: &str) -> (Arc<ItemMap>, ModuleId) { | ||
24 | let (mut db, pos) = MockDatabase::with_position(fixture); | ||
25 | |||
26 | let mut crate_graph = CrateGraph::default(); | ||
27 | crate_graph.add_crate_root(pos.file_id); | ||
28 | db.set_crate_graph(Arc::new(crate_graph)); | ||
29 | |||
30 | let module = crate::source_binder::module_from_position(&db, pos).unwrap(); | ||
31 | let krate = module.krate(&db).unwrap(); | ||
32 | let module_id = module.module_id; | ||
33 | (db.item_map(krate.crate_id), module_id) | ||
34 | } | ||
35 | |||
22 | fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { | 36 | fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { |
23 | let mut lines = map[module_id] | 37 | let mut lines = map[module_id] |
24 | .items | 38 | .items |
@@ -134,6 +148,28 @@ fn re_exports() { | |||
134 | } | 148 | } |
135 | 149 | ||
136 | #[test] | 150 | #[test] |
151 | fn module_resolution_works_for_non_standard_filenames() { | ||
152 | let (item_map, module_id) = item_map_custom_crate_root( | ||
153 | " | ||
154 | //- /my_library.rs | ||
155 | mod foo; | ||
156 | use self::foo::Bar; | ||
157 | <|> | ||
158 | //- /foo/mod.rs | ||
159 | pub struct Bar; | ||
160 | ", | ||
161 | ); | ||
162 | check_module_item_map( | ||
163 | &item_map, | ||
164 | module_id, | ||
165 | " | ||
166 | Bar: t v | ||
167 | foo: t | ||
168 | ", | ||
169 | ); | ||
170 | } | ||
171 | |||
172 | #[test] | ||
137 | fn name_res_works_for_broken_modules() { | 173 | fn name_res_works_for_broken_modules() { |
138 | covers!(name_res_works_for_broken_modules); | 174 | covers!(name_res_works_for_broken_modules); |
139 | let (item_map, module_id) = item_map( | 175 | let (item_map, module_id) = item_map( |