convert TypeLocation::GenericArg to struct variant

This commit is contained in:
Max Heller 2023-08-08 20:37:23 -04:00
parent 0b57fa3931
commit 400f618a5c
4 changed files with 52 additions and 35 deletions

View file

@ -703,7 +703,7 @@ pub(super) fn complete_name_ref(
TypeLocation::TypeAscription(ascription) => { TypeLocation::TypeAscription(ascription) => {
r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription); r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription);
} }
TypeLocation::GenericArg(_) TypeLocation::GenericArg { .. }
| TypeLocation::AssocConstEq | TypeLocation::AssocConstEq
| TypeLocation::AssocTypeEq | TypeLocation::AssocTypeEq
| TypeLocation::TypeBound | TypeLocation::TypeBound

View file

@ -42,7 +42,7 @@ pub(crate) fn complete_type_path(
}; };
let add_assoc_item = |acc: &mut Completions, item| match item { let add_assoc_item = |acc: &mut Completions, item| match item {
hir::AssocItem::Const(ct) if matches!(location, TypeLocation::GenericArg(_)) => { hir::AssocItem::Const(ct) if matches!(location, TypeLocation::GenericArg { .. }) => {
acc.add_const(ctx, ct) acc.add_const(ctx, ct)
} }
hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => (), hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => (),
@ -156,33 +156,30 @@ pub(crate) fn complete_type_path(
}); });
return; return;
} }
TypeLocation::GenericArg(Some((arg_list, in_trait, _))) => { TypeLocation::GenericArg {
if let Some(trait_) = in_trait { args: Some(arg_list), of_trait: Some(trait_), ..
if arg_list.syntax().ancestors().find_map(ast::TypeBound::cast).is_some() { } => {
let arg_idx = arg_list if arg_list.syntax().ancestors().find_map(ast::TypeBound::cast).is_some() {
.generic_args() let arg_idx = arg_list
.filter(|arg| { .generic_args()
arg.syntax().text_range().end() .filter(|arg| {
< ctx.original_token.text_range().start() arg.syntax().text_range().end()
}) < ctx.original_token.text_range().start()
.count(); })
.count();
let n_required_params = let n_required_params = trait_.type_or_const_param_count(ctx.sema.db, true);
trait_.type_or_const_param_count(ctx.sema.db, true); if arg_idx >= n_required_params {
if arg_idx >= n_required_params { trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each(|it| {
trait_.items_with_supertraits(ctx.sema.db).into_iter().for_each( if let hir::AssocItem::TypeAlias(alias) = it {
|it| { cov_mark::hit!(complete_assoc_type_in_generics_list);
if let hir::AssocItem::TypeAlias(alias) = it { acc.add_type_alias_with_eq(ctx, alias);
cov_mark::hit!(complete_assoc_type_in_generics_list);
acc.add_type_alias_with_eq(ctx, alias);
}
},
);
let n_params = trait_.type_or_const_param_count(ctx.sema.db, false);
if arg_idx >= n_params {
return; // only show assoc types
} }
});
let n_params = trait_.type_or_const_param_count(ctx.sema.db, false);
if arg_idx >= n_params {
return; // only show assoc types
} }
} }
} }

View file

@ -156,7 +156,14 @@ pub(crate) enum TypeLocation {
TupleField, TupleField,
TypeAscription(TypeAscriptionTarget), TypeAscription(TypeAscriptionTarget),
/// Generic argument position e.g. `Foo<$0>` /// Generic argument position e.g. `Foo<$0>`
GenericArg(Option<(ast::GenericArgList, Option<hir::Trait>, Option<ast::GenericParam>)>), GenericArg {
/// The generic argument list containing the generic arg
args: Option<ast::GenericArgList>,
/// `Some(trait_)` if `trait_` is being instantiated with `args`
of_trait: Option<hir::Trait>,
/// The generic parameter being filled in by the generic arg
corresponding_param: Option<ast::GenericParam>,
},
/// Associated type equality constraint e.g. `Foo<Bar = $0>` /// Associated type equality constraint e.g. `Foo<Bar = $0>`
AssocTypeEq, AssocTypeEq,
/// Associated constant equality constraint e.g. `Foo<X = $0>` /// Associated constant equality constraint e.g. `Foo<X = $0>`
@ -171,13 +178,19 @@ impl TypeLocation {
pub(crate) fn complete_lifetimes(&self) -> bool { pub(crate) fn complete_lifetimes(&self) -> bool {
matches!( matches!(
self, self,
TypeLocation::GenericArg(Some((_, _, Some(ast::GenericParam::LifetimeParam(_))))) TypeLocation::GenericArg {
corresponding_param: Some(ast::GenericParam::LifetimeParam(_)),
..
}
) )
} }
pub(crate) fn complete_consts(&self) -> bool { pub(crate) fn complete_consts(&self) -> bool {
match self { match self {
TypeLocation::GenericArg(Some((_, _, Some(ast::GenericParam::ConstParam(_))))) => true, TypeLocation::GenericArg {
corresponding_param: Some(ast::GenericParam::ConstParam(_)),
..
} => true,
TypeLocation::AssocConstEq => true, TypeLocation::AssocConstEq => true,
_ => false, _ => false,
} }
@ -185,7 +198,7 @@ impl TypeLocation {
pub(crate) fn complete_types(&self) -> bool { pub(crate) fn complete_types(&self) -> bool {
match self { match self {
TypeLocation::GenericArg(Some((_, _, Some(param)))) => { TypeLocation::GenericArg { corresponding_param: Some(param), .. } => {
matches!(param, ast::GenericParam::TypeParam(_)) matches!(param, ast::GenericParam::TypeParam(_))
} }
TypeLocation::AssocConstEq => false, TypeLocation::AssocConstEq => false,

View file

@ -838,7 +838,15 @@ fn classify_name_ref(
})(); })();
(args, in_trait, param) (args, in_trait, param)
}); });
override_location.unwrap_or(TypeLocation::GenericArg(location)) let (arg_list, of_trait, corresponding_param) = match location {
Some((arg_list, of_trait, param)) => (Some(arg_list), of_trait, param),
_ => (None, None, None),
};
override_location.unwrap_or(TypeLocation::GenericArg {
args: arg_list,
of_trait,
corresponding_param,
})
}; };
let type_location = |node: &SyntaxNode| { let type_location = |node: &SyntaxNode| {
@ -899,9 +907,8 @@ fn classify_name_ref(
ast::GenericArg(it) => generic_arg_location(it), ast::GenericArg(it) => generic_arg_location(it),
// is this case needed? // is this case needed?
ast::GenericArgList(it) => { ast::GenericArgList(it) => {
let location = find_opt_node_in_file_compensated(sema, original_file, Some(it)) let args = find_opt_node_in_file_compensated(sema, original_file, Some(it));
.map(|node| (node, None, None)); TypeLocation::GenericArg { args, of_trait: None, corresponding_param: None }
TypeLocation::GenericArg(location)
}, },
ast::TupleField(_) => TypeLocation::TupleField, ast::TupleField(_) => TypeLocation::TupleField,
_ => return None, _ => return None,