Skip to content

Commit c58a157

Browse files
committed
[CodeCompletion] 'case' keyword completion at the top of 'switch' stmt
rdar://problem/35943849 (cherry picked from commit fd542e2)
1 parent b281a3c commit c58a157

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;
@@ -4442,6 +4443,11 @@ void CodeCompletionCallbacksImpl::completeTypeIdentifierWithoutDot(
44424443
CurDeclContext = P.CurDeclContext;
44434444
}
44444445

4446+
void CodeCompletionCallbacksImpl::completeCaseStmtKeyword() {
4447+
Kind = CompletionKind::CaseStmtKeyword;
4448+
CurDeclContext = P.CurDeclContext;
4449+
}
4450+
44454451
void CodeCompletionCallbacksImpl::completeCaseStmtBeginning() {
44464452
assert(!InEnumElementRawValue);
44474453

@@ -4623,6 +4629,11 @@ static void addStmtKeywords(CodeCompletionResultSink &Sink, bool MaybeFuncBody)
46234629
#include "swift/Syntax/TokenKinds.def"
46244630
}
46254631

4632+
static void addCaseStmtKeywords(CodeCompletionResultSink &Sink) {
4633+
addKeyword(Sink, "case", CodeCompletionKeywordKind::kw_case);
4634+
addKeyword(Sink, "default", CodeCompletionKeywordKind::kw_default);
4635+
}
4636+
46264637
static void addLetVarKeywords(CodeCompletionResultSink &Sink) {
46274638
addKeyword(Sink, "let", CodeCompletionKeywordKind::kw_let);
46284639
addKeyword(Sink, "var", CodeCompletionKeywordKind::kw_var);
@@ -4712,6 +4723,10 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
47124723
addAnyTypeKeyword(Sink);
47134724
break;
47144725

4726+
case CompletionKind::CaseStmtKeyword:
4727+
addCaseStmtKeywords(Sink);
4728+
break;
4729+
47154730
case CompletionKind::PostfixExpr:
47164731
case CompletionKind::PostfixExprParen:
47174732
case CompletionKind::SuperExpr:
@@ -5214,12 +5229,13 @@ void CodeCompletionCallbacksImpl::doneParsing() {
52145229
}
52155230
}
52165231
break;
5217-
case CompletionKind::AfterIfStmtElse:
5218-
// Handled earlier by keyword completions.
5219-
break;
52205232
case CompletionKind::PrecedenceGroup:
52215233
Lookup.getPrecedenceGroupCompletions(SyntxKind);
52225234
break;
5235+
case CompletionKind::AfterIfStmtElse:
5236+
case CompletionKind::CaseStmtKeyword:
5237+
// Handled earlier by keyword completions.
5238+
break;
52235239
}
52245240

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

lib/Parse/ParseStmt.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,11 @@ Parser::parseStmtCases(SmallVectorImpl<ASTNode> &cases, bool IsActive) {
22732273
if (auto PDD = PoundDiagnosticResult.getPtrOrNull()) {
22742274
cases.emplace_back(PDD);
22752275
}
2276+
} else if (Tok.is(tok::code_complete)) {
2277+
if (CodeCompletion)
2278+
CodeCompletion->completeCaseStmtKeyword();
2279+
consumeToken(tok::code_complete);
2280+
return makeParserCodeCompletionStatus();
22762281
} else {
22772282
// If there are non-case-label statements at the start of the switch body,
22782283
// 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)