@@ -3897,15 +3897,16 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3897
3897
return ;
3898
3898
3899
3899
for (auto *VD : decls) {
3900
- if (!isa<AbstractFunctionDecl>(VD) ||
3900
+ if (( !isa<AbstractFunctionDecl>(VD) && !isa<SubscriptDecl>(VD) ) ||
3901
3901
shouldHideDeclFromCompletionResults (VD))
3902
3902
continue ;
3903
3903
resolver->resolveDeclSignature (VD);
3904
3904
if (!VD->hasInterfaceType ())
3905
3905
continue ;
3906
3906
Type declaredMemberType = VD->getInterfaceType ();
3907
3907
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
3908
- declaredMemberType = AFD->getMethodInterfaceType ();
3908
+ if (AFD->getDeclContext ()->isTypeContext ())
3909
+ declaredMemberType = AFD->getMethodInterfaceType ();
3909
3910
3910
3911
auto fnType =
3911
3912
baseTy->getTypeOfMember (DC.getParentModule (), VD, declaredMemberType);
@@ -3935,8 +3936,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3935
3936
}
3936
3937
3937
3938
static bool
3938
- collectPossibleParamLists (DeclContext &DC, ApplyExpr *callExpr,
3939
- SmallVectorImpl<FunctionParams> &candidates) {
3939
+ collectPossibleParamListsApply (DeclContext &DC, ApplyExpr *callExpr,
3940
+ SmallVectorImpl<FunctionParams> &candidates) {
3940
3941
auto *fnExpr = callExpr->getFn ();
3941
3942
3942
3943
if (auto type = fnExpr->getType ()) {
@@ -3980,6 +3981,24 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3980
3981
return !candidates.empty ();
3981
3982
}
3982
3983
3984
+ static bool collectPossibleParamListsSubscript (
3985
+ DeclContext &DC, SubscriptExpr *subscriptExpr,
3986
+ SmallVectorImpl<FunctionParams> &candidates) {
3987
+ if (subscriptExpr->hasDecl ()) {
3988
+ if (auto SD =
3989
+ dyn_cast<SubscriptDecl>(subscriptExpr->getDecl ().getDecl ())) {
3990
+ auto declType = SD->getInterfaceType ();
3991
+ if (auto *funcType = declType->getAs <AnyFunctionType>())
3992
+ candidates.push_back (funcType->getParams ());
3993
+ }
3994
+ } else {
3995
+ collectPossibleParamListByQualifiedLookup (DC, subscriptExpr->getBase (),
3996
+ DeclBaseName::createSubscript (),
3997
+ candidates);
3998
+ }
3999
+ return !candidates.empty ();
4000
+ }
4001
+
3983
4002
static bool getPositionInArgs (DeclContext &DC, Expr *Args, Expr *CCExpr,
3984
4003
unsigned &Position, bool &HasName) {
3985
4004
if (auto TSE = dyn_cast<TupleShuffleExpr>(Args))
@@ -4040,26 +4059,34 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4040
4059
}
4041
4060
4042
4061
static bool
4043
- collectArgumentExpectation (DeclContext &DC, ApplyExpr *CallE , Expr *CCExpr,
4062
+ collectArgumentExpectation (DeclContext &DC, Expr *E , Expr *CCExpr,
4044
4063
std::vector<Type> &ExpectedTypes,
4045
4064
std::vector<StringRef> &ExpectedNames) {
4046
4065
// Collect parameter lists for possible func decls.
4047
4066
SmallVector<FunctionParams, 4 > Candidates;
4048
- if (!collectPossibleParamLists (DC, CallE, Candidates))
4049
- return false ;
4067
+ Expr *Arg = nullptr ;
4068
+ if (auto *applyExpr = dyn_cast<ApplyExpr>(E)) {
4069
+ if (!collectPossibleParamListsApply (DC, applyExpr, Candidates))
4070
+ return false ;
4071
+ Arg = applyExpr->getArg ();
4072
+ } else if (auto *subscriptExpr = dyn_cast<SubscriptExpr>(E)) {
4073
+ if (!collectPossibleParamListsSubscript (DC, subscriptExpr, Candidates))
4074
+ return false ;
4075
+ Arg = subscriptExpr->getIndex ();
4076
+ }
4050
4077
4051
4078
// Determine the position of code completion token in call argument.
4052
4079
unsigned Position;
4053
4080
bool HasName;
4054
- if (!getPositionInArgs (DC, CallE-> getArg () , CCExpr, Position, HasName))
4081
+ if (!getPositionInArgs (DC, Arg , CCExpr, Position, HasName))
4055
4082
return false ;
4056
- if (!translateArgIndexToParamIndex (CallE-> getArg () , Position, HasName))
4083
+ if (!translateArgIndexToParamIndex (Arg , Position, HasName))
4057
4084
return false ;
4058
4085
4059
4086
// Collect possible types (or labels) at the position.
4060
4087
{
4061
- bool MayNeedName =
4062
- !HasName && isa<CallExpr>(CallE) && !CallE-> isImplicit ( );
4088
+ bool MayNeedName = !HasName && !E-> isImplicit () &&
4089
+ ( isa<CallExpr>(E) | isa<SubscriptExpr>(E) );
4063
4090
SmallPtrSet<TypeBase *, 4 > seenTypes;
4064
4091
SmallPtrSet<Identifier, 4 > seenNames;
4065
4092
for (auto Params : Candidates) {
@@ -5307,11 +5334,13 @@ class CodeCompletionTypeContextAnalyzer {
5307
5334
case ExprKind::Binary:
5308
5335
case ExprKind::PrefixUnary:
5309
5336
case ExprKind::Assign:
5337
+ case ExprKind::Subscript:
5310
5338
return true ;
5311
5339
case ExprKind::Tuple: {
5312
5340
auto ParentE = Parent.getAsExpr ();
5313
5341
return !ParentE || (!isa<CallExpr>(ParentE) &&
5314
- !isa<BinaryExpr>(ParentE)&&
5342
+ !isa<SubscriptExpr>(ParentE) &&
5343
+ !isa<BinaryExpr>(ParentE) &&
5315
5344
!isa<TupleShuffleExpr>(ParentE));
5316
5345
}
5317
5346
default :
@@ -5351,13 +5380,13 @@ class CodeCompletionTypeContextAnalyzer {
5351
5380
SmallVectorImpl<StringRef> &PossibleNames) {
5352
5381
switch (Parent->getKind ()) {
5353
5382
case ExprKind::Call:
5383
+ case ExprKind::Subscript:
5354
5384
case ExprKind::Binary:
5355
5385
case ExprKind::PrefixUnary: {
5356
5386
std::vector<Type> PotentialTypes;
5357
5387
std::vector<StringRef> ExpectedNames;
5358
5388
CompletionLookup::collectArgumentExpectation (
5359
- *DC, cast<ApplyExpr>(Parent), ParsedExpr, PotentialTypes,
5360
- ExpectedNames);
5389
+ *DC, Parent, ParsedExpr, PotentialTypes, ExpectedNames);
5361
5390
for (Type Ty : PotentialTypes)
5362
5391
Callback (Ty);
5363
5392
for (auto name : ExpectedNames)
0 commit comments