Skip to content

[SourceKit] Disable labeled trailing closure completion support except code completion #32276

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
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
4 changes: 4 additions & 0 deletions include/swift/Parse/CodeCompletionCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ class CodeCompletionCallbacks {

virtual void completeCallArg(CodeCompletionExpr *E, bool isFirst) {};

virtual bool canPerformCompleteLabeledTrailingClosure() const {
return false;
}

virtual void completeLabeledTrailingClosure(CodeCompletionExpr *E,
bool isAtStartOfLine) {};

Expand Down
4 changes: 4 additions & 0 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,10 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
void completeLabeledTrailingClosure(CodeCompletionExpr *E,
bool isAtStartOfLine) override;

bool canPerformCompleteLabeledTrailingClosure() const override {
return true;
}

void completeReturnStmt(CodeCompletionExpr *E) override;
void completeYieldStmt(CodeCompletionExpr *E,
Optional<unsigned> yieldIndex) override;
Expand Down
7 changes: 7 additions & 0 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3213,6 +3213,13 @@ Parser::parseTrailingClosures(bool isExprBasic, SourceRange calleeRange,
if (!Tok.is(tok::code_complete))
break;

// If the current completion mode doesn't support trailing closure
// completion, leave the token here and let "postfix completion" to
// handle it.
if (CodeCompletion &&
!CodeCompletion->canPerformCompleteLabeledTrailingClosure())
break;

// foo() {} <token>
auto CCExpr = new (Context) CodeCompletionExpr(Tok.getLoc());
if (CodeCompletion)
Expand Down
27 changes: 27 additions & 0 deletions test/IDE/conforming-methods-afterclosure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-swift-ide-test -conforming-methods -source-filename %s -code-completion-token=AFTER_TRAILINGCLOSURE -module-name MyModule -conforming-methods-expected-types 's:8MyModule7TargetPP' | %FileCheck %s -check-prefix=AFTER_TRAILINGCLOSURE

public protocol TargetP {}
struct ConcreteP: TargetP {}

public struct MyStruct {
init(arg1: Int = 0, fn: () -> Int) {}

public func returnSomeP -> some TargetP { ConcreteP() }
public func returnConcreteP -> ConcreteP { ConcreteP() }
public func reutrnInt -> Int { 1 }
}

func test() {
MyStruct {
1
} #^AFTER_TRAILINGCLOSURE^#
}

//AFTER_TRAILINGCLOSURE: -----BEGIN CONFORMING METHOD LIST-----
//AFTER_TRAILINGCLOSURE-NEXT: - TypeName: MyStruct
//AFTER_TRAILINGCLOSURE-NEXT: - Members:
//AFTER_TRAILINGCLOSURE-NEXT: - Name: returnSomeP()
//AFTER_TRAILINGCLOSURE-NEXT: TypeName: some TargetP
//AFTER_TRAILINGCLOSURE-NEXT: - Name: returnConcreteP()
//AFTER_TRAILINGCLOSURE-NEXT: TypeName: ConcreteP
//AFTER_TRAILINGCLOSURE-NEXT: -----END CONFORMING METHOD LIST-----