1 //! Completes mod declarations.
3 use hir::{Module, ModuleSource};
4 use ide_db::base_db::{SourceDatabaseExt, VfsPath};
5 use ide_db::RootDatabase;
6 use rustc_hash::FxHashSet;
8 use crate::{CompletionItem, CompletionItemKind};
10 use crate::{context::CompletionContext, item::CompletionKind, Completions};
12 /// Complete mod declaration, i.e. `mod <|> ;`
13 pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
14 let mod_under_caret = match &ctx.mod_declaration_under_caret {
15 Some(mod_under_caret) if mod_under_caret.item_list().is_some() => return None,
16 Some(mod_under_caret) => mod_under_caret,
20 let _p = profile::span("completion::complete_mod");
22 let current_module = ctx.scope.module()?;
24 let module_definition_file =
25 current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
26 let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
27 let directory_to_look_for_submodules = directory_to_look_for_submodules(
30 source_root.path_for_file(&module_definition_file)?,
33 let existing_mod_declarations = current_module
35 .filter_map(|module| Some(module.name(ctx.db)?.to_string()))
36 .collect::<FxHashSet<_>>();
38 let module_declaration_file =
39 current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
40 module_declaration_source_file.file_id.original_file(ctx.db)
45 .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
46 .filter(|submodule_candidate_file| {
47 Some(submodule_candidate_file) != module_declaration_file.as_ref()
49 .filter_map(|submodule_file| {
50 let submodule_path = source_root.path_for_file(&submodule_file)?;
51 let directory_with_submodule = submodule_path.parent()?;
52 match submodule_path.name_and_extension()? {
53 ("lib", Some("rs")) | ("main", Some("rs")) => None,
54 ("mod", Some("rs")) => {
55 if directory_with_submodule.parent()? == directory_to_look_for_submodules {
56 match directory_with_submodule.name_and_extension()? {
57 (directory_name, None) => Some(directory_name.to_owned()),
64 (file_name, Some("rs"))
65 if directory_with_submodule == directory_to_look_for_submodules =>
67 Some(file_name.to_owned())
72 .filter(|name| !existing_mod_declarations.contains(name))
73 .for_each(|submodule_name| {
74 let mut label = submodule_name;
75 if mod_under_caret.semicolon_token().is_none() {
78 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
79 .kind(CompletionItemKind::Module)
86 fn directory_to_look_for_submodules(
89 module_file_path: &VfsPath,
90 ) -> Option<VfsPath> {
91 let directory_with_module_path = module_file_path.parent()?;
92 let base_directory = match module_file_path.name_and_extension()? {
93 ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
94 Some(directory_with_module_path)
96 (regular_rust_file_name, Some("rs")) => {
99 directory_with_module_path
102 .and_then(|path| path.name_and_extension()),
103 directory_with_module_path.name_and_extension(),
105 (Some(("src", None)), Some(("bin", None)))
107 // files in /src/bin/ can import each other directly
108 Some(directory_with_module_path)
110 directory_with_module_path.join(regular_rust_file_name)
116 let mut resulting_path = base_directory;
117 for module in module_chain_to_containing_module_file(module, db) {
118 if let Some(name) = module.name(db) {
119 resulting_path = resulting_path.join(&name.to_string())?;
126 fn module_chain_to_containing_module_file(
127 current_module: Module,
130 let mut path = Vec::new();
132 let mut current_module = Some(current_module);
133 while let Some(ModuleSource::Module(_)) =
134 current_module.map(|module| module.definition_source(db).value)
136 if let Some(module) = current_module {
137 path.insert(0, module);
138 current_module = module.parent(db);
140 current_module = None;
149 use crate::{test_utils::completion_list, CompletionKind};
150 use expect_test::{expect, Expect};
152 fn check(ra_fixture: &str, expect: Expect) {
153 let actual = completion_list(ra_fixture, CompletionKind::Magic);
154 expect.assert_eq(&actual);
158 fn lib_module_completion() {
165 //- /foo/ignored_foo.rs
169 //- /bar/ignored_bar.rs
180 fn no_module_completion_with_module_body() {
195 fn main_module_completion() {
202 //- /foo/ignored_foo.rs
206 //- /bar/ignored_bar.rs
217 fn main_test_module_completion() {
234 fn directly_nested_module_completion() {
243 //- /foo/bar/ignored_bar.rs
247 //- /foo/moar/ignored_moar.rs
258 fn nested_in_source_module_completion() {
276 // FIXME binary modules are not supported in tests properly
277 // Binary modules are a bit special, they allow importing the modules from `/src/bin`
278 // and that's why are good to test two things:
279 // * no cycles are allowed in mod declarations
280 // * no modules from the parent directory are proposed
281 // Unfortunately, binary modules support is in cargo not rustc,
282 // hence the test does not work now
285 // fn regular_bin_module_completion() {
290 // //- /src/bin/foo.rs
292 // //- /src/bin/bar.rs
294 // //- /src/bin/bar/bar_ignored.rs
295 // fn bar_ignored() {}
304 fn already_declared_bin_module_completion_omitted() {
307 //- /src/bin.rs crate:main
314 //- /src/bin/bar/bar_ignored.rs