Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a2a74bf

Browse files
committed
Remove some of the unnecessary helpfer functions from CompletionContext
1 parent a0c1816 commit a2a74bf

File tree

12 files changed

+151
-195
lines changed

12 files changed

+151
-195
lines changed

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

Lines changed: 8 additions & 6 deletions
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

Lines changed: 78 additions & 43 deletions
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

Lines changed: 23 additions & 21 deletions
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

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,33 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
1616
return;
1717
}
1818

19-
let (&is_absolute_path, path_qualifier, kind) = match ctx.path_context() {
20-
Some(PathCompletionCtx {
21-
kind: PathKind::Item { kind },
22-
is_absolute_path,
23-
qualifier,
24-
..
25-
}) => (is_absolute_path, qualifier, Some(kind)),
26-
Some(PathCompletionCtx {
27-
kind: PathKind::Expr { in_block_expr: true, .. },
28-
is_absolute_path,
29-
qualifier,
30-
..
31-
}) => (is_absolute_path, qualifier, None),
19+
let (&is_absolute_path, path_qualifier, kind, is_trivial_path) = match ctx.path_context() {
20+
Some(
21+
ctx @ PathCompletionCtx {
22+
kind: PathKind::Item { kind },
23+
is_absolute_path,
24+
qualifier,
25+
..
26+
},
27+
) => (is_absolute_path, qualifier, Some(kind), ctx.is_trivial_path()),
28+
Some(
29+
ctx @ PathCompletionCtx {
30+
kind: PathKind::Expr { in_block_expr: true, .. },
31+
is_absolute_path,
32+
qualifier,
33+
..
34+
},
35+
) => (is_absolute_path, qualifier, None, ctx.is_trivial_path()),
3236
_ => return,
3337
};
3438

3539
if matches!(kind, Some(ItemListKind::TraitImpl)) {
3640
trait_impl::complete_trait_impl(acc, ctx);
3741
}
3842

39-
add_keywords(acc, ctx, kind);
43+
if is_trivial_path {
44+
add_keywords(acc, ctx, kind);
45+
}
4046

4147
if kind.is_none() {
4248
// this is already handled by expression
@@ -71,9 +77,6 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
7177
}
7278

7379
fn add_keywords(acc: &mut Completions, ctx: &CompletionContext, kind: Option<&ItemListKind>) {
74-
if ctx.is_non_trivial_path() {
75-
return;
76-
}
7780
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
7881

7982
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ use crate::{context::NameRefContext, CompletionContext, Completions};
88

99
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
1010
let item = match ctx.nameref_ctx() {
11-
Some(NameRefContext { keyword: Some(item), record_expr: None, .. })
12-
if !ctx.is_non_trivial_path() =>
13-
{
14-
item
15-
}
11+
Some(NameRefContext { keyword: Some(item), record_expr: None, .. }) => item,
1612
_ => return,
1713
};
1814

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -71,43 +71,6 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
7171
Some(())
7272
}
7373

74-
pub(crate) fn complete_record_literal(
75-
acc: &mut Completions,
76-
ctx: &CompletionContext,
77-
) -> Option<()> {
78-
if !ctx.expects_expression() {
79-
return None;
80-
}
81-
82-
match ctx.expected_type.as_ref()?.as_adt()? {
83-
hir::Adt::Struct(strukt) if ctx.path_qual().is_none() => {
84-
let path = ctx
85-
.module
86-
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
87-
.filter(|it| it.len() > 1);
88-
89-
acc.add_struct_literal(ctx, strukt, path, None);
90-
91-
let impl_ = ctx.impl_def.as_ref()?;
92-
let impl_adt = ctx.sema.to_def(impl_)?.self_ty(ctx.db).as_adt()?;
93-
if hir::Adt::Struct(strukt) == impl_adt {
94-
acc.add_struct_literal(ctx, strukt, None, Some(hir::known::SELF_TYPE));
95-
}
96-
}
97-
hir::Adt::Union(un) if ctx.path_qual().is_none() => {
98-
let path = ctx
99-
.module
100-
.find_use_path(ctx.db, hir::ModuleDef::from(un))
101-
.filter(|it| it.len() > 1);
102-
103-
acc.add_union_literal(ctx, un, path, None);
104-
}
105-
_ => {}
106-
};
107-
108-
Some(())
109-
}
110-
11174
#[cfg(test)]
11275
mod tests {
11376
use crate::tests::check_edit;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ use crate::{
1313

1414
pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) {
1515
let _p = profile::span("complete_type_path");
16-
if ctx.is_path_disallowed() {
17-
return;
18-
}
1916

2017
let (&is_absolute_path, qualifier) = match ctx.path_context() {
2118
Some(PathCompletionCtx {

0 commit comments

Comments
 (0)