1 //! Completes mod declarations.
5 use hir::{Module, ModuleSource};
7 base_db::{SourceDatabaseExt, VfsPath},
8 FxHashSet, RootDatabase, SymbolKind,
10 use syntax::{ast, AstNode, SyntaxKind};
12 use crate::{context::CompletionContext, CompletionItem, Completions};
14 /// Complete mod declaration, i.e. `mod $0;`
15 pub(crate) fn complete_mod(
16 acc: &mut Completions,
17 ctx: &CompletionContext<'_>,
18 mod_under_caret: &ast::Module,
20 if mod_under_caret.item_list().is_some() {
24 let _p = profile::span("completion::complete_mod");
26 let mut current_module = ctx.module;
27 // For `mod $0`, `ctx.module` is its parent, but for `mod f$0`, it's `mod f` itself, but we're
28 // interested in its parent.
29 if ctx.original_token.kind() == SyntaxKind::IDENT {
31 ctx.original_token.parent_ancestors().nth(1).and_then(ast::Module::cast)
33 match ctx.sema.to_def(&module) {
34 Some(module) if module == current_module => {
35 if let Some(parent) = current_module.parent(ctx.db) {
36 current_module = parent;
44 let module_definition_file =
45 current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
46 let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
47 let directory_to_look_for_submodules = directory_to_look_for_submodules(
50 source_root.path_for_file(&module_definition_file)?,
53 let existing_mod_declarations = current_module
55 .filter_map(|module| Some(module.name(ctx.db)?.to_string()))
56 .filter(|module| module != ctx.original_token.text())
57 .collect::<FxHashSet<_>>();
59 let module_declaration_file =
60 current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
61 module_declaration_source_file.file_id.original_file(ctx.db)
66 .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
67 .filter(|submodule_candidate_file| {
68 Some(submodule_candidate_file) != module_declaration_file.as_ref()
70 .filter_map(|submodule_file| {
71 let submodule_path = source_root.path_for_file(&submodule_file)?;
72 let directory_with_submodule = submodule_path.parent()?;
73 let (name, ext) = submodule_path.name_and_extension()?;
74 if ext != Some("rs") {
78 "lib" | "main" => None,
80 if directory_with_submodule.parent()? == directory_to_look_for_submodules {
81 match directory_with_submodule.name_and_extension()? {
82 (directory_name, None) => Some(directory_name.to_owned()),
89 file_name if directory_with_submodule == directory_to_look_for_submodules => {
90 Some(file_name.to_owned())
95 .filter(|name| !existing_mod_declarations.contains(name))
96 .for_each(|submodule_name| {
97 let mut label = submodule_name;
98 if mod_under_caret.semicolon_token().is_none() {
101 let item = CompletionItem::new(SymbolKind::Module, ctx.source_range(), &label);
108 fn directory_to_look_for_submodules(
111 module_file_path: &VfsPath,
112 ) -> Option<VfsPath> {
113 let directory_with_module_path = module_file_path.parent()?;
114 let (name, ext) = module_file_path.name_and_extension()?;
115 if ext != Some("rs") {
118 let base_directory = match name {
119 "mod" | "lib" | "main" => Some(directory_with_module_path),
120 regular_rust_file_name => {
123 directory_with_module_path
126 .and_then(|path| path.name_and_extension()),
127 directory_with_module_path.name_and_extension(),
129 (Some(("src", None)), Some(("bin", None)))
131 // files in /src/bin/ can import each other directly
132 Some(directory_with_module_path)
134 directory_with_module_path.join(regular_rust_file_name)
139 module_chain_to_containing_module_file(module, db)
141 .filter_map(|module| module.name(db))
142 .try_fold(base_directory, |path, name| path.join(&name.to_smol_str()))
145 fn module_chain_to_containing_module_file(
146 current_module: Module,
150 iter::successors(Some(current_module), |current_module| current_module.parent(db))
151 .take_while(|current_module| {
152 matches!(current_module.definition_source(db).value, ModuleSource::Module(_))
154 .collect::<Vec<_>>();
161 use expect_test::{expect, Expect};
163 use crate::tests::completion_list;
165 fn check(ra_fixture: &str, expect: Expect) {
166 let actual = completion_list(ra_fixture);
167 expect.assert_eq(&actual);
171 fn lib_module_completion() {
178 //- /foo/ignored_foo.rs
182 //- /bar/ignored_bar.rs
193 fn no_module_completion_with_module_body() {
208 fn main_module_completion() {
215 //- /foo/ignored_foo.rs
219 //- /bar/ignored_bar.rs
230 fn main_test_module_completion() {
247 fn directly_nested_module_completion() {
256 //- /foo/bar/ignored_bar.rs
260 //- /foo/moar/ignored_moar.rs
271 fn nested_in_source_module_completion() {
289 // FIXME binary modules are not supported in tests properly
290 // Binary modules are a bit special, they allow importing the modules from `/src/bin`
291 // and that's why are good to test two things:
292 // * no cycles are allowed in mod declarations
293 // * no modules from the parent directory are proposed
294 // Unfortunately, binary modules support is in cargo not rustc,
295 // hence the test does not work now
298 // fn regular_bin_module_completion() {
303 // //- /src/bin/foo.rs
305 // //- /src/bin/bar.rs
307 // //- /src/bin/bar/bar_ignored.rs
308 // fn bar_ignored() {}
317 fn already_declared_bin_module_completion_omitted() {
320 //- /src/bin.rs crate:main
327 //- /src/bin/bar/bar_ignored.rs
335 fn name_partially_typed() {
342 //- /foo/ignored_foo.rs
346 //- /bar/ignored_bar.rs
357 fn semi_colon_completion() {