Skip to content

Commit 9fb8321

Browse files
Complete semicolon when needed
1 parent 57a260f commit 9fb8321

File tree

6 files changed

+30
-26
lines changed

6 files changed

+30
-26
lines changed

crates/ide/src/completion/complete_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::completion::{
1313
};
1414

1515
pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
16-
if ctx.mod_is_prev {
16+
if ctx.mod_under_caret.is_some() {
1717
return None;
1818
}
1919

crates/ide/src/completion/complete_mod.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ use hir::{Module, ModuleSource};
55
use ide_db::RootDatabase;
66
use rustc_hash::FxHashSet;
77

8-
use super::{completion_context::CompletionContext, completion_item::Completions};
8+
use crate::{CompletionItem, CompletionItemKind};
9+
10+
use super::{
11+
completion_context::CompletionContext, completion_item::CompletionKind,
12+
completion_item::Completions,
13+
};
914

1015
/// Complete mod declaration, i.e. `mod <|> ;`
1116
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
12-
let _p = profile::span("completion::complete_mod");
17+
let mod_under_caret = match &ctx.mod_under_caret {
18+
Some(mod_under_caret) if mod_under_caret.item_list().is_some() => return None,
19+
Some(mod_under_caret) => mod_under_caret,
20+
None => return None,
21+
};
1322

14-
if !ctx.mod_is_prev {
15-
return None;
16-
}
23+
let _p = profile::span("completion::complete_mod");
1724

1825
let current_module = ctx.scope.module()?;
1926

@@ -36,7 +43,7 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
3643
module_declaration_source_file.file_id.original_file(ctx.db)
3744
});
3845

39-
let mod_declaration_candidates = source_root
46+
source_root
4047
.iter()
4148
.filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
4249
.filter(|submodule_candidate_file| {
@@ -66,10 +73,16 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
6673
_ => None,
6774
})
6875
.filter(|name| !existing_mod_declarations.contains(name))
69-
.collect::<Vec<_>>();
70-
dbg!(mod_declaration_candidates);
71-
72-
// TODO kb actually add the results
76+
.for_each(|submodule_name| {
77+
let mut label = submodule_name;
78+
if mod_under_caret.semicolon_token().is_none() {
79+
label.push(';')
80+
}
81+
acc.add(
82+
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
83+
.kind(CompletionItemKind::Module),
84+
)
85+
});
7386

7487
Some(())
7588
}

crates/ide/src/completion/complete_qualified_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
1313
None => return,
1414
};
1515

16-
if ctx.attribute_under_caret.is_some() || ctx.mod_is_prev {
16+
if ctx.attribute_under_caret.is_some() || ctx.mod_under_caret.is_some() {
1717
return;
1818
}
1919

crates/ide/src/completion/complete_unqualified_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1313
if ctx.record_lit_syntax.is_some()
1414
|| ctx.record_pat_syntax.is_some()
1515
|| ctx.attribute_under_caret.is_some()
16-
|| ctx.mod_is_prev
16+
|| ctx.mod_under_caret.is_some()
1717
{
1818
return;
1919
}

crates/ide/src/completion/completion_context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
has_bind_pat_parent, has_block_expr_parent, has_field_list_parent,
2020
has_impl_as_prev_sibling, has_impl_parent, has_item_list_or_source_file_parent,
2121
has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, if_is_prev,
22-
is_in_loop_body, is_match_arm, mod_is_prev, unsafe_is_prev,
22+
is_in_loop_body, is_match_arm, unsafe_is_prev,
2323
},
2424
CompletionConfig,
2525
},
@@ -77,7 +77,7 @@ pub(crate) struct CompletionContext<'a> {
7777
pub(super) is_path_type: bool,
7878
pub(super) has_type_args: bool,
7979
pub(super) attribute_under_caret: Option<ast::Attr>,
80-
pub(super) mod_is_prev: bool,
80+
pub(super) mod_under_caret: Option<ast::Module>,
8181
pub(super) unsafe_is_prev: bool,
8282
pub(super) if_is_prev: bool,
8383
pub(super) block_expr_parent: bool,
@@ -153,7 +153,7 @@ impl<'a> CompletionContext<'a> {
153153
has_type_args: false,
154154
dot_receiver_is_ambiguous_float_literal: false,
155155
attribute_under_caret: None,
156-
mod_is_prev: false,
156+
mod_under_caret: None,
157157
unsafe_is_prev: false,
158158
in_loop_body: false,
159159
ref_pat_parent: false,
@@ -241,7 +241,7 @@ impl<'a> CompletionContext<'a> {
241241
self.is_match_arm = is_match_arm(syntax_element.clone());
242242
self.has_item_list_or_source_file_parent =
243243
has_item_list_or_source_file_parent(syntax_element.clone());
244-
self.mod_is_prev = mod_is_prev(syntax_element);
244+
self.mod_under_caret = find_node_at_offset(&file_with_fake_ident, offset);
245245
}
246246

247247
fn fill(

crates/ide/src/completion/patterns.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,6 @@ pub(crate) fn if_is_prev(element: SyntaxElement) -> bool {
116116
.is_some()
117117
}
118118

119-
// TODO kb generify?
120-
pub(crate) fn mod_is_prev(element: SyntaxElement) -> bool {
121-
element
122-
.into_token()
123-
.and_then(|it| previous_non_trivia_token(it))
124-
.filter(|it| it.kind() == MOD_KW)
125-
.is_some()
126-
}
127-
128119
#[test]
129120
fn test_if_is_prev() {
130121
check_pattern_is_applicable(r"if l<|>", if_is_prev);

0 commit comments

Comments
 (0)