Skip to content

[code-completion] Add a new custom completion context for a for-each sequence #11059

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
Jul 19, 2017
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
1 change: 1 addition & 0 deletions include/swift/IDE/CodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ enum class CompletionKind {
AssignmentRHS,
CallArg,
ReturnStmtExpr,
ForEachSequence,
AfterPound,
GenericParams,
SwiftKeyPath,
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Parse/CodeCompletionCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ class CodeCompletionCallbacks {
/// by user.
virtual void completePostfixExprBeginning(CodeCompletionExpr *E) = 0;

/// \brief Complete the beginning of expr-postfix in a for-each loop sequqence
/// -- no tokens provided by user.
virtual void completeForEachSequenceBeginning(CodeCompletionExpr *E) = 0;

/// \brief Complete a given expr-postfix.
virtual void completePostfixExpr(Expr *E, bool hasSpace) = 0;

Expand Down
11 changes: 11 additions & 0 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
void completeDotExpr(Expr *E, SourceLoc DotLoc) override;
void completeStmtOrExpr() override;
void completePostfixExprBeginning(CodeCompletionExpr *E) override;
void completeForEachSequenceBeginning(CodeCompletionExpr *E) override;
void completePostfixExpr(Expr *E, bool hasSpace) override;
void completePostfixExprParen(Expr *E, Expr *CodeCompletionE) override;
void completeExprSuper(SuperRefExpr *SRE) override;
Expand Down Expand Up @@ -4479,6 +4480,14 @@ void CodeCompletionCallbacksImpl::completePostfixExprBeginning(CodeCompletionExp
CodeCompleteTokenExpr = E;
}

void CodeCompletionCallbacksImpl::completeForEachSequenceBeginning(
CodeCompletionExpr *E) {
assert(P.Tok.is(tok::code_complete));
Kind = CompletionKind::ForEachSequence;
CurDeclContext = P.CurDeclContext;
CodeCompleteTokenExpr = E;
}

void CodeCompletionCallbacksImpl::completePostfixExpr(Expr *E, bool hasSpace) {
assert(P.Tok.is(tok::code_complete));

Expand Down Expand Up @@ -4840,6 +4849,7 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
case CompletionKind::AssignmentRHS:
case CompletionKind::ReturnStmtExpr:
case CompletionKind::PostfixExprBeginning:
case CompletionKind::ForEachSequence:
addSuperKeyword(Sink);
addLetVarKeywords(Sink);
addExprKeywords(Sink);
Expand Down Expand Up @@ -5191,6 +5201,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
DoPostfixExprBeginning();
break;

case CompletionKind::ForEachSequence:
case CompletionKind::PostfixExprBeginning: {
::CodeCompletionTypeContextAnalyzer Analyzer(CurDeclContext,
CodeCompleteTokenExpr);
Expand Down
9 changes: 9 additions & 0 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,15 @@ ParserResult<Stmt> Parser::parseStmtForEach(SourceLoc ForLoc,
SourceLoc LBraceLoc = Tok.getLoc();
diagnose(LBraceLoc, diag::expected_foreach_container);
Container = makeParserErrorResult(new (Context) ErrorExpr(LBraceLoc));
} else if (Tok.is(tok::code_complete)) {
Container =
makeParserResult(new (Context) CodeCompletionExpr(Tok.getLoc()));
Container.setHasCodeCompletion();
Status |= Container;
if (CodeCompletion)
CodeCompletion->completeForEachSequenceBeginning(
cast<CodeCompletionExpr>(Container.get()));
consumeToken(tok::code_complete);
} else {
Container = parseExprBasic(diag::expected_foreach_container);
if (Container.isNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
source.lang.swift.stmt,
source.lang.swift.type
]
},
{
key.name: "customForEach",
key.kind: myuid.customForEach,
key.context: [
source.lang.swift.foreach.sequence,
]
}
]
}
9 changes: 9 additions & 0 deletions test/SourceKit/CodeComplete/complete_custom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ func test() {
// stmt
()
let foo: // type
for x in { } // foreach.sequence
}

// ===--- Errors
Expand Down Expand Up @@ -62,6 +63,14 @@ func test() {
// TYPE-NEXT: key.name: "customType"
// TYPE-NOT: myuid

// RUN: %sourcekitd-test -json-request-path %S/Inputs/custom-completion/custom.json == \
// RUN: -req=complete.open -pos=5:12 %s -- %s | %FileCheck %s -check-prefix=FOREACH

// FOREACH-NOT: myuid
// FOREACH: myuid.customForEach
// FOREACH-NEXT: key.name: "customForEach"
// FOREACH-NOT: myuid

// ===--- Filtering.

// RUN: %sourcekitd-test -json-request-path %S/Inputs/custom-completion/custom.json == \
Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ struct CustomCompletionInfo {
Stmt = 1 << 0,
Expr = 1 << 1,
Type = 1 << 2,
ForEachSequence = 1 << 3,
};
swift::OptionSet<Context> Contexts;
};
Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/include/SourceKit/Core/ProtocolUIDs.def
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ KIND(ObjectLiteral, "source.lang.swift.syntaxtype.objectliteral")
KIND(Expr, "source.lang.swift.expr")
KIND(Stmt, "source.lang.swift.stmt")
KIND(Type, "source.lang.swift.type")
KIND(ForEachSequence, "source.lang.swift.foreach.sequence")

KIND(DiagNote, "source.diagnostic.severity.note")
KIND(DiagWarning, "source.diagnostic.severity.warning")
Expand Down
6 changes: 6 additions & 0 deletions tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
addCompletion(custom);
}
break;
case CompletionKind::ForEachSequence:
if (custom.Contexts.contains(CustomCompletionInfo::ForEachSequence)) {
changed = true;
addCompletion(custom);
}
break;
case CompletionKind::TypeSimpleBeginning:
if (custom.Contexts.contains(CustomCompletionInfo::Type)) {
changed = true;
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, ResponseReceiver Rec) {
CCInfo.Contexts |= CustomCompletionInfo::Stmt;
} else if (context == KindType) {
CCInfo.Contexts |= CustomCompletionInfo::Type;
} else if (context == KindForEachSequence) {
CCInfo.Contexts |= CustomCompletionInfo::ForEachSequence;
} else {
err = createErrorRequestInvalid("invalid value for 'key.context'");
return true;
Expand Down