Skip to content

Commit a2940c4

Browse files
bors[bot]Veykril
andauthored
Merge #9020
9020: fix: Don't complete non-macro item paths in impls and modules r=Veykril a=Veykril Part of #8518 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents cc5d806 + 3a16950 commit a2940c4

File tree

7 files changed

+198
-134
lines changed

7 files changed

+198
-134
lines changed

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
110110
if !ctx.config.enable_imports_on_the_fly {
111111
return None;
112112
}
113-
if ctx.use_item_syntax.is_some() || ctx.is_path_disallowed() {
113+
if ctx.use_item_syntax.is_some()
114+
|| ctx.is_path_disallowed()
115+
|| ctx.expects_item()
116+
|| ctx.expects_assoc_item()
117+
{
114118
return None;
115119
}
116120
let potential_import_name = {

crates/ide_completion/src/completions/keyword.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,35 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
4949
return;
5050
}
5151

52-
let has_trait_or_impl_parent = ctx.has_impl_or_trait_parent();
52+
let expects_assoc_item = ctx.expects_assoc_item();
5353
let has_block_expr_parent = ctx.has_block_expr_parent();
54-
let has_item_list_parent = ctx.has_item_list_parent();
54+
let expects_item = ctx.expects_item();
5555
if ctx.has_impl_or_trait_prev_sibling() {
5656
add_keyword(ctx, acc, "where", "where ");
5757
return;
5858
}
5959
if ctx.previous_token_is(T![unsafe]) {
60-
if has_item_list_parent || has_block_expr_parent {
60+
if expects_item || has_block_expr_parent {
6161
add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}")
6262
}
6363

64-
if has_item_list_parent || has_block_expr_parent {
64+
if expects_item || has_block_expr_parent {
6565
add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}");
6666
add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}");
6767
}
6868

6969
return;
7070
}
71-
if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent {
71+
if expects_item || expects_assoc_item || has_block_expr_parent {
7272
add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}");
7373
}
74-
if has_item_list_parent || has_block_expr_parent {
74+
if expects_item || has_block_expr_parent {
7575
add_keyword(ctx, acc, "use", "use ");
7676
add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}");
7777
add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}");
7878
}
7979

80-
if has_item_list_parent {
80+
if expects_item {
8181
add_keyword(ctx, acc, "enum", "enum $1 {\n $0\n}");
8282
add_keyword(ctx, acc, "struct", "struct $0");
8383
add_keyword(ctx, acc, "union", "union $1 {\n $0\n}");
@@ -101,24 +101,23 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
101101
add_keyword(ctx, acc, "else", "else {\n $0\n}");
102102
add_keyword(ctx, acc, "else if", "else if $1 {\n $0\n}");
103103
}
104-
if has_item_list_parent || has_block_expr_parent {
104+
if expects_item || has_block_expr_parent {
105105
add_keyword(ctx, acc, "mod", "mod $0");
106106
}
107-
if ctx.has_ident_or_ref_pat_parent() {
107+
if ctx.expects_ident_pat_or_ref_expr() {
108108
add_keyword(ctx, acc, "mut", "mut ");
109109
}
110-
if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent {
110+
if expects_item || expects_assoc_item || has_block_expr_parent {
111111
add_keyword(ctx, acc, "const", "const ");
112112
add_keyword(ctx, acc, "type", "type ");
113113
}
114-
if has_item_list_parent || has_block_expr_parent {
114+
if expects_item || has_block_expr_parent {
115115
add_keyword(ctx, acc, "static", "static ");
116116
};
117-
if has_item_list_parent || has_block_expr_parent {
117+
if expects_item || has_block_expr_parent {
118118
add_keyword(ctx, acc, "extern", "extern ");
119119
}
120-
if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent || ctx.is_match_arm
121-
{
120+
if expects_item || expects_assoc_item || has_block_expr_parent || ctx.is_match_arm {
122121
add_keyword(ctx, acc, "unsafe", "unsafe ");
123122
}
124123
if ctx.in_loop_body {
@@ -130,7 +129,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
130129
add_keyword(ctx, acc, "break", "break");
131130
}
132131
}
133-
if has_item_list_parent || ctx.has_impl_parent() || ctx.has_field_list_parent() {
132+
if expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field() {
134133
add_keyword(ctx, acc, "pub(crate)", "pub(crate) ");
135134
add_keyword(ctx, acc, "pub", "pub ");
136135
}

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
2020
None => return,
2121
};
2222
let context_module = ctx.scope.module();
23+
if ctx.expects_item() || ctx.expects_assoc_item() {
24+
if let PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
25+
let module_scope = module.scope(ctx.db, context_module);
26+
for (name, def) in module_scope {
27+
if let ScopeDef::MacroDef(macro_def) = def {
28+
acc.add_macro(ctx, Some(name.to_string()), macro_def);
29+
}
30+
}
31+
}
32+
return;
33+
}
2334

2435
// Add associated types on type parameters and `Self`.
2536
resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
@@ -594,14 +605,33 @@ fn main() { T::$0; }
594605
macro_rules! foo { () => {} }
595606
596607
fn main() { let _ = crate::$0 }
597-
"#,
608+
"#,
598609
expect![[r##"
599610
fn main() fn()
600611
ma foo!(…) #[macro_export] macro_rules! foo
601612
"##]],
602613
);
603614
}
604615

616+
#[test]
617+
fn completes_qualified_macros_in_impl() {
618+
check(
619+
r#"
620+
#[macro_export]
621+
macro_rules! foo { () => {} }
622+
623+
struct MyStruct {}
624+
625+
impl MyStruct {
626+
crate::$0
627+
}
628+
"#,
629+
expect![[r##"
630+
ma foo! #[macro_export] macro_rules! foo
631+
"##]],
632+
);
633+
}
634+
605635
#[test]
606636
fn test_super_super_completion() {
607637
check(

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1212
if ctx.is_path_disallowed() {
1313
return;
1414
}
15+
if ctx.expects_item() || ctx.expects_assoc_item() {
16+
ctx.scope.process_all_names(&mut |name, def| {
17+
if let ScopeDef::MacroDef(macro_def) = def {
18+
acc.add_macro(ctx, Some(name.to_string()), macro_def);
19+
}
20+
});
21+
return;
22+
}
1523

1624
if let Some(hir::Adt::Enum(e)) =
1725
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
@@ -647,7 +655,7 @@ fn f() {}
647655
}
648656

649657
#[test]
650-
fn completes_type_or_trait_in_impl_block() {
658+
fn completes_target_type_or_trait_in_impl_block() {
651659
check(
652660
r#"
653661
trait MyTrait {}
@@ -662,4 +670,21 @@ impl My$0
662670
"#]],
663671
)
664672
}
673+
674+
#[test]
675+
fn only_completes_macros_in_assoc_item_list() {
676+
check(
677+
r#"
678+
struct MyStruct {}
679+
macro_rules! foo {}
680+
681+
impl MyStruct {
682+
$0
683+
}
684+
"#,
685+
expect![[r#"
686+
ma foo! macro_rules! foo
687+
"#]],
688+
)
689+
}
665690
}

crates/ide_completion/src/context.rs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ use text_edit::Indel;
1717

1818
use crate::{
1919
patterns::{
20-
for_is_prev2, has_bind_pat_parent, has_block_expr_parent, has_field_list_parent,
21-
has_impl_parent, has_item_list_or_source_file_parent, has_prev_sibling, has_ref_parent,
22-
has_trait_parent, inside_impl_trait_block, is_in_loop_body, is_match_arm, previous_token,
20+
determine_location, for_is_prev2, has_prev_sibling, inside_impl_trait_block,
21+
is_in_loop_body, is_match_arm, previous_token, ImmediateLocation,
2322
},
2423
CompletionConfig,
2524
};
@@ -30,18 +29,6 @@ pub(crate) enum PatternRefutability {
3029
Irrefutable,
3130
}
3231

33-
/// Direct parent container of the cursor position
34-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
35-
pub(crate) enum ImmediateLocation {
36-
Impl,
37-
Trait,
38-
RecordFieldList,
39-
RefPatOrExpr,
40-
IdentPat,
41-
BlockExpr,
42-
ItemList,
43-
}
44-
4532
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4633
pub(crate) enum PrevSibling {
4734
Trait,
@@ -127,6 +114,7 @@ pub(crate) struct CompletionContext<'a> {
127114

128115
no_completion_required: bool,
129116
}
117+
130118
impl<'a> CompletionContext<'a> {
131119
pub(super) fn new(
132120
db: &'a RootDatabase,
@@ -281,34 +269,34 @@ impl<'a> CompletionContext<'a> {
281269
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
282270
}
283271

284-
pub(crate) fn has_impl_or_trait_parent(&self) -> bool {
272+
pub(crate) fn expects_assoc_item(&self) -> bool {
285273
matches!(
286274
self.completion_location,
287275
Some(ImmediateLocation::Trait) | Some(ImmediateLocation::Impl)
288276
)
289277
}
290278

291-
pub(crate) fn has_block_expr_parent(&self) -> bool {
292-
matches!(self.completion_location, Some(ImmediateLocation::BlockExpr))
279+
pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
280+
matches!(self.completion_location, Some(ImmediateLocation::Impl))
293281
}
294282

295-
pub(crate) fn has_item_list_parent(&self) -> bool {
283+
pub(crate) fn expects_item(&self) -> bool {
296284
matches!(self.completion_location, Some(ImmediateLocation::ItemList))
297285
}
298286

299-
pub(crate) fn has_ident_or_ref_pat_parent(&self) -> bool {
287+
pub(crate) fn has_block_expr_parent(&self) -> bool {
288+
matches!(self.completion_location, Some(ImmediateLocation::BlockExpr))
289+
}
290+
291+
pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool {
300292
matches!(
301293
self.completion_location,
302-
Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefPatOrExpr)
294+
Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefExpr)
303295
)
304296
}
305297

306-
pub(crate) fn has_impl_parent(&self) -> bool {
307-
matches!(self.completion_location, Some(ImmediateLocation::Impl))
308-
}
309-
310-
pub(crate) fn has_field_list_parent(&self) -> bool {
311-
matches!(self.completion_location, Some(ImmediateLocation::RecordFieldList))
298+
pub(crate) fn expect_record_field(&self) -> bool {
299+
matches!(self.completion_location, Some(ImmediateLocation::RecordField))
312300
}
313301

314302
pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool {
@@ -320,12 +308,11 @@ impl<'a> CompletionContext<'a> {
320308
|| self.record_pat_syntax.is_some()
321309
|| self.attribute_under_caret.is_some()
322310
|| self.mod_declaration_under_caret.is_some()
323-
|| self.has_impl_or_trait_parent()
324311
}
325312

326313
fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
327314
let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
328-
let syntax_element = NodeOrToken::Token(fake_ident_token);
315+
let syntax_element = NodeOrToken::Token(fake_ident_token.clone());
329316
self.previous_token = previous_token(syntax_element.clone());
330317
self.in_loop_body = is_in_loop_body(syntax_element.clone());
331318
self.is_match_arm = is_match_arm(syntax_element.clone());
@@ -335,22 +322,6 @@ impl<'a> CompletionContext<'a> {
335322
self.prev_sibling = Some(PrevSibling::Trait)
336323
}
337324

338-
if has_block_expr_parent(syntax_element.clone()) {
339-
self.completion_location = Some(ImmediateLocation::BlockExpr);
340-
} else if has_bind_pat_parent(syntax_element.clone()) {
341-
self.completion_location = Some(ImmediateLocation::IdentPat);
342-
} else if has_ref_parent(syntax_element.clone()) {
343-
self.completion_location = Some(ImmediateLocation::RefPatOrExpr);
344-
} else if has_impl_parent(syntax_element.clone()) {
345-
self.completion_location = Some(ImmediateLocation::Impl);
346-
} else if has_field_list_parent(syntax_element.clone()) {
347-
self.completion_location = Some(ImmediateLocation::RecordFieldList);
348-
} else if has_trait_parent(syntax_element.clone()) {
349-
self.completion_location = Some(ImmediateLocation::Trait);
350-
} else if has_item_list_or_source_file_parent(syntax_element.clone()) {
351-
self.completion_location = Some(ImmediateLocation::ItemList);
352-
}
353-
354325
self.mod_declaration_under_caret =
355326
find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset)
356327
.filter(|module| module.item_list().is_none());
@@ -363,6 +334,8 @@ impl<'a> CompletionContext<'a> {
363334
let fn_is_prev = self.previous_token_is(T![fn]);
364335
let for_is_prev2 = for_is_prev2(syntax_element.clone());
365336
self.no_completion_required = (fn_is_prev && !inside_impl_trait_block) || for_is_prev2;
337+
338+
self.completion_location = determine_location(fake_ident_token);
366339
}
367340

368341
fn fill_impl_def(&mut self) {

0 commit comments

Comments
 (0)