Skip to content

Commit 5e28b06

Browse files
committed
Add in keyword completion and a test
1 parent 7349383 commit 5e28b06

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

include/swift/IDE/CodeCompletionResult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ enum class CompletionKind : uint8_t {
214214
ReturnStmtExpr,
215215
YieldStmtExpr,
216216
ForEachSequence,
217+
ForEachInKw,
217218
AfterPoundExpr,
218219
AfterPoundDirective,
219220
PlatformConditon,

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ class CodeCompletionCallbacks {
137137
/// -- no tokens provided by user.
138138
virtual void completeForEachSequenceBeginning(CodeCompletionExpr *E) {};
139139

140+
/// Add comment
141+
virtual void completeForEachInKeyword(){};
142+
140143
/// Complete a given expr-postfix.
141144
virtual void completePostfixExpr(Expr *E, bool hasSpace) {};
142145

lib/IDE/CodeCompletion.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
242242
void completeStmtOrExpr(CodeCompletionExpr *E) override;
243243
void completePostfixExprBeginning(CodeCompletionExpr *E) override;
244244
void completeForEachSequenceBeginning(CodeCompletionExpr *E) override;
245+
void completeForEachInKeyword() override;
245246
void completePostfixExpr(Expr *E, bool hasSpace) override;
246247
void completePostfixExprParen(Expr *E, Expr *CodeCompletionE) override;
247248
void completeExprKeyPath(KeyPathExpr *KPE, SourceLoc DotLoc) override;
@@ -365,6 +366,12 @@ void CodeCompletionCallbacksImpl::completeForEachSequenceBeginning(
365366
CodeCompleteTokenExpr = E;
366367
}
367368

369+
void CodeCompletionCallbacksImpl::completeForEachInKeyword() {
370+
assert(P.Tok.is(tok::code_complete));
371+
Kind = CompletionKind::ForEachInKw;
372+
CurDeclContext = P.CurDeclContext;
373+
}
374+
368375
void CodeCompletionCallbacksImpl::completePostfixExpr(Expr *E, bool hasSpace) {
369376
assert(P.Tok.is(tok::code_complete));
370377

@@ -1069,6 +1076,9 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
10691076
addKeyword(Sink, "await", CodeCompletionKeywordKind::None);
10701077
addKeyword(Sink, "var", CodeCompletionKeywordKind::kw_var);
10711078
addKeyword(Sink, "case", CodeCompletionKeywordKind::kw_case);
1079+
break;
1080+
case CompletionKind::ForEachInKw:
1081+
addKeyword(Sink, "in", CodeCompletionKeywordKind::kw_in);
10721082
}
10731083
}
10741084

@@ -1960,6 +1970,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
19601970
case CompletionKind::CaseStmtKeyword:
19611971
case CompletionKind::EffectsSpecifier:
19621972
case CompletionKind::ForEachPatternBeginning:
1973+
case CompletionKind::ForEachInKw:
19631974
// Handled earlier by keyword completions.
19641975
break;
19651976
}

lib/Parse/ParseStmt.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,14 +2292,23 @@ ParserResult<Stmt> Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) {
22922292
diagnose(LBraceLoc, diag::expected_foreach_container);
22932293
Container = makeParserErrorResult(new (Context) ErrorExpr(LBraceLoc));
22942294
} else if (Tok.is(tok::code_complete)) {
2295-
Container =
2296-
makeParserResult(new (Context) CodeCompletionExpr(Tok.getLoc()));
2297-
Container.setHasCodeCompletionAndIsError();
2298-
Status |= Container;
2299-
if (CodeCompletion)
2300-
CodeCompletion->completeForEachSequenceBeginning(
2301-
cast<CodeCompletionExpr>(Container.get()));
2302-
consumeToken(tok::code_complete);
2295+
if (InLoc.isInvalid()) {
2296+
// Write something to complete In
2297+
if (CodeCompletion)
2298+
CodeCompletion->completeForEachInKeyword();
2299+
consumeToken(tok::code_complete);
2300+
return makeParserCodeCompletionStatus();
2301+
} else {
2302+
// Complete everything else
2303+
Container =
2304+
makeParserResult(new (Context) CodeCompletionExpr(Tok.getLoc()));
2305+
Container.setHasCodeCompletionAndIsError();
2306+
Status |= Container;
2307+
if (CodeCompletion)
2308+
CodeCompletion->completeForEachSequenceBeginning(
2309+
cast<CodeCompletionExpr>(Container.get()));
2310+
consumeToken(tok::code_complete);
2311+
}
23032312
} else {
23042313
Container = parseExprBasic(diag::expected_foreach_container);
23052314
Status |= Container;

test/IDE/complete_loop.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LOOP_3 | %FileCheck %s -check-prefix=LOOP_3
66
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LOOP_4 | %FileCheck %s -check-prefix=LOOP_4
77
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LOOP_5 | %FileCheck %s -check-prefix=LOOP_5
8+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=LOOP_6 | %FileCheck %s -check-prefix=LOOP_6
89

910
class Gen {
1011
func IntGen() -> Int { return 0 }
@@ -67,3 +68,11 @@ class C {
6768
}
6869
// LOOP_5: Begin completions
6970
}
71+
72+
do {
73+
for value #^LOOP_6^#
74+
}
75+
// LOOP_6: Begin completions, 1 items
76+
// LOOP_6-CHECK-NEXT: Keyword[in]/None: in; name=in
77+
// LOOP_6-CHECK-NEXT: End completions
78+

0 commit comments

Comments
 (0)