Skip to content

Commit 6ba479c

Browse files
Finally cretae the mod completion module
1 parent 33179a0 commit 6ba479c

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

crates/base_db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn possible_sudmobule_names(module_files: &FileSet, module_file: FileId) -> Vec<
199199
})
200200
.filter_map(|file_name_and_extension| {
201201
match file_name_and_extension {
202-
// TODO kb wrong resolution for nested non-file modules (mod tests {mod <|>)
202+
// TODO kb wrong resolution for nested non-file modules (mod tests { mod <|> })
203203
// TODO kb in src/bin when a module is included into another,
204204
// the included file gets "moved" into a directory below and now cannot add any other modules
205205
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None,

crates/ide/src/completion.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod complete_unqualified_path;
1919
mod complete_postfix;
2020
mod complete_macro_in_item_position;
2121
mod complete_trait_impl;
22+
mod complete_mod;
2223

2324
use ide_db::RootDatabase;
2425

@@ -124,6 +125,7 @@ pub(crate) fn completions(
124125
complete_postfix::complete_postfix(&mut acc, &ctx);
125126
complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
126127
complete_trait_impl::complete_trait_impl(&mut acc, &ctx);
128+
complete_mod::complete_mod(&mut acc, &ctx);
127129

128130
Some(acc)
129131
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Completes mod declarations.
2+
3+
use base_db::FileLoader;
4+
use hir::ModuleSource;
5+
6+
use super::{completion_context::CompletionContext, completion_item::Completions};
7+
8+
/// Complete mod declaration, i.e. `mod <|> ;`
9+
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) {
10+
let module_names_for_import = ctx
11+
.sema
12+
// TODO kb this is wrong, since we need not the file module
13+
.to_module_def(ctx.position.file_id)
14+
.and_then(|current_module| {
15+
dbg!(current_module.name(ctx.db));
16+
dbg!(current_module.definition_source(ctx.db));
17+
dbg!(current_module.declaration_source(ctx.db));
18+
let mut zz = Vec::new();
19+
let mut vv = Some(current_module);
20+
while let Some(ModuleSource::Module(_)) =
21+
vv.map(|vv| vv.definition_source(ctx.db).value)
22+
{
23+
zz.push(current_module.name(ctx.db));
24+
vv = current_module.parent(ctx.db);
25+
}
26+
dbg!(zz);
27+
let definition_source = current_module.definition_source(ctx.db);
28+
// TODO kb filter out declarations in possible_sudmobule_names
29+
// let declaration_source = current_module.declaration_source(ctx.db);
30+
let module_definition_source_file = definition_source.file_id.original_file(ctx.db);
31+
let mod_declaration_candidates =
32+
ctx.db.possible_sudmobule_names(module_definition_source_file);
33+
dbg!(mod_declaration_candidates);
34+
// TODO kb exlude existing children from the candidates
35+
let existing_children = current_module.children(ctx.db).collect::<Vec<_>>();
36+
None::<Vec<String>>
37+
})
38+
.unwrap_or_default();
39+
}

crates/ide/src/completion/completion_context.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! FIXME: write short doc here
22
33
use base_db::{FileLoader, SourceDatabase};
4-
use hir::{Semantics, SemanticsScope, Type};
4+
use hir::{ModuleSource, Semantics, SemanticsScope, Type};
55
use ide_db::RootDatabase;
66
use syntax::{
77
algo::{find_covering_element, find_node_at_offset},
@@ -112,22 +112,6 @@ impl<'a> CompletionContext<'a> {
112112
};
113113
let fake_ident_token =
114114
file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap();
115-
{
116-
let module_names_for_import = sema
117-
.to_module_def(position.file_id)
118-
.and_then(|current_module| {
119-
let definition_source = current_module.definition_source(db);
120-
let module_definition_source_file = definition_source.file_id.original_file(db);
121-
let mod_declaration_candidates =
122-
db.possible_sudmobule_names(module_definition_source_file);
123-
dbg!(mod_declaration_candidates);
124-
// TODO kb exlude existing children from the candidates
125-
let existing_children = current_module.children(db).collect::<Vec<_>>();
126-
None::<Vec<String>>
127-
})
128-
.unwrap_or_default();
129-
};
130-
131115
let krate = sema.to_module_def(position.file_id).map(|m| m.krate());
132116
let original_token =
133117
original_file.syntax().token_at_offset(position.offset).left_biased()?;

0 commit comments

Comments
 (0)