From 6ba479cd058aa54a9f161085c7ff9ac1f12d8df3 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 7 Sep 2020 19:21:39 +0300 Subject: Finally cretae the mod completion module --- crates/base_db/src/lib.rs | 2 +- crates/ide/src/completion.rs | 2 ++ crates/ide/src/completion/complete_mod.rs | 39 +++++++++++++++++++++++++ crates/ide/src/completion/completion_context.rs | 18 +----------- 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 crates/ide/src/completion/complete_mod.rs diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs index 9733e1fd3..321007d33 100644 --- a/crates/base_db/src/lib.rs +++ b/crates/base_db/src/lib.rs @@ -199,7 +199,7 @@ fn possible_sudmobule_names(module_files: &FileSet, module_file: FileId) -> Vec< }) .filter_map(|file_name_and_extension| { match file_name_and_extension { - // TODO kb wrong resolution for nested non-file modules (mod tests {mod <|>) + // TODO kb wrong resolution for nested non-file modules (mod tests { mod <|> }) // TODO kb in src/bin when a module is included into another, // the included file gets "moved" into a directory below and now cannot add any other modules ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None, diff --git a/crates/ide/src/completion.rs b/crates/ide/src/completion.rs index 33bed6991..daea2aa95 100644 --- a/crates/ide/src/completion.rs +++ b/crates/ide/src/completion.rs @@ -19,6 +19,7 @@ mod complete_unqualified_path; mod complete_postfix; mod complete_macro_in_item_position; mod complete_trait_impl; +mod complete_mod; use ide_db::RootDatabase; @@ -124,6 +125,7 @@ pub(crate) fn completions( complete_postfix::complete_postfix(&mut acc, &ctx); complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); complete_trait_impl::complete_trait_impl(&mut acc, &ctx); + complete_mod::complete_mod(&mut acc, &ctx); Some(acc) } diff --git a/crates/ide/src/completion/complete_mod.rs b/crates/ide/src/completion/complete_mod.rs new file mode 100644 index 000000000..4c1e79603 --- /dev/null +++ b/crates/ide/src/completion/complete_mod.rs @@ -0,0 +1,39 @@ +//! Completes mod declarations. + +use base_db::FileLoader; +use hir::ModuleSource; + +use super::{completion_context::CompletionContext, completion_item::Completions}; + +/// Complete mod declaration, i.e. `mod <|> ;` +pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) { + let module_names_for_import = ctx + .sema + // TODO kb this is wrong, since we need not the file module + .to_module_def(ctx.position.file_id) + .and_then(|current_module| { + dbg!(current_module.name(ctx.db)); + dbg!(current_module.definition_source(ctx.db)); + dbg!(current_module.declaration_source(ctx.db)); + let mut zz = Vec::new(); + let mut vv = Some(current_module); + while let Some(ModuleSource::Module(_)) = + vv.map(|vv| vv.definition_source(ctx.db).value) + { + zz.push(current_module.name(ctx.db)); + vv = current_module.parent(ctx.db); + } + dbg!(zz); + let definition_source = current_module.definition_source(ctx.db); + // TODO kb filter out declarations in possible_sudmobule_names + // let declaration_source = current_module.declaration_source(ctx.db); + let module_definition_source_file = definition_source.file_id.original_file(ctx.db); + let mod_declaration_candidates = + ctx.db.possible_sudmobule_names(module_definition_source_file); + dbg!(mod_declaration_candidates); + // TODO kb exlude existing children from the candidates + let existing_children = current_module.children(ctx.db).collect::>(); + None::> + }) + .unwrap_or_default(); +} diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs index a8fe44083..31886942a 100644 --- a/crates/ide/src/completion/completion_context.rs +++ b/crates/ide/src/completion/completion_context.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use base_db::{FileLoader, SourceDatabase}; -use hir::{Semantics, SemanticsScope, Type}; +use hir::{ModuleSource, Semantics, SemanticsScope, Type}; use ide_db::RootDatabase; use syntax::{ algo::{find_covering_element, find_node_at_offset}, @@ -112,22 +112,6 @@ impl<'a> CompletionContext<'a> { }; let fake_ident_token = file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap(); - { - let module_names_for_import = sema - .to_module_def(position.file_id) - .and_then(|current_module| { - let definition_source = current_module.definition_source(db); - let module_definition_source_file = definition_source.file_id.original_file(db); - let mod_declaration_candidates = - db.possible_sudmobule_names(module_definition_source_file); - dbg!(mod_declaration_candidates); - // TODO kb exlude existing children from the candidates - let existing_children = current_module.children(db).collect::>(); - None::> - }) - .unwrap_or_default(); - }; - let krate = sema.to_module_def(position.file_id).map(|m| m.krate()); let original_token = original_file.syntax().token_at_offset(position.offset).left_biased()?; -- cgit v1.2.3