Skip to content

Commit 400f618

Browse files
committed
convert TypeLocation::GenericArg to struct variant
1 parent 0b57fa3 commit 400f618

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ pub(super) fn complete_name_ref(
703703
TypeLocation::TypeAscription(ascription) => {
704704
r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription);
705705
}
706-
TypeLocation::GenericArg(_)
706+
TypeLocation::GenericArg { .. }
707707
| TypeLocation::AssocConstEq
708708
| TypeLocation::AssocTypeEq
709709
| TypeLocation::TypeBound

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn complete_type_path(
4242
};
4343

4444
let add_assoc_item = |acc: &mut Completions, item| match item {
45-
hir::AssocItem::Const(ct) if matches!(location, TypeLocation::GenericArg(_)) => {
45+
hir::AssocItem::Const(ct) if matches!(location, TypeLocation::GenericArg { .. }) => {
4646
acc.add_const(ctx, ct)
4747
}
4848
hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => (),
@@ -156,33 +156,30 @@ pub(crate) fn complete_type_path(
156156
});
157157
return;
158158
}
159-
TypeLocation::GenericArg(Some((arg_list, in_trait, _))) => {
160-
if let Some(trait_) = in_trait {
161-
if arg_list.syntax().ancestors().find_map(ast::TypeBound::cast).is_some() {
162-
let arg_idx = arg_list
163-
.generic_args()
164-
.filter(|arg| {
165-
arg.syntax().text_range().end()
166-
< ctx.original_token.text_range().start()
167-
})
168-
.count();
169-
170-
let n_required_params =
171-
trait_.type_or_const_param_count(ctx.sema.db, true);
172-
if arg_idx >= n_required_params {
173-
trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each(
174-
|it| {
175-
if let hir::AssocItem::TypeAlias(alias) = it {
176-
cov_mark::hit!(complete_assoc_type_in_generics_list);
177-
acc.add_type_alias_with_eq(ctx, alias);
178-
}
179-
},
180-
);
181-
182-
let n_params = trait_.type_or_const_param_count(ctx.sema.db, false);
183-
if arg_idx >= n_params {
184-
return; // only show assoc types
159+
TypeLocation::GenericArg {
160+
args: Some(arg_list), of_trait: Some(trait_), ..
161+
} => {
162+
if arg_list.syntax().ancestors().find_map(ast::TypeBound::cast).is_some() {
163+
let arg_idx = arg_list
164+
.generic_args()
165+
.filter(|arg| {
166+
arg.syntax().text_range().end()
167+
< ctx.original_token.text_range().start()
168+
})
169+
.count();
170+
171+
let n_required_params = trait_.type_or_const_param_count(ctx.sema.db, true);
172+
if arg_idx >= n_required_params {
173+
trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each(|it| {
174+
if let hir::AssocItem::TypeAlias(alias) = it {
175+
cov_mark::hit!(complete_assoc_type_in_generics_list);
176+
acc.add_type_alias_with_eq(ctx, alias);
185177
}
178+
});
179+
180+
let n_params = trait_.type_or_const_param_count(ctx.sema.db, false);
181+
if arg_idx >= n_params {
182+
return; // only show assoc types
186183
}
187184
}
188185
}

crates/ide-completion/src/context.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ pub(crate) enum TypeLocation {
156156
TupleField,
157157
TypeAscription(TypeAscriptionTarget),
158158
/// Generic argument position e.g. `Foo<$0>`
159-
GenericArg(Option<(ast::GenericArgList, Option<hir::Trait>, Option<ast::GenericParam>)>),
159+
GenericArg {
160+
/// The generic argument list containing the generic arg
161+
args: Option<ast::GenericArgList>,
162+
/// `Some(trait_)` if `trait_` is being instantiated with `args`
163+
of_trait: Option<hir::Trait>,
164+
/// The generic parameter being filled in by the generic arg
165+
corresponding_param: Option<ast::GenericParam>,
166+
},
160167
/// Associated type equality constraint e.g. `Foo<Bar = $0>`
161168
AssocTypeEq,
162169
/// Associated constant equality constraint e.g. `Foo<X = $0>`
@@ -171,21 +178,27 @@ impl TypeLocation {
171178
pub(crate) fn complete_lifetimes(&self) -> bool {
172179
matches!(
173180
self,
174-
TypeLocation::GenericArg(Some((_, _, Some(ast::GenericParam::LifetimeParam(_)))))
181+
TypeLocation::GenericArg {
182+
corresponding_param: Some(ast::GenericParam::LifetimeParam(_)),
183+
..
184+
}
175185
)
176186
}
177187

178188
pub(crate) fn complete_consts(&self) -> bool {
179189
match self {
180-
TypeLocation::GenericArg(Some((_, _, Some(ast::GenericParam::ConstParam(_))))) => true,
190+
TypeLocation::GenericArg {
191+
corresponding_param: Some(ast::GenericParam::ConstParam(_)),
192+
..
193+
} => true,
181194
TypeLocation::AssocConstEq => true,
182195
_ => false,
183196
}
184197
}
185198

186199
pub(crate) fn complete_types(&self) -> bool {
187200
match self {
188-
TypeLocation::GenericArg(Some((_, _, Some(param)))) => {
201+
TypeLocation::GenericArg { corresponding_param: Some(param), .. } => {
189202
matches!(param, ast::GenericParam::TypeParam(_))
190203
}
191204
TypeLocation::AssocConstEq => false,

crates/ide-completion/src/context/analysis.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,15 @@ fn classify_name_ref(
838838
})();
839839
(args, in_trait, param)
840840
});
841-
override_location.unwrap_or(TypeLocation::GenericArg(location))
841+
let (arg_list, of_trait, corresponding_param) = match location {
842+
Some((arg_list, of_trait, param)) => (Some(arg_list), of_trait, param),
843+
_ => (None, None, None),
844+
};
845+
override_location.unwrap_or(TypeLocation::GenericArg {
846+
args: arg_list,
847+
of_trait,
848+
corresponding_param,
849+
})
842850
};
843851

844852
let type_location = |node: &SyntaxNode| {
@@ -899,9 +907,8 @@ fn classify_name_ref(
899907
ast::GenericArg(it) => generic_arg_location(it),
900908
// is this case needed?
901909
ast::GenericArgList(it) => {
902-
let location = find_opt_node_in_file_compensated(sema, original_file, Some(it))
903-
.map(|node| (node, None, None));
904-
TypeLocation::GenericArg(location)
910+
let args = find_opt_node_in_file_compensated(sema, original_file, Some(it));
911+
TypeLocation::GenericArg { args, of_trait: None, corresponding_param: None }
905912
},
906913
ast::TupleField(_) => TypeLocation::TupleField,
907914
_ => return None,

0 commit comments

Comments
 (0)