]> git.lizzy.rs Git - rust.git/commitdiff
Finally cretae the mod completion module
authorKirill Bulatov <mail4score@gmail.com>
Mon, 7 Sep 2020 16:21:39 +0000 (19:21 +0300)
committerKirill Bulatov <mail4score@gmail.com>
Wed, 9 Sep 2020 22:42:20 +0000 (01:42 +0300)
crates/base_db/src/lib.rs
crates/ide/src/completion.rs
crates/ide/src/completion/complete_mod.rs [new file with mode: 0644]
crates/ide/src/completion/completion_context.rs

index 9733e1fd3b1df061491634eb20aa53ed8ae0aad2..321007d3397fb2e3fad2fbf82983487759021e2f 100644 (file)
@@ -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,
index 33bed699172bc9692ef94d06c0ddef983b73c998..daea2aa9585b187d8b3c5b97b4d11b7db06ae583 100644 (file)
@@ -19,6 +19,7 @@
 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 (file)
index 0000000..4c1e796
--- /dev/null
@@ -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::<Vec<_>>();
+            None::<Vec<String>>
+        })
+        .unwrap_or_default();
+}
index a8fe44083846879c5881030ea8715a4a03ed99fc..31886942a60495419736ad9ed6c03775f54c6ff7 100644 (file)
@@ -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 @@ pub(super) fn new(
         };
         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::<Vec<_>>();
-                    None::<Vec<String>>
-                })
-                .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()?;