Skip to content

[CodeCompletion] Implement context type analysis at subscript position #20066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3897,15 +3897,16 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return;

for (auto *VD : decls) {
if (!isa<AbstractFunctionDecl>(VD) ||
if ((!isa<AbstractFunctionDecl>(VD) && !isa<SubscriptDecl>(VD)) ||
shouldHideDeclFromCompletionResults(VD))
continue;
resolver->resolveDeclSignature(VD);
if (!VD->hasInterfaceType())
continue;
Type declaredMemberType = VD->getInterfaceType();
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
declaredMemberType = AFD->getMethodInterfaceType();
if (AFD->getDeclContext()->isTypeContext())
declaredMemberType = AFD->getMethodInterfaceType();

auto fnType =
baseTy->getTypeOfMember(DC.getParentModule(), VD, declaredMemberType);
Expand Down Expand Up @@ -3935,8 +3936,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

static bool
collectPossibleParamLists(DeclContext &DC, ApplyExpr *callExpr,
SmallVectorImpl<FunctionParams> &candidates) {
collectPossibleParamListsApply(DeclContext &DC, ApplyExpr *callExpr,
SmallVectorImpl<FunctionParams> &candidates) {
auto *fnExpr = callExpr->getFn();

if (auto type = fnExpr->getType()) {
Expand Down Expand Up @@ -3980,6 +3981,24 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
return !candidates.empty();
}

static bool collectPossibleParamListsSubscript(
DeclContext &DC, SubscriptExpr *subscriptExpr,
SmallVectorImpl<FunctionParams> &candidates) {
if (subscriptExpr->hasDecl()) {
if (auto SD =
dyn_cast<SubscriptDecl>(subscriptExpr->getDecl().getDecl())) {
auto declType = SD->getInterfaceType();
if (auto *funcType = declType->getAs<AnyFunctionType>())
candidates.push_back(funcType->getParams());
}
} else {
collectPossibleParamListByQualifiedLookup(DC, subscriptExpr->getBase(),
DeclBaseName::createSubscript(),
candidates);
}
return !candidates.empty();
}

static bool getPositionInArgs(DeclContext &DC, Expr *Args, Expr *CCExpr,
unsigned &Position, bool &HasName) {
if (auto TSE = dyn_cast<TupleShuffleExpr>(Args))
Expand Down Expand Up @@ -4040,26 +4059,34 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

static bool
collectArgumentExpectation(DeclContext &DC, ApplyExpr *CallE, Expr *CCExpr,
collectArgumentExpectation(DeclContext &DC, Expr *E, Expr *CCExpr,
std::vector<Type> &ExpectedTypes,
std::vector<StringRef> &ExpectedNames) {
// Collect parameter lists for possible func decls.
SmallVector<FunctionParams, 4> Candidates;
if (!collectPossibleParamLists(DC, CallE, Candidates))
return false;
Expr *Arg = nullptr;
if (auto *applyExpr = dyn_cast<ApplyExpr>(E)) {
if (!collectPossibleParamListsApply(DC, applyExpr, Candidates))
return false;
Arg = applyExpr->getArg();
} else if (auto *subscriptExpr = dyn_cast<SubscriptExpr>(E)) {
if (!collectPossibleParamListsSubscript(DC, subscriptExpr, Candidates))
return false;
Arg = subscriptExpr->getIndex();
}

// Determine the position of code completion token in call argument.
unsigned Position;
bool HasName;
if (!getPositionInArgs(DC, CallE->getArg(), CCExpr, Position, HasName))
if (!getPositionInArgs(DC, Arg, CCExpr, Position, HasName))
return false;
if (!translateArgIndexToParamIndex(CallE->getArg(), Position, HasName))
if (!translateArgIndexToParamIndex(Arg, Position, HasName))
return false;

// Collect possible types (or labels) at the position.
{
bool MayNeedName =
!HasName && isa<CallExpr>(CallE) && !CallE->isImplicit();
bool MayNeedName = !HasName && !E->isImplicit() &&
(isa<CallExpr>(E) | isa<SubscriptExpr>(E));
SmallPtrSet<TypeBase *, 4> seenTypes;
SmallPtrSet<Identifier, 4> seenNames;
for (auto Params : Candidates) {
Expand Down Expand Up @@ -5307,11 +5334,13 @@ class CodeCompletionTypeContextAnalyzer {
case ExprKind::Binary:
case ExprKind::PrefixUnary:
case ExprKind::Assign:
case ExprKind::Subscript:
return true;
case ExprKind::Tuple: {
auto ParentE = Parent.getAsExpr();
return !ParentE || (!isa<CallExpr>(ParentE) &&
!isa<BinaryExpr>(ParentE)&&
!isa<SubscriptExpr>(ParentE) &&
!isa<BinaryExpr>(ParentE) &&
!isa<TupleShuffleExpr>(ParentE));
}
default:
Expand Down Expand Up @@ -5351,13 +5380,13 @@ class CodeCompletionTypeContextAnalyzer {
SmallVectorImpl<StringRef> &PossibleNames) {
switch (Parent->getKind()) {
case ExprKind::Call:
case ExprKind::Subscript:
case ExprKind::Binary:
case ExprKind::PrefixUnary: {
std::vector<Type> PotentialTypes;
std::vector<StringRef> ExpectedNames;
CompletionLookup::collectArgumentExpectation(
*DC, cast<ApplyExpr>(Parent), ParsedExpr, PotentialTypes,
ExpectedNames);
*DC, Parent, ParsedExpr, PotentialTypes, ExpectedNames);
for (Type Ty : PotentialTypes)
Callback(Ty);
for (auto name : ExpectedNames)
Expand Down
6 changes: 1 addition & 5 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,12 +1232,8 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
/*isPostfix=*/true, isExprBasic, lSquareLoc, indexArgs,
indexArgLabels, indexArgLabelLocs, rSquareLoc, trailingClosure,
SyntaxKind::FunctionCallArgumentList);
if (status.hasCodeCompletion())
return makeParserCodeCompletionResult<Expr>();
if (status.isError() || Result.isNull())
return nullptr;
Result = makeParserResult(
Result,
status | Result,
SubscriptExpr::create(Context, Result.get(), lSquareLoc, indexArgs,
indexArgLabels, indexArgLabelLocs, rSquareLoc,
trailingClosure, ConcreteDeclRef(),
Expand Down
49 changes: 49 additions & 0 deletions test/IDE/complete_call_arg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SHUFFLE_2 | %FileCheck %s -check-prefix=SHUFFLE_2
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SHUFFLE_3 | %FileCheck %s -check-prefix=SHUFFLE_3

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SUBSCRIPT_1 | %FileCheck %s -check-prefix=SUBSCRIPT_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SUBSCRIPT_1_DOT | %FileCheck %s -check-prefix=SUBSCRIPT_1_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SUBSCRIPT_2 | %FileCheck %s -check-prefix=SUBSCRIPT_2
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SUBSCRIPT_2_DOT | %FileCheck %s -check-prefix=SUBSCRIPT_2_DOT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SUBSCRIPT_3 | %FileCheck %s -check-prefix=SUBSCRIPT_3
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SUBSCRIPT_3_DOT | %FileCheck %s -check-prefix=SUBSCRIPT_3_DOT

var i1 = 1
var i2 = 2
var oi1 : Int?
Expand Down Expand Up @@ -460,3 +467,45 @@ func testTupleShuffle() {
// SHUFFLE_3-DAG: Decl[EnumElement]/ExprSpecific: foo[#SimpleEnum#]; name=foo
// SHUFFLE_3-DAG: Decl[EnumElement]/ExprSpecific: bar[#SimpleEnum#]; name=bar
// SHUFFLE_3-DAG: Decl[EnumElement]/ExprSpecific: baz[#SimpleEnum#]; name=baz

class HasSubscript {
subscript(idx: Int) -> String {}
subscript(idx: Int, default defaultValue: String) -> String {}
}
func testSubscript(obj: HasSubscript, intValue: Int, strValue: String) {
let _ = obj[#^SUBSCRIPT_1^#
// SUBSCRIPT_1: Begin completions
// SUBSCRIPT_1-DAG: Decl[GlobalVar]/CurrModule/TypeRelation[Identical]: i1[#Int#]; name=i1
// SUBSCRIPT_1-DAG: Decl[GlobalVar]/CurrModule/TypeRelation[Identical]: i2[#Int#]; name=i2
// SUBSCRIPT_1-DAG: Decl[GlobalVar]/CurrModule: s1[#String#]; name=s1
// SUBSCRIPT_1-DAG: Decl[GlobalVar]/CurrModule: s2[#String#]; name=s2

let _ = obj[.#^SUBSCRIPT_1_DOT^#
// SUBSCRIPT_1_DOT: Begin completions
// SUBSCRIPT_1_DOT-NOT: i1
// SUBSCRIPT_1_DOT-NOT: s1
// SUBSCRIPT_1_DOT-DAG: Decl[StaticVar]/Super: max[#Int#]; name=max
// SUBSCRIPT_1_DOT-DAG: Decl[StaticVar]/Super: min[#Int#]; name=min

let _ = obj[42, #^SUBSCRIPT_2^#
// SUBSCRIPT_2: Begin completions, 1 items
// SUBSCRIPT_2-NEXT: Keyword/ExprSpecific: default: [#Argument name#]; name=default:

let _ = obj[42, .#^SUBSCRIPT_2_DOT^#
// SUBSCRIPT_2_DOT-NOT: Begin completions

let _ = obj[42, default: #^SUBSCRIPT_3^#
// SUBSCRIPT_3: Begin completions
// SUBSCRIPT_3-DAG: Decl[GlobalVar]/CurrModule: i1[#Int#]; name=i1
// SUBSCRIPT_3-DAG: Decl[GlobalVar]/CurrModule: i2[#Int#]; name=i2
// SUBSCRIPT_3-DAG: Decl[GlobalVar]/CurrModule/TypeRelation[Identical]: s1[#String#]; name=s1
// SUBSCRIPT_3-DAG: Decl[GlobalVar]/CurrModule/TypeRelation[Identical]: s2[#String#]; name=s2

let _ = obj[42, default: .#^SUBSCRIPT_3_DOT^#
// SUBSCRIPT_3_DOT: Begin completions
// SUBSCRIPT_3_DOT-NOT: i1
// SUBSCRIPT_3_DOT-NOT: s1
// SUBSCRIPT_3_DOT-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Identical]: init()[#String#]; name=init()
// SUBSCRIPT_3_DOT-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Identical]: init({#(c): Character#})[#String#]; name=init(c: Character)

}