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

Commit c1446a2

Browse files
committed
Move CompletionContext::impl_def into corresponding entities
1 parent 83e8f3a commit c1446a2

File tree

8 files changed

+97
-48
lines changed

8 files changed

+97
-48
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::iter;
2424

2525
use hir::{db::HirDatabase, known, ScopeDef};
2626
use ide_db::SymbolKind;
27+
use syntax::ast;
2728

2829
use crate::{
2930
context::Visible,
@@ -409,11 +410,12 @@ fn enum_variants_with_paths(
409410
acc: &mut Completions,
410411
ctx: &CompletionContext,
411412
enum_: hir::Enum,
413+
impl_: &Option<ast::Impl>,
412414
cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
413415
) {
414416
let variants = enum_.variants(ctx.db);
415417

416-
if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
418+
if let Some(impl_) = impl_.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
417419
if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) {
418420
for &variant in &variants {
419421
let self_path = hir::ModPath::from_segments(

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub(crate) fn complete_expr_path(
2727
in_condition,
2828
ty,
2929
incomplete_let,
30+
impl_,
3031
) = match path_ctx {
3132
&PathCompletionCtx {
3233
kind:
@@ -39,6 +40,7 @@ pub(crate) fn complete_expr_path(
3940
ref ref_expr_parent,
4041
ref is_func_update,
4142
ref innermost_ret_ty,
43+
ref impl_,
4244
..
4345
},
4446
ref qualified,
@@ -53,6 +55,7 @@ pub(crate) fn complete_expr_path(
5355
in_condition,
5456
innermost_ret_ty,
5557
incomplete_let,
58+
impl_,
5659
),
5760
_ => return,
5861
};
@@ -181,8 +184,7 @@ pub(crate) fn complete_expr_path(
181184
if let Some(adt) =
182185
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
183186
{
184-
let self_ty =
185-
(|| ctx.sema.to_def(ctx.impl_def.as_ref()?)?.self_ty(ctx.db).as_adt())();
187+
let self_ty = (|| ctx.sema.to_def(impl_.as_ref()?)?.self_ty(ctx.db).as_adt())();
186188
let complete_self = self_ty == Some(adt);
187189

188190
match adt {
@@ -210,9 +212,15 @@ pub(crate) fn complete_expr_path(
210212
}
211213
}
212214
hir::Adt::Enum(e) => {
213-
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
214-
acc.add_qualified_enum_variant(ctx, variant, path)
215-
});
215+
super::enum_variants_with_paths(
216+
acc,
217+
ctx,
218+
e,
219+
impl_,
220+
|acc, ctx, variant, path| {
221+
acc.add_qualified_enum_variant(ctx, variant, path)
222+
},
223+
);
216224
}
217225
}
218226
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub(crate) fn complete_fn_param(
2424
ctx: &CompletionContext,
2525
pattern_ctx: &PatternContext,
2626
) -> Option<()> {
27-
let (param_list, _, param_kind) = match pattern_ctx {
28-
PatternContext { param_ctx: Some(kind), .. } => kind,
27+
let ((param_list, _, param_kind), impl_) = match pattern_ctx {
28+
PatternContext { param_ctx: Some(kind), impl_, .. } => (kind, impl_),
2929
_ => return None,
3030
};
3131

@@ -45,7 +45,7 @@ pub(crate) fn complete_fn_param(
4545

4646
match param_kind {
4747
ParamKind::Function(function) => {
48-
fill_fn_params(ctx, function, param_list, add_new_item_to_acc);
48+
fill_fn_params(ctx, function, param_list, impl_, add_new_item_to_acc);
4949
}
5050
ParamKind::Closure(closure) => {
5151
let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?;
@@ -62,6 +62,7 @@ fn fill_fn_params(
6262
ctx: &CompletionContext,
6363
function: &ast::Fn,
6464
param_list: &ast::ParamList,
65+
impl_: &Option<ast::Impl>,
6566
mut add_new_item_to_acc: impl FnMut(&str),
6667
) {
6768
let mut file_params = FxHashMap::default();
@@ -104,7 +105,7 @@ fn fill_fn_params(
104105
}
105106
remove_duplicated(&mut file_params, param_list.params());
106107
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
107-
if should_add_self_completions(ctx, param_list) {
108+
if should_add_self_completions(param_list, impl_) {
108109
self_completion_items.into_iter().for_each(|self_item| add_new_item_to_acc(self_item));
109110
}
110111

@@ -155,11 +156,10 @@ fn remove_duplicated(
155156
})
156157
}
157158

158-
fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamList) -> bool {
159-
let inside_impl = ctx.impl_def.is_some();
159+
fn should_add_self_completions(param_list: &ast::ParamList, impl_: &Option<ast::Impl>) -> bool {
160160
let no_params = param_list.params().next().is_none() && param_list.self_param().is_none();
161161

162-
inside_impl && no_params
162+
impl_.is_some() && no_params
163163
}
164164

165165
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange)> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn add_keywords(acc: &mut Completions, ctx: &CompletionContext, kind: Option<&It
6666
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));
6767
let in_extern_block = matches!(kind, Some(ItemListKind::ExternBlock));
6868
let in_trait = matches!(kind, Some(ItemListKind::Trait));
69-
let in_trait_impl = matches!(kind, Some(ItemListKind::TraitImpl));
69+
let in_trait_impl = matches!(kind, Some(ItemListKind::TraitImpl(_)));
7070
let in_inherent_impl = matches!(kind, Some(ItemListKind::Impl));
7171
let no_qualifiers = ctx.qualifier_ctx.vis_node.is_none();
7272
let in_block = matches!(kind, None);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn complete_trait_impl_name(
8181
kind,
8282
replacement_range(ctx, &item),
8383
// item -> ASSOC_ITEM_LIST -> IMPL
84-
ast::Impl::cast(item.parent()?.parent()?)?,
84+
&ast::Impl::cast(item.parent()?.parent()?)?,
8585
);
8686
Some(())
8787
}
@@ -97,7 +97,7 @@ pub(crate) fn complete_trait_impl_name_ref(
9797
kind:
9898
NameRefKind::Path(
9999
path_ctx @ PathCompletionCtx {
100-
kind: PathKind::Item { kind: ItemListKind::TraitImpl },
100+
kind: PathKind::Item { kind: ItemListKind::TraitImpl(Some(impl_)) },
101101
..
102102
},
103103
),
@@ -109,7 +109,7 @@ pub(crate) fn complete_trait_impl_name_ref(
109109
Some(name) => name.syntax().text_range(),
110110
None => ctx.source_range(),
111111
},
112-
ctx.impl_def.clone()?,
112+
impl_,
113113
),
114114
_ => (),
115115
}
@@ -121,10 +121,10 @@ fn complete_trait_impl(
121121
ctx: &CompletionContext,
122122
kind: ImplCompletionKind,
123123
replacement_range: TextRange,
124-
impl_def: ast::Impl,
124+
impl_def: &ast::Impl,
125125
) {
126-
if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {
127-
get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| {
126+
if let Some(hir_impl) = ctx.sema.to_def(impl_def) {
127+
get_missing_assoc_items(&ctx.sema, impl_def).into_iter().for_each(|item| {
128128
use self::ImplCompletionKind::*;
129129
match (item, kind) {
130130
(hir::AssocItem::Function(func), All | Fn) => {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ pub(crate) fn complete_pattern(
5151
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
5252
{
5353
if refutable || single_variant_enum(e) {
54-
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
55-
acc.add_qualified_variant_pat(ctx, variant, path);
56-
});
54+
super::enum_variants_with_paths(
55+
acc,
56+
ctx,
57+
e,
58+
&patctx.impl_,
59+
|acc, ctx, variant, path| {
60+
acc.add_qualified_variant_pat(ctx, variant, path);
61+
},
62+
);
5763
}
5864
}
5965

crates/ide-completion/src/context.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub(super) enum PathKind {
9898
is_func_update: Option<ast::RecordExpr>,
9999
self_param: Option<hir::SelfParam>,
100100
innermost_ret_ty: Option<hir::Type>,
101+
impl_: Option<ast::Impl>,
101102
},
102103
Type {
103104
location: TypeLocation,
@@ -143,12 +144,12 @@ pub(crate) enum TypeAscriptionTarget {
143144
}
144145

145146
/// The kind of item list a [`PathKind::Item`] belongs to.
146-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
147+
#[derive(Debug, PartialEq, Eq)]
147148
pub(super) enum ItemListKind {
148149
SourceFile,
149150
Module,
150151
Impl,
151-
TraitImpl,
152+
TraitImpl(Option<ast::Impl>),
152153
Trait,
153154
ExternBlock,
154155
}
@@ -179,6 +180,7 @@ pub(super) struct PatternContext {
179180
pub(super) mut_token: Option<SyntaxToken>,
180181
/// The record pattern this name or ref is a field of
181182
pub(super) record_pat: Option<ast::RecordPat>,
183+
pub(super) impl_: Option<ast::Impl>,
182184
}
183185

184186
/// The state of the lifetime we are completing.
@@ -320,10 +322,6 @@ pub(crate) struct CompletionContext<'a> {
320322
/// The expected type of what we are completing.
321323
pub(super) expected_type: Option<Type>,
322324

323-
/// The parent impl of the cursor position if it exists.
324-
// FIXME: This probably doesn't belong here
325-
pub(super) impl_def: Option<ast::Impl>,
326-
327325
// FIXME: This shouldn't exist
328326
pub(super) previous_token: Option<SyntaxToken>,
329327

@@ -497,7 +495,6 @@ impl<'a> CompletionContext<'a> {
497495
module,
498496
expected_name: None,
499497
expected_type: None,
500-
impl_def: None,
501498
previous_token: None,
502499
// dummy value, will be overwritten
503500
ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None },

0 commit comments

Comments
 (0)