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 .collect::<FxHashSet<_>>();
58 let module_declaration_file =
59 current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
60 module_declaration_source_file.file_id.original_file(ctx.db)
65 .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
66 .filter(|submodule_candidate_file| {
67 Some(submodule_candidate_file) != module_declaration_file.as_ref()
69 .filter_map(|submodule_file| {
70 let submodule_path = source_root.path_for_file(&submodule_file)?;
71 let directory_with_submodule = submodule_path.parent()?;
72 let (name, ext) = submodule_path.name_and_extension()?;
73 if ext != Some("rs") {
77 "lib" | "main" => None,
79 if directory_with_submodule.parent()? == directory_to_look_for_submodules {
80 match directory_with_submodule.name_and_extension()? {
81 (directory_name, None) => Some(directory_name.to_owned()),
88 file_name if directory_with_submodule == directory_to_look_for_submodules => {
89 Some(file_name.to_owned())
94 .filter(|name| !existing_mod_declarations.contains(name))
95 .for_each(|submodule_name| {
96 let mut label = submodule_name;
97 if mod_under_caret.semicolon_token().is_none() {
100 let item = CompletionItem::new(SymbolKind::Module, ctx.source_range(), &label);
107 fn directory_to_look_for_submodules(
110 module_file_path: &VfsPath,
111 ) -> Option<VfsPath> {
112 let directory_with_module_path = module_file_path.parent()?;
113 let (name, ext) = module_file_path.name_and_extension()?;
114 if ext != Some("rs") {
117 let base_directory = match name {
118 "mod" | "lib" | "main" => Some(directory_with_module_path),
119 regular_rust_file_name => {
122 directory_with_module_path
125 .and_then(|path| path.name_and_extension()),
126 directory_with_module_path.name_and_extension(),
128 (Some(("src", None)), Some(("bin", None)))
130 // files in /src/bin/ can import each other directly
131 Some(directory_with_module_path)
133 directory_with_module_path.join(regular_rust_file_name)
138 module_chain_to_containing_module_file(module, db)
140 .filter_map(|module| module.name(db))
141 .try_fold(base_directory, |path, name| path.join(&name.to_smol_str()))
144 fn module_chain_to_containing_module_file(
145 current_module: Module,
149 iter::successors(Some(current_module), |current_module| current_module.parent(db))
150 .take_while(|current_module| {
151 matches!(current_module.definition_source(db).value, ModuleSource::Module(_))
153 .collect::<Vec<_>>();
160 use expect_test::{expect, Expect};
162 use crate::tests::completion_list;
164 fn check(ra_fixture: &str, expect: Expect) {
165 let actual = completion_list(ra_fixture);
166 expect.assert_eq(&actual);
170 fn lib_module_completion() {
177 //- /foo/ignored_foo.rs
181 //- /bar/ignored_bar.rs
192 fn no_module_completion_with_module_body() {
207 fn main_module_completion() {
214 //- /foo/ignored_foo.rs
218 //- /bar/ignored_bar.rs
229 fn main_test_module_completion() {
246 fn directly_nested_module_completion() {
255 //- /foo/bar/ignored_bar.rs
259 //- /foo/moar/ignored_moar.rs
270 fn nested_in_source_module_completion() {
288 // FIXME binary modules are not supported in tests properly
289 // Binary modules are a bit special, they allow importing the modules from `/src/bin`
290 // and that's why are good to test two things:
291 // * no cycles are allowed in mod declarations
292 // * no modules from the parent directory are proposed
293 // Unfortunately, binary modules support is in cargo not rustc,
294 // hence the test does not work now
297 // fn regular_bin_module_completion() {
302 // //- /src/bin/foo.rs
304 // //- /src/bin/bar.rs
306 // //- /src/bin/bar/bar_ignored.rs
307 // fn bar_ignored() {}
316 fn already_declared_bin_module_completion_omitted() {
319 //- /src/bin.rs crate:main
326 //- /src/bin/bar/bar_ignored.rs
334 fn name_partially_typed() {
341 //- /foo/ignored_foo.rs
345 //- /bar/ignored_bar.rs