Skip to content

Commit fd542e2

Browse files
committed
[CodeCompletion] 'case' keyword completion at the top of 'switch' stmt
rdar://problem/35943849
1 parent d58c072 commit fd542e2

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ enum class CompletionKind {
492492
TypeSimpleBeginning,
493493
TypeIdentifierWithDot,
494494
TypeIdentifierWithoutDot,
495+
CaseStmtKeyword,
495496
CaseStmtBeginning,
496497
CaseStmtDotPrefix,
497498
NominalMemberBeginning,

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ class CodeCompletionCallbacks {
165165
/// Complete a given type-identifier when there is no trailing dot.
166166
virtual void completeTypeIdentifierWithoutDot(IdentTypeRepr *ITR) {};
167167

168+
/// Complete the beginning of a case statement at the top of switch stmt.
169+
virtual void completeCaseStmtKeyword() {};
170+
168171
/// Complete at the beginning of a case stmt pattern.
169172
virtual void completeCaseStmtBeginning() {};
170173

lib/IDE/CodeCompletion.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13481348
void completeTypeIdentifierWithDot(IdentTypeRepr *ITR) override;
13491349
void completeTypeIdentifierWithoutDot(IdentTypeRepr *ITR) override;
13501350

1351+
void completeCaseStmtKeyword() override;
13511352
void completeCaseStmtBeginning() override;
13521353
void completeCaseStmtDotPrefix() override;
13531354
void completeDeclAttrKeyword(Decl *D, bool Sil, bool Param) override;
@@ -4417,6 +4418,11 @@ void CodeCompletionCallbacksImpl::completeTypeIdentifierWithoutDot(
44174418
CurDeclContext = P.CurDeclContext;
44184419
}
44194420

4421+
void CodeCompletionCallbacksImpl::completeCaseStmtKeyword() {
4422+
Kind = CompletionKind::CaseStmtKeyword;
4423+
CurDeclContext = P.CurDeclContext;
4424+
}
4425+
44204426
void CodeCompletionCallbacksImpl::completeCaseStmtBeginning() {
44214427
assert(!InEnumElementRawValue);
44224428

@@ -4598,6 +4604,11 @@ static void addStmtKeywords(CodeCompletionResultSink &Sink, bool MaybeFuncBody)
45984604
#include "swift/Syntax/TokenKinds.def"
45994605
}
46004606

4607+
static void addCaseStmtKeywords(CodeCompletionResultSink &Sink) {
4608+
addKeyword(Sink, "case", CodeCompletionKeywordKind::kw_case);
4609+
addKeyword(Sink, "default", CodeCompletionKeywordKind::kw_default);
4610+
}
4611+
46014612
static void addLetVarKeywords(CodeCompletionResultSink &Sink) {
46024613
addKeyword(Sink, "let", CodeCompletionKeywordKind::kw_let);
46034614
addKeyword(Sink, "var", CodeCompletionKeywordKind::kw_var);
@@ -4687,6 +4698,10 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
46874698
addAnyTypeKeyword(Sink);
46884699
break;
46894700

4701+
case CompletionKind::CaseStmtKeyword:
4702+
addCaseStmtKeywords(Sink);
4703+
break;
4704+
46904705
case CompletionKind::PostfixExpr:
46914706
case CompletionKind::PostfixExprParen:
46924707
case CompletionKind::SuperExpr:
@@ -5189,12 +5204,13 @@ void CodeCompletionCallbacksImpl::doneParsing() {
51895204
}
51905205
}
51915206
break;
5192-
case CompletionKind::AfterIfStmtElse:
5193-
// Handled earlier by keyword completions.
5194-
break;
51955207
case CompletionKind::PrecedenceGroup:
51965208
Lookup.getPrecedenceGroupCompletions(SyntxKind);
51975209
break;
5210+
case CompletionKind::AfterIfStmtElse:
5211+
case CompletionKind::CaseStmtKeyword:
5212+
// Handled earlier by keyword completions.
5213+
break;
51985214
}
51995215

52005216
for (auto &Request: Lookup.RequestedCachedResults) {

lib/Parse/ParseStmt.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,11 @@ Parser::parseStmtCases(SmallVectorImpl<ASTNode> &cases, bool IsActive) {
22742274
if (auto PDD = PoundDiagnosticResult.getPtrOrNull()) {
22752275
cases.emplace_back(PDD);
22762276
}
2277+
} else if (Tok.is(tok::code_complete)) {
2278+
if (CodeCompletion)
2279+
CodeCompletion->completeCaseStmtKeyword();
2280+
consumeToken(tok::code_complete);
2281+
return makeParserCodeCompletionStatus();
22772282
} else {
22782283
// If there are non-case-label statements at the start of the switch body,
22792284
// raise an error and recover by discarding them.

test/IDE/complete_keywords.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
// RUN: %FileCheck %s -check-prefix=KW_EXPR < %t.expr6
9191
// RUN: %FileCheck %s -check-prefix=KW_EXPR_NEG < %t.expr6
9292

93+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SWITCH_TOP | %FileCheck %s -check-prefix=KW_CASE
94+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=SWITCH_IN_CASE | %FileCheck %s -check-prefix=KW_CASE
95+
9396
// KW_RETURN: Keyword[return]/None: return{{; name=.+$}}
9497
// KW_NO_RETURN-NOT: Keyword[return]
9598

@@ -401,3 +404,16 @@ func inExpr5() {
401404
func inExpr6() -> Int {
402405
return #^EXPR_6^#
403406
}
407+
408+
func inSwitch(val: Int) {
409+
switch val {
410+
#^SWITCH_TOP^#
411+
case 1:
412+
foo()
413+
#^SWITCH_IN_CASE^#
414+
}
415+
// Begin completions
416+
// KW_CASE-DAG: Keyword[case]/None: case; name=case
417+
// KW_CASE-DAG: Keyword[default]/None: default; name=default
418+
// End completions
419+
}

0 commit comments

Comments
 (0)