Skip to content

Commit 695c725

Browse files
committed
[CodeComplete] Return true from canParseType if type contains code completion token
From my point of view, a type that contains a code completion token can still be parsed as a type and as such, `canParseType` should return `true` for e.g. `#^COMPLETE^#` or `SomeType.#^COMPLETE^#`. Changing this fixes an issue that caused no type completions to be shown inside a enum case decl. Fixes rdar://71984715
1 parent 3d2fd56 commit 695c725

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,13 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
351351
param.FirstNameLoc = SourceLoc();
352352
param.SecondName = Identifier();
353353
param.SecondNameLoc = SourceLoc();
354-
} else if (isBareType) {
354+
} else if (isBareType && !Tok.is(tok::code_complete)) {
355355
// Otherwise, if this is a bare type, then the user forgot to name the
356356
// parameter, e.g. "func foo(Int) {}"
357+
// Don't enter this case if the element could only be parsed as a bare
358+
// type because a code completion token is positioned here. In this case
359+
// the user is about to type the parameter label and we shouldn't
360+
// suggest types.
357361
SourceLoc typeStartLoc = Tok.getLoc();
358362
auto type = parseType(diag::expected_parameter_type, false);
359363
status |= type;

lib/Parse/ParseType.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,9 +1410,10 @@ bool Parser::canParseType() {
14101410
switch (Tok.getKind()) {
14111411
case tok::kw_Self:
14121412
case tok::kw_Any:
1413-
if (!canParseTypeIdentifier())
1414-
return false;
1415-
break;
1413+
case tok::code_complete:
1414+
if (!canParseTypeIdentifier())
1415+
return false;
1416+
break;
14161417
case tok::kw_protocol: // Deprecated composition syntax
14171418
case tok::identifier:
14181419
if (!canParseTypeIdentifierOrTypeComposition())
@@ -1500,7 +1501,7 @@ bool Parser::canParseTypeIdentifierOrTypeComposition() {
15001501

15011502
bool Parser::canParseSimpleTypeIdentifier() {
15021503
// Parse an identifier.
1503-
if (!Tok.isAny(tok::identifier, tok::kw_Self, tok::kw_Any))
1504+
if (!Tok.isAny(tok::identifier, tok::kw_Self, tok::kw_Any, tok::code_complete))
15041505
return false;
15051506
consumeToken();
15061507

test/IDE/complete_in_enum_decl.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ENUM_CASE_5 | %FileCheck %s -check-prefix=NO_RESULTS
66
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ENUM_CASE_6 | %FileCheck %s -check-prefix=NO_RESULTS
77

8+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TYPE_IN_ENUM_CASE_DECL | %FileCheck %s -check-prefix=TYPE_IN_ENUM_CASE_DECL
9+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=NESTED_TYPE_IN_ENUM_CASE_DECL | %FileCheck %s -check-prefix=NESTED_TYPE_IN_ENUM_CASE_DECL
10+
811
// NO_RESULTS: found code completion token
912
// NO_RESULTS-NOT: Begin completions
1013

@@ -33,3 +36,20 @@ enum EnumCase6 : Int {
3336
case Foo = super.#^ENUM_CASE_6^#
3437
}
3538

39+
enum EnumCase7 {
40+
case foo(#^TYPE_IN_ENUM_CASE_DECL^#)
41+
}
42+
// TYPE_IN_ENUM_CASE_DECL: Begin completions
43+
// TYPE_IN_ENUM_CASE_DECL-DAG: Decl[Enum]/CurrModule: EnumCase7[#EnumCase7#];
44+
// TYPE_IN_ENUM_CASE_DECL: End completions
45+
46+
struct Wrapper {
47+
struct Nested {}
48+
}
49+
enum EnumCase8 {
50+
case foo(Wrapper.#^NESTED_TYPE_IN_ENUM_CASE_DECL^#)
51+
}
52+
// NESTED_TYPE_IN_ENUM_CASE_DECL: Begin completions, 2 items
53+
// NESTED_TYPE_IN_ENUM_CASE_DECL-DAG: Decl[Struct]/CurrNominal: Nested[#Wrapper.Nested#];
54+
// NESTED_TYPE_IN_ENUM_CASE_DECL-DAG: Keyword/None: Type[#Wrapper.Type#];
55+
// NESTED_TYPE_IN_ENUM_CASE_DECL: End completions

0 commit comments

Comments
 (0)