Skip to content

[CodeCompletion] Fallback to nominal member completion after trailing closure #32521

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
Jun 24, 2020
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
30 changes: 22 additions & 8 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6158,15 +6158,29 @@ void CodeCompletionCallbacksImpl::doneParsing() {
if (IsAtStartOfLine) {
// foo() {}
// <HERE>
// Global completion.

auto &Sink = CompletionContext.getResultSink();
addDeclKeywords(Sink);
addStmtKeywords(Sink, MaybeFuncBody);
addSuperKeyword(Sink);
addLetVarKeywords(Sink);
addExprKeywords(Sink);
addAnyTypeKeyword(Sink, CurDeclContext->getASTContext().TheAnyType);
DoPostfixExprBeginning();
if (isa<Initializer>(CurDeclContext))
CurDeclContext = CurDeclContext->getParent();

if (CurDeclContext->isTypeContext()) {
// Override completion (CompletionKind::NominalMemberBeginning).
addDeclKeywords(Sink);
addLetVarKeywords(Sink);
SmallVector<StringRef, 0> ParsedKeywords;
CompletionOverrideLookup OverrideLookup(Sink, Context, CurDeclContext,
ParsedKeywords, SourceLoc());
OverrideLookup.getOverrideCompletions(SourceLoc());
} else {
// Global completion (CompletionKind::PostfixExprBeginning).
addDeclKeywords(Sink);
addStmtKeywords(Sink, MaybeFuncBody);
addSuperKeyword(Sink);
addLetVarKeywords(Sink);
addExprKeywords(Sink);
addAnyTypeKeyword(Sink, Context.TheAnyType);
DoPostfixExprBeginning();
}
} else {
// foo() {} <HERE>
// Member completion.
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,7 @@ Parser::parseTrailingClosures(bool isExprBasic, SourceRange calleeRange,
if (CodeCompletion)
CodeCompletion->completeLabeledTrailingClosure(CCExpr, Tok.isAtStartOfLine());
consumeToken(tok::code_complete);
result.hasCodeCompletion();
result.setHasCodeCompletion();
closures.push_back({Identifier(), SourceLoc(), CCExpr});
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions test/IDE/complete_multiple_trailingclosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_REQUIRED_NEWLINE_3 | %FileCheck %s -check-prefix=INIT_REQUIRED_NEWLINE_3
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FALLBACK_1 | %FileCheck %s -check-prefix=INIT_FALLBACK
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=INIT_FALLBACK_2 | %FileCheck %s -check-prefix=INIT_FALLBACK
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MEMBERDECL_SAMELINE | %FileCheck %s -check-prefix=MEMBERDECL_SAMELINE
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MEMBERDECL_NEWLINE | %FileCheck %s -check-prefix=MEMBERDECL_NEWLINE

func globalFunc1(fn1: () -> Int, fn2: () -> String) {}
func testGlobalFunc() {
Expand Down Expand Up @@ -207,3 +209,28 @@ func testFallbackPostfix() {
1
} #^INIT_FALLBACK_2^#
}

protocol P {
func foo()
}
struct TestNominalMember: P {
var value = MyStruct().method1 { 1 } #^MEMBERDECL_SAMELINE^#
#^MEMBERDECL_NEWLINE^#

// MEMBERDECL_SAMELINE: Begin completions, 4 items
// MEMBERDECL_SAMELINE-DAG: Pattern/ExprSpecific: {#fn2: (() -> String)? {() -> String in|}#}[#(() -> String)?#]; name=fn2: (() -> String)?
// MEMBERDECL_SAMELINE-DAG: Decl[InstanceMethod]/CurrNominal: .enumFunc()[#Void#]; name=enumFunc()
// MEMBERDECL_SAMELINE-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]/IsSystem: [' ']+ {#SimpleEnum#}[#SimpleEnum#]; name=+ SimpleEnum
// MEMBERDECL_SAMELINE-DAG: Keyword[self]/CurrNominal: .self[#SimpleEnum#]; name=self
// MEMBERDECL_SAMELINE: End completions

// MEMBERDECL_NEWLINE: Begin completions
// MEMBERDECL_NEWLINE-DAG: Pattern/ExprSpecific: {#fn2: (() -> String)? {() -> String in|}#}[#(() -> String)?#]; name=fn2: (() -> String)?
// MEMBERDECL_NEWLINE-DAG: Keyword[enum]/None: enum; name=enum
// MEMBERDECL_NEWLINE-DAG: Keyword[func]/None: func; name=func
// MEMBERDECL_NEWLINE-DAG: Keyword[private]/None: private; name=private
// MEMBERDECL_NEWLINE-DAG: Keyword/None: lazy; name=lazy
// MEMBERDECL_NEWLINE-DAG: Keyword[var]/None: var; name=var
// MEMBERDECL_NEWLINE-DAG: Decl[InstanceMethod]/Super: func foo() {|}; name=foo()
// MEMBERDECL_NEWLINE: End completions
}