Skip to content

Commit 4b1eb98

Browse files
committed
Auto merge of rust-lang#12161 - Veykril:completion-rev, r=Veykril
internal: Lift out item list path completions from (un)qualified_path cc rust-lang/rust-analyzer#12144
2 parents 52a58f6 + 25d133e commit 4b1eb98

13 files changed

+97
-68
lines changed

crates/ide-completion/src/completions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub(crate) mod extern_abi;
66
pub(crate) mod flyimport;
77
pub(crate) mod fn_param;
88
pub(crate) mod format_string;
9+
pub(crate) mod item_list;
910
pub(crate) mod keyword;
1011
pub(crate) mod lifetime;
1112
pub(crate) mod mod_;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Completion of paths and keywords at item list position.
2+
3+
use crate::{
4+
completions::module_or_fn_macro,
5+
context::{PathCompletionCtx, PathKind, PathQualifierCtx},
6+
CompletionContext, Completions,
7+
};
8+
9+
pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) {
10+
let _p = profile::span("complete_unqualified_path");
11+
if ctx.is_path_disallowed() || ctx.has_unfinished_impl_or_trait_prev_sibling() {
12+
return;
13+
}
14+
15+
let (&is_absolute_path, qualifier) = match &ctx.path_context {
16+
Some(PathCompletionCtx {
17+
kind: Some(PathKind::Item), is_absolute_path, qualifier, ..
18+
}) => (is_absolute_path, qualifier),
19+
_ => return,
20+
};
21+
22+
match qualifier {
23+
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
24+
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
25+
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
26+
if let Some(def) = module_or_fn_macro(ctx.db, def) {
27+
acc.add_resolution(ctx, name, def);
28+
}
29+
}
30+
}
31+
32+
if *is_super_chain {
33+
acc.add_keyword(ctx, "super::");
34+
}
35+
}
36+
None if is_absolute_path => {
37+
acc.add_crate_roots(ctx);
38+
}
39+
None => {
40+
ctx.process_all_names(&mut |name, def| {
41+
if let Some(def) = module_or_fn_macro(ctx.db, def) {
42+
acc.add_resolution(ctx, name, def);
43+
}
44+
});
45+
acc.add_nameref_keywords_with_colon(ctx);
46+
}
47+
}
48+
}

crates/ide-completion/src/completions/keyword.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
4040
if let Some(PathKind::Vis { .. }) = ctx.path_kind() {
4141
return;
4242
}
43-
if ctx.has_impl_or_trait_prev_sibling() {
43+
if ctx.has_unfinished_impl_or_trait_prev_sibling() {
4444
add_keyword("where", "where");
4545
if ctx.has_impl_prev_sibling() {
4646
add_keyword("for", "for");

crates/ide-completion/src/completions/pattern.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,13 @@ fn pattern_path_completion(
196196
// qualifier can only be none here if we are in a TuplePat or RecordPat in which case special characters have to follow the path
197197
None if *is_absolute_path => acc.add_crate_roots(ctx),
198198
None => {
199-
cov_mark::hit!(unqualified_path_only_modules_in_import);
200199
ctx.process_all_names(&mut |name, res| {
201-
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
200+
// FIXME: properly filter here
201+
if let ScopeDef::ModuleDef(_) = res {
202202
acc.add_resolution(ctx, name, res);
203203
}
204204
});
205+
205206
acc.add_nameref_keywords_with_colon(ctx);
206207
}
207208
}

crates/ide-completion/src/completions/qualified_path.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ use ide_db::FxHashSet;
55
use syntax::ast;
66

77
use crate::{
8-
completions::module_or_fn_macro,
98
context::{PathCompletionCtx, PathKind},
10-
patterns::ImmediateLocation,
119
CompletionContext, Completions,
1210
};
1311

1412
pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
15-
if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
13+
if ctx.is_path_disallowed() || ctx.has_unfinished_impl_or_trait_prev_sibling() {
1614
return;
1715
}
1816
if ctx.pattern_ctx.is_some() {
@@ -54,26 +52,13 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
5452
None => return,
5553
};
5654

57-
match ctx.completion_location {
58-
Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
59-
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
60-
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
61-
if let Some(def) = module_or_fn_macro(ctx.db, def) {
62-
acc.add_resolution(ctx, name, def);
63-
}
64-
}
65-
}
66-
return;
67-
}
68-
_ => (),
69-
}
70-
7155
match kind {
7256
Some(
7357
PathKind::Pat
7458
| PathKind::Attr { .. }
7559
| PathKind::Vis { .. }
7660
| PathKind::Use
61+
| PathKind::Item
7762
| PathKind::Derive,
7863
) => {
7964
return;

crates/ide-completion/src/completions/snippet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
4646
if !(ctx.expects_item() || ctx.has_block_expr_parent())
4747
|| ctx.previous_token_is(T![unsafe])
4848
|| ctx.path_qual().is_some()
49-
|| ctx.has_impl_or_trait_prev_sibling()
49+
|| ctx.has_unfinished_impl_or_trait_prev_sibling()
5050
{
5151
return;
5252
}

crates/ide-completion/src/completions/trait_impl.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -547,12 +547,7 @@ impl Test for T {
547547
type Test = fn $0;
548548
}
549549
",
550-
expect![[r#"
551-
sp Self
552-
st T
553-
tt Test
554-
bt u32
555-
"#]],
550+
expect![[r#""#]],
556551
);
557552
}
558553

crates/ide-completion/src/completions/unqualified_path.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ use hir::ScopeDef;
44
use syntax::{ast, AstNode};
55

66
use crate::{
7-
completions::module_or_fn_macro,
87
context::{PathCompletionCtx, PathKind},
98
patterns::ImmediateLocation,
109
CompletionContext, Completions,
1110
};
1211

1312
pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
1413
let _p = profile::span("complete_unqualified_path");
15-
if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
14+
if ctx.is_path_disallowed() || ctx.has_unfinished_impl_or_trait_prev_sibling() {
1615
return;
1716
}
18-
match ctx.path_context {
17+
18+
match &ctx.path_context {
1919
Some(PathCompletionCtx {
2020
is_absolute_path: false,
2121
qualifier: None,
22-
kind: None | Some(PathKind::Expr | PathKind::Type | PathKind::Item),
22+
kind: None | Some(PathKind::Expr | PathKind::Type),
2323
..
2424
}) => (),
2525
_ => return,
@@ -28,15 +28,6 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
2828
acc.add_nameref_keywords(ctx);
2929

3030
match &ctx.completion_location {
31-
Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
32-
// only show macros in {Assoc}ItemList
33-
ctx.process_all_names(&mut |name, def| {
34-
if let Some(def) = module_or_fn_macro(ctx.db, def) {
35-
acc.add_resolution(ctx, name, def);
36-
}
37-
});
38-
return;
39-
}
4031
Some(ImmediateLocation::TypeBound) => {
4132
ctx.process_all_names(&mut |name, res| {
4233
let add_resolution = match res {

crates/ide-completion/src/context.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,17 @@ impl<'a> CompletionContext<'a> {
270270
|| matches!(self.name_ctx, Some(NameContext::RecordField))
271271
}
272272

273-
pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool {
273+
/// Whether the cursor is right after a trait or impl header.
274+
/// trait Foo ident$0
275+
// FIXME: This probably shouldn't exist
276+
pub(crate) fn has_unfinished_impl_or_trait_prev_sibling(&self) -> bool {
274277
matches!(
275278
self.prev_sibling,
276279
Some(ImmediatePrevSibling::ImplDefType | ImmediatePrevSibling::TraitDefName)
277280
)
278281
}
279282

283+
// FIXME: This probably shouldn't exist
280284
pub(crate) fn has_impl_prev_sibling(&self) -> bool {
281285
matches!(self.prev_sibling, Some(ImmediatePrevSibling::ImplDefType))
282286
}
@@ -289,6 +293,7 @@ impl<'a> CompletionContext<'a> {
289293
matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
290294
}
291295

296+
// FIXME: This shouldn't exist
292297
pub(crate) fn is_path_disallowed(&self) -> bool {
293298
self.previous_token_is(T![unsafe])
294299
|| matches!(

crates/ide-completion/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub fn completions(
155155
completions::flyimport::import_on_the_fly(&mut acc, &ctx);
156156
completions::fn_param::complete_fn_param(&mut acc, &ctx);
157157
completions::format_string::format_string(&mut acc, &ctx);
158+
completions::item_list::complete_item_list(&mut acc, &ctx);
158159
completions::inferred_type(&mut acc, &ctx);
159160
completions::keyword::complete_expr_keyword(&mut acc, &ctx);
160161
completions::lifetime::complete_label(&mut acc, &ctx);

crates/ide-completion/src/tests/item.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn after_struct_name() {
9595
ma makro!(…) macro_rules! makro
9696
md module
9797
kw const
98-
kw crate
98+
kw crate::
9999
kw enum
100100
kw extern
101101
kw fn
@@ -104,10 +104,10 @@ fn after_struct_name() {
104104
kw pub
105105
kw pub(crate)
106106
kw pub(super)
107-
kw self
107+
kw self::
108108
kw static
109109
kw struct
110-
kw super
110+
kw super::
111111
kw trait
112112
kw type
113113
kw union
@@ -129,7 +129,7 @@ fn after_fn_name() {
129129
ma makro!(…) macro_rules! makro
130130
md module
131131
kw const
132-
kw crate
132+
kw crate::
133133
kw enum
134134
kw extern
135135
kw fn
@@ -138,10 +138,10 @@ fn after_fn_name() {
138138
kw pub
139139
kw pub(crate)
140140
kw pub(super)
141-
kw self
141+
kw self::
142142
kw static
143143
kw struct
144-
kw super
144+
kw super::
145145
kw trait
146146
kw type
147147
kw union

crates/ide-completion/src/tests/item_list.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn in_mod_item_list() {
1515
expect![[r#"
1616
ma makro!(…) macro_rules! makro
1717
kw const
18-
kw crate
18+
kw crate::
1919
kw enum
2020
kw extern
2121
kw fn
@@ -24,10 +24,10 @@ fn in_mod_item_list() {
2424
kw pub
2525
kw pub(crate)
2626
kw pub(super)
27-
kw self
27+
kw self::
2828
kw static
2929
kw struct
30-
kw super
30+
kw super::
3131
kw trait
3232
kw type
3333
kw union
@@ -48,7 +48,7 @@ fn in_source_file_item_list() {
4848
ma makro!(…) macro_rules! makro
4949
md module
5050
kw const
51-
kw crate
51+
kw crate::
5252
kw enum
5353
kw extern
5454
kw fn
@@ -57,10 +57,10 @@ fn in_source_file_item_list() {
5757
kw pub
5858
kw pub(crate)
5959
kw pub(super)
60-
kw self
60+
kw self::
6161
kw static
6262
kw struct
63-
kw super
63+
kw super::
6464
kw trait
6565
kw type
6666
kw union
@@ -166,13 +166,13 @@ fn in_impl_assoc_item_list() {
166166
ma makro!(…) macro_rules! makro
167167
md module
168168
kw const
169-
kw crate
169+
kw crate::
170170
kw fn
171171
kw pub
172172
kw pub(crate)
173173
kw pub(super)
174-
kw self
175-
kw super
174+
kw self::
175+
kw super::
176176
kw type
177177
kw unsafe
178178
"#]],
@@ -203,10 +203,10 @@ fn in_trait_assoc_item_list() {
203203
ma makro!(…) macro_rules! makro
204204
md module
205205
kw const
206-
kw crate
206+
kw crate::
207207
kw fn
208-
kw self
209-
kw super
208+
kw self::
209+
kw super::
210210
kw type
211211
kw unsafe
212212
"#]],
@@ -240,13 +240,13 @@ impl Test for () {
240240
md module
241241
ta type Type1 =
242242
kw const
243-
kw crate
243+
kw crate::
244244
kw fn
245245
kw pub
246246
kw pub(crate)
247247
kw pub(super)
248-
kw self
249-
kw super
248+
kw self::
249+
kw super::
250250
kw type
251251
kw unsafe
252252
"#]],

crates/ide-completion/src/tests/pattern.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ fn foo() {
394394
}
395395
"#,
396396
expect![[r#"
397+
fn foo() fn()
398+
st Bar
399+
bt u32
397400
kw crate::
398401
kw self::
399402
kw super::
@@ -403,19 +406,18 @@ fn foo() {
403406
r#"
404407
struct Foo { bar: u32 }
405408
fn foo() {
406-
match Foo { bar: 0 } {
409+
match (Foo { bar: 0 }) {
407410
F$0 { bar } => {}
408411
}
409412
}
410413
"#,
411414
expect![[r#"
412-
fn foo() fn()
415+
fn foo() fn()
413416
st Foo
414417
bt u32
415-
kw crate
416-
kw return
417-
kw self
418-
kw super
418+
kw crate::
419+
kw self::
420+
kw super::
419421
"#]],
420422
);
421423
check_empty(

0 commit comments

Comments
 (0)