2020-09-07 19:21:39 +03:00
|
|
|
//! Completes mod declarations.
|
|
|
|
|
2020-09-07 20:52:37 +03:00
|
|
|
use base_db::{SourceDatabaseExt, VfsPath};
|
|
|
|
use hir::{Module, ModuleSource};
|
|
|
|
use ide_db::RootDatabase;
|
2020-09-08 00:00:00 +03:00
|
|
|
use rustc_hash::FxHashSet;
|
2020-09-07 19:21:39 +03:00
|
|
|
|
|
|
|
use super::{completion_context::CompletionContext, completion_item::Completions};
|
|
|
|
|
|
|
|
/// Complete mod declaration, i.e. `mod <|> ;`
|
2020-09-07 21:39:23 +03:00
|
|
|
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
|
|
|
let current_module = ctx.scope.module()?;
|
2020-09-07 20:52:37 +03:00
|
|
|
|
2020-09-08 00:00:00 +03:00
|
|
|
let module_definition_file =
|
2020-09-07 21:39:23 +03:00
|
|
|
current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
|
2020-09-08 00:00:00 +03:00
|
|
|
let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
|
2020-09-07 21:39:23 +03:00
|
|
|
let directory_to_look_for_submodules = directory_to_look_for_submodules(
|
|
|
|
current_module,
|
|
|
|
ctx.db,
|
2020-09-08 00:00:00 +03:00
|
|
|
source_root.path_for_file(&module_definition_file)?,
|
2020-09-07 21:39:23 +03:00
|
|
|
)?;
|
2020-09-07 20:52:37 +03:00
|
|
|
|
2020-09-08 00:00:00 +03:00
|
|
|
let existing_mod_declarations = current_module
|
|
|
|
.children(ctx.db)
|
|
|
|
.filter_map(|module| Some(module.name(ctx.db)?.to_string()))
|
|
|
|
.collect::<FxHashSet<_>>();
|
|
|
|
|
|
|
|
let module_declaration_file =
|
|
|
|
current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
|
|
|
|
module_declaration_source_file.file_id.original_file(ctx.db)
|
|
|
|
});
|
|
|
|
|
2020-09-07 21:39:23 +03:00
|
|
|
let mod_declaration_candidates = source_root
|
|
|
|
.iter()
|
2020-09-08 00:00:00 +03:00
|
|
|
.filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
|
|
|
|
.filter(|submodule_candidate_file| {
|
|
|
|
Some(submodule_candidate_file) != module_declaration_file.as_ref()
|
|
|
|
})
|
2020-09-07 21:39:23 +03:00
|
|
|
.filter_map(|submodule_file| {
|
|
|
|
let submodule_path = source_root.path_for_file(&submodule_file)?;
|
2020-09-08 00:00:00 +03:00
|
|
|
if !is_special_rust_file_path(&submodule_path)
|
|
|
|
&& submodule_path.parent()? == directory_to_look_for_submodules
|
|
|
|
{
|
2020-09-07 21:39:23 +03:00
|
|
|
submodule_path.file_name_and_extension()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-09-08 00:00:00 +03:00
|
|
|
.filter_map(|submodule_file_name_and_extension| match submodule_file_name_and_extension {
|
|
|
|
(file_name, Some("rs")) => Some(file_name.to_owned()),
|
|
|
|
(subdirectory_name, None) => {
|
|
|
|
let mod_rs_path =
|
|
|
|
directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?;
|
|
|
|
if source_root.file_for_path(&mod_rs_path).is_some() {
|
|
|
|
Some(subdirectory_name.to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
2020-09-07 21:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 00:00:00 +03:00
|
|
|
_ => None,
|
2020-09-07 19:21:39 +03:00
|
|
|
})
|
2020-09-08 00:00:00 +03:00
|
|
|
.filter(|name| !existing_mod_declarations.contains(name))
|
2020-09-07 21:39:23 +03:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
dbg!(mod_declaration_candidates);
|
2020-09-07 20:52:37 +03:00
|
|
|
|
2020-09-07 21:39:23 +03:00
|
|
|
// TODO kb exlude existing children from the candidates
|
2020-09-07 20:52:37 +03:00
|
|
|
|
2020-09-07 21:39:23 +03:00
|
|
|
Some(())
|
2020-09-07 20:52:37 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 00:00:00 +03:00
|
|
|
fn is_special_rust_file_path(path: &VfsPath) -> bool {
|
|
|
|
matches!(
|
|
|
|
path.file_name_and_extension(),
|
|
|
|
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs")))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-07 21:39:23 +03:00
|
|
|
fn directory_to_look_for_submodules(
|
|
|
|
module: Module,
|
|
|
|
db: &RootDatabase,
|
|
|
|
module_file_path: &VfsPath,
|
|
|
|
) -> Option<VfsPath> {
|
2020-09-07 20:52:37 +03:00
|
|
|
let module_directory_path = module_file_path.parent()?;
|
2020-09-08 00:00:00 +03:00
|
|
|
let base_directory = if is_special_rust_file_path(module_file_path) {
|
|
|
|
Some(module_directory_path)
|
|
|
|
} else if let (regular_rust_file_name, Some("rs")) =
|
|
|
|
module_file_path.file_name_and_extension()?
|
|
|
|
{
|
|
|
|
if matches!(
|
|
|
|
(
|
|
|
|
module_directory_path
|
|
|
|
.parent()
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|path| path.file_name_and_extension()),
|
|
|
|
module_directory_path.file_name_and_extension(),
|
|
|
|
),
|
|
|
|
(Some(("src", None)), Some(("bin", None)))
|
|
|
|
) {
|
|
|
|
// files in /src/bin/ can import each other directly
|
2020-09-07 20:52:37 +03:00
|
|
|
Some(module_directory_path)
|
2020-09-08 00:00:00 +03:00
|
|
|
} else {
|
|
|
|
module_directory_path.join(regular_rust_file_name)
|
2020-09-07 20:52:37 +03:00
|
|
|
}
|
2020-09-08 00:00:00 +03:00
|
|
|
} else {
|
|
|
|
None
|
2020-09-07 21:39:23 +03:00
|
|
|
}?;
|
|
|
|
|
|
|
|
let mut resulting_path = base_directory;
|
|
|
|
for module in module_chain_to_containing_module_file(module, db) {
|
|
|
|
if let Some(name) = module.name(db) {
|
|
|
|
resulting_path = resulting_path.join(&name.to_string())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(resulting_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn module_chain_to_containing_module_file(
|
|
|
|
current_module: Module,
|
|
|
|
db: &RootDatabase,
|
|
|
|
) -> Vec<Module> {
|
|
|
|
let mut path = Vec::new();
|
|
|
|
|
|
|
|
let mut current_module = Some(current_module);
|
|
|
|
while let Some(ModuleSource::Module(_)) =
|
|
|
|
current_module.map(|module| module.definition_source(db).value)
|
|
|
|
{
|
|
|
|
if let Some(module) = current_module {
|
|
|
|
path.insert(0, module);
|
|
|
|
current_module = module.parent(db);
|
|
|
|
} else {
|
|
|
|
current_module = None;
|
|
|
|
}
|
2020-09-07 20:52:37 +03:00
|
|
|
}
|
2020-09-07 21:39:23 +03:00
|
|
|
|
|
|
|
path
|
2020-09-07 20:52:37 +03:00
|
|
|
}
|