Skip to content

Commit 312913a

Browse files
committed
Auto merge of rust-lang#12461 - Veykril:completions, r=Veykril
Move trait_impl completion analysis into CompletionContext
2 parents d06d0f8 + a2a74bf commit 312913a

File tree

14 files changed

+403
-437
lines changed

14 files changed

+403
-437
lines changed

crates/ide-completion/src/completions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub(crate) mod pattern;
1616
pub(crate) mod postfix;
1717
pub(crate) mod record;
1818
pub(crate) mod snippet;
19-
pub(crate) mod trait_impl;
2019
pub(crate) mod r#type;
2120
pub(crate) mod use_;
2221
pub(crate) mod vis;

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
4646
return;
4747
}
4848
match ctx.path_context() {
49-
Some(PathCompletionCtx {
50-
is_absolute_path: false,
51-
qualifier: None,
52-
kind: PathKind::Expr { .. },
53-
..
54-
}) if !ctx.is_path_disallowed() => {}
49+
Some(
50+
path_ctx @ PathCompletionCtx {
51+
is_absolute_path: false,
52+
qualifier: None,
53+
kind: PathKind::Expr { .. },
54+
..
55+
},
56+
) if path_ctx.is_trivial_path() && ctx.qualifier_ctx.none() => {}
5557
_ => return,
5658
}
5759

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

+78-43
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,38 @@ use crate::{
1111

1212
pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) {
1313
let _p = profile::span("complete_expr_path");
14-
if ctx.is_path_disallowed() {
15-
return;
16-
}
1714

18-
let (is_absolute_path, qualifier, in_block_expr, in_loop_body, is_func_update, after_if_expr) =
19-
match ctx.nameref_ctx() {
20-
Some(NameRefContext {
21-
path_ctx:
22-
Some(PathCompletionCtx {
23-
kind: PathKind::Expr { in_block_expr, in_loop_body, after_if_expr },
24-
is_absolute_path,
25-
qualifier,
26-
..
27-
}),
28-
record_expr,
29-
..
30-
}) => (
31-
*is_absolute_path,
32-
qualifier,
33-
*in_block_expr,
34-
*in_loop_body,
35-
record_expr.as_ref().map_or(false, |&(_, it)| it),
36-
*after_if_expr,
37-
),
38-
_ => return,
39-
};
15+
let (
16+
is_absolute_path,
17+
qualifier,
18+
in_block_expr,
19+
in_loop_body,
20+
is_func_update,
21+
after_if_expr,
22+
wants_mut_token,
23+
) = match ctx.nameref_ctx() {
24+
Some(NameRefContext {
25+
path_ctx:
26+
Some(PathCompletionCtx {
27+
kind:
28+
PathKind::Expr { in_block_expr, in_loop_body, after_if_expr, ref_expr_parent },
29+
is_absolute_path,
30+
qualifier,
31+
..
32+
}),
33+
record_expr,
34+
..
35+
}) if ctx.qualifier_ctx.none() => (
36+
*is_absolute_path,
37+
qualifier,
38+
*in_block_expr,
39+
*in_loop_body,
40+
record_expr.as_ref().map_or(false, |&(_, it)| it),
41+
*after_if_expr,
42+
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false),
43+
),
44+
_ => return,
45+
};
4046

4147
let scope_def_applicable = |def| {
4248
use hir::{GenericParam::*, ModuleDef::*};
@@ -164,12 +170,43 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
164170
None if is_absolute_path => acc.add_crate_roots(ctx),
165171
None => {
166172
acc.add_nameref_keywords_with_colon(ctx);
167-
if let Some(hir::Adt::Enum(e)) =
173+
if let Some(adt) =
168174
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
169175
{
170-
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
171-
acc.add_qualified_enum_variant(ctx, variant, path)
172-
});
176+
let self_ty =
177+
(|| ctx.sema.to_def(ctx.impl_def.as_ref()?)?.self_ty(ctx.db).as_adt())();
178+
let complete_self = self_ty == Some(adt);
179+
180+
match adt {
181+
hir::Adt::Struct(strukt) => {
182+
let path = ctx
183+
.module
184+
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
185+
.filter(|it| it.len() > 1);
186+
187+
acc.add_struct_literal(ctx, strukt, path, None);
188+
189+
if complete_self {
190+
acc.add_struct_literal(ctx, strukt, None, Some(hir::known::SELF_TYPE));
191+
}
192+
}
193+
hir::Adt::Union(un) => {
194+
let path = ctx
195+
.module
196+
.find_use_path(ctx.db, hir::ModuleDef::from(un))
197+
.filter(|it| it.len() > 1);
198+
199+
acc.add_union_literal(ctx, un, path, None);
200+
if complete_self {
201+
acc.add_union_literal(ctx, un, None, Some(hir::known::SELF_TYPE));
202+
}
203+
}
204+
hir::Adt::Enum(e) => {
205+
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
206+
acc.add_qualified_enum_variant(ctx, variant, path)
207+
});
208+
}
209+
}
173210
}
174211
ctx.process_all_names(&mut |name, def| {
175212
if scope_def_applicable(def) {
@@ -180,20 +217,18 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
180217
if !is_func_update {
181218
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
182219

183-
if ctx.expects_expression() {
184-
if !in_block_expr {
185-
add_keyword("unsafe", "unsafe {\n $0\n}");
186-
}
187-
add_keyword("match", "match $1 {\n $0\n}");
188-
add_keyword("while", "while $1 {\n $0\n}");
189-
add_keyword("while let", "while let $1 = $2 {\n $0\n}");
190-
add_keyword("loop", "loop {\n $0\n}");
191-
add_keyword("if", "if $1 {\n $0\n}");
192-
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
193-
add_keyword("for", "for $1 in $2 {\n $0\n}");
194-
add_keyword("true", "true");
195-
add_keyword("false", "false");
220+
if !in_block_expr {
221+
add_keyword("unsafe", "unsafe {\n $0\n}");
196222
}
223+
add_keyword("match", "match $1 {\n $0\n}");
224+
add_keyword("while", "while $1 {\n $0\n}");
225+
add_keyword("while let", "while let $1 = $2 {\n $0\n}");
226+
add_keyword("loop", "loop {\n $0\n}");
227+
add_keyword("if", "if $1 {\n $0\n}");
228+
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
229+
add_keyword("for", "for $1 in $2 {\n $0\n}");
230+
add_keyword("true", "true");
231+
add_keyword("false", "false");
197232

198233
if ctx.previous_token_is(T![if])
199234
|| ctx.previous_token_is(T![while])
@@ -207,7 +242,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
207242
add_keyword("else if", "else if $1 {\n $0\n}");
208243
}
209244

210-
if ctx.expects_ident_ref_expr() {
245+
if wants_mut_token {
211246
add_keyword("mut", "mut ");
212247
}
213248

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

+23-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use itertools::Itertools;
88
use syntax::{AstNode, SyntaxNode, T};
99

1010
use crate::{
11-
context::{CompletionContext, PathKind},
11+
context::{CompletionContext, NameRefContext, PathCompletionCtx, PathKind, PatternContext},
1212
patterns::ImmediateLocation,
1313
render::{render_resolution_with_import, RenderContext},
1414
};
@@ -110,16 +110,26 @@ 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 matches!(ctx.path_kind(), Some(PathKind::Vis { .. } | PathKind::Use | PathKind::Item { .. }))
114-
|| ctx.is_path_disallowed()
115-
{
116-
return None;
117-
}
118-
// FIXME: This should be encoded in a different way
119-
if ctx.pattern_ctx.is_none() && ctx.path_context().is_none() && !ctx.has_dot_receiver() {
120-
// completion inside `ast::Name` of a item declaration
121-
return None;
122-
}
113+
let path_kind = match ctx.nameref_ctx() {
114+
Some(NameRefContext { path_ctx: Some(PathCompletionCtx { kind, .. }), .. })
115+
if matches!(
116+
kind,
117+
PathKind::Expr { .. }
118+
| PathKind::Type { .. }
119+
| PathKind::Attr { .. }
120+
| PathKind::Derive
121+
| PathKind::Pat
122+
) =>
123+
{
124+
Some(kind)
125+
}
126+
Some(NameRefContext { dot_access: Some(_), .. }) => None,
127+
None if matches!(ctx.pattern_ctx, Some(PatternContext { record_pat: None, .. })) => {
128+
Some(&PathKind::Pat)
129+
}
130+
_ => return None,
131+
};
132+
123133
let potential_import_name = {
124134
let token_kind = ctx.token.kind();
125135
if matches!(token_kind, T![.] | T![::]) {
@@ -138,18 +148,10 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
138148
return None;
139149
}
140150

141-
let path_kind = match ctx.path_kind() {
142-
Some(kind) => Some(kind),
143-
None if ctx.pattern_ctx.is_some() => Some(PathKind::Pat),
144-
None => None,
145-
};
146151
let ns_filter = |import: &LocatedImport| {
147152
let path_kind = match path_kind {
148-
Some(path_kind) => path_kind,
149-
None => match import.original_item {
150-
ItemInNs::Macros(mac) => return mac.is_fn_like(ctx.db),
151-
_ => return true,
152-
},
153+
Some(it) => it,
154+
None => return true,
153155
};
154156
match (path_kind, import.original_item) {
155157
// Aren't handled in flyimport

0 commit comments

Comments
 (0)