1 //! Completes mod declarations.
5 use hir::{Module, ModuleSource};
7 base_db::{SourceDatabaseExt, VfsPath},
8 RootDatabase, SymbolKind,
10 use rustc_hash::FxHashSet;
12 use crate::{patterns::ImmediateLocation, CompletionItem};
14 use crate::{context::CompletionContext, item::CompletionKind, Completions};
16 /// Complete mod declaration, i.e. `mod $0 ;`
17 pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
18 let mod_under_caret = match &ctx.completion_location {
19 Some(ImmediateLocation::ModDeclaration(mod_under_caret)) => mod_under_caret,
23 let _p = profile::span("completion::complete_mod");
25 let current_module = ctx.scope.module()?;
27 let module_definition_file =
28 current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
29 let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
30 let directory_to_look_for_submodules = directory_to_look_for_submodules(
33 source_root.path_for_file(&module_definition_file)?,
36 let existing_mod_declarations = current_module
38 .filter_map(|module| Some(module.name(ctx.db)?.to_string()))
39 .collect::<FxHashSet<_>>();
41 let module_declaration_file =
42 current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
43 module_declaration_source_file.file_id.original_file(ctx.db)
48 .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
49 .filter(|submodule_candidate_file| {
50 Some(submodule_candidate_file) != module_declaration_file.as_ref()
52 .filter_map(|submodule_file| {
53 let submodule_path = source_root.path_for_file(&submodule_file)?;
54 let directory_with_submodule = submodule_path.parent()?;
55 let (name, ext) = submodule_path.name_and_extension()?;
56 if ext != Some("rs") {
60 "lib" | "main" => None,
62 if directory_with_submodule.parent()? == directory_to_look_for_submodules {
63 match directory_with_submodule.name_and_extension()? {
64 (directory_name, None) => Some(directory_name.to_owned()),
71 file_name if directory_with_submodule == directory_to_look_for_submodules => {
72 Some(file_name.to_owned())
77 .filter(|name| !existing_mod_declarations.contains(name))
78 .for_each(|submodule_name| {
79 let mut label = submodule_name;
80 if mod_under_caret.semicolon_token().is_none() {
83 let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label);
84 item.kind(SymbolKind::Module);
91 fn directory_to_look_for_submodules(
94 module_file_path: &VfsPath,
95 ) -> Option<VfsPath> {
96 let directory_with_module_path = module_file_path.parent()?;
97 let (name, ext) = module_file_path.name_and_extension()?;
98 if ext != Some("rs") {
101 let base_directory = match name {
102 "mod" | "lib" | "main" => Some(directory_with_module_path),
103 regular_rust_file_name => {
106 directory_with_module_path
109 .and_then(|path| path.name_and_extension()),
110 directory_with_module_path.name_and_extension(),
112 (Some(("src", None)), Some(("bin", None)))
114 // files in /src/bin/ can import each other directly
115 Some(directory_with_module_path)
117 directory_with_module_path.join(regular_rust_file_name)
122 module_chain_to_containing_module_file(module, db)
124 .filter_map(|module| module.name(db))
125 .try_fold(base_directory, |path, name| path.join(&name.to_string()))
128 fn module_chain_to_containing_module_file(
129 current_module: Module,
133 iter::successors(Some(current_module), |current_module| current_module.parent(db))
134 .take_while(|current_module| {
135 matches!(current_module.definition_source(db).value, ModuleSource::Module(_))
137 .collect::<Vec<_>>();
144 use crate::{test_utils::completion_list, CompletionKind};
145 use expect_test::{expect, Expect};
147 fn check(ra_fixture: &str, expect: Expect) {
148 let actual = completion_list(ra_fixture, CompletionKind::Magic);
149 expect.assert_eq(&actual);
153 fn lib_module_completion() {
160 //- /foo/ignored_foo.rs
164 //- /bar/ignored_bar.rs
175 fn no_module_completion_with_module_body() {
190 fn main_module_completion() {
197 //- /foo/ignored_foo.rs
201 //- /bar/ignored_bar.rs
212 fn main_test_module_completion() {
229 fn directly_nested_module_completion() {
238 //- /foo/bar/ignored_bar.rs
242 //- /foo/moar/ignored_moar.rs
253 fn nested_in_source_module_completion() {
271 // FIXME binary modules are not supported in tests properly
272 // Binary modules are a bit special, they allow importing the modules from `/src/bin`
273 // and that's why are good to test two things:
274 // * no cycles are allowed in mod declarations
275 // * no modules from the parent directory are proposed
276 // Unfortunately, binary modules support is in cargo not rustc,
277 // hence the test does not work now
280 // fn regular_bin_module_completion() {
285 // //- /src/bin/foo.rs
287 // //- /src/bin/bar.rs
289 // //- /src/bin/bar/bar_ignored.rs
290 // fn bar_ignored() {}
299 fn already_declared_bin_module_completion_omitted() {
302 //- /src/bin.rs crate:main
309 //- /src/bin/bar/bar_ignored.rs